May 05, 2024, 07:56:02 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Detect position of text in a control on screen

Started by Andy, July 03, 2012, 05:18:18 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

I have a window:

OPENWINDOW f3,250,120,720,280,@MINBOX|@MAXBOX|@SIZE,0," Add to banned sites.",&f3banadd_handler

and a static control

CONTROL f3,@STATIC,"Add to banned sites",0,30,720,25,@CTEDITCENTER,STATIC_75

I use @CTEDITCENTER to centre the text

Is there a way of detecting the position on screen of the letter 'A' in the word 'Add' as I would like to place a bitmap just to the left of it? OR is there a way to calculate what the position is (given the window size, font size and users screen resolution)?

Thanks,
Andy.


Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

GWS

Hi Andy,

Is this what you mean ? ..



def w:window
def style,Width,Height,run:int
def varW,varH,key,rectwidth:int
def a$:string

autodefine "off"

Width=500
Height=200

style = @sysmenu

openwindow w,-Width,0,Width,Height,style,0,"Text Location",&main
setwindowcolor w,rgb(0,20,50)
frontpen backbuffer,rgb(0,180,255)
centerwindow w

a$ = "Add to banned sites"
gettextsize w,a$,varW,varH
rectwidth = 60

CONTROL w,@STATIC,"Add to banned sites",(Width-varW)/2,30,140,25,@CTEDITCENTER|0x200,75
rect w,(width-varW-rectwidth)/2,30,30,25,RGB(255,0,0),RGB(0,0,255)

run = 1

WAITUNTIL run = 0
closewindow w
END

sub main(),int
select @CLASS
case @idclosewindow
  run = 0
case @idchar
key = @CODE
if (key = 27) then run = 0
endselect
return 0
endsub


all the best, :)

Graham
Tomorrow may be too late ..

Andy

Graham,

Thanks, yes that looks good to me.

Will do some testing tomorrow and let you know!

Thanks again,
All the best,
Andy.

:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

Graham, until I saw your example my mind drew a blank this morning on how to accomplish the task.
When I ran your example I noticed that the combination of the text and the box appeared to be off center. I measured and it was.
The text itself was centered(almost) and then the box was added to the left.

So I started playing with your example and making some adjustments.
The first thing I adjusted was the centered text.
When you want to center a control in a window/dialog you will always be off a little if you use 1/2 the value of the width used to create the window.
Doing it that way disregards the border widths.
So I added the GETCLIENTSIZE function to get the inside width.

Next I noticed that the rectwidth was actually double the width of the actual rectangle.
I changed the value to 30 and used that variable to draw the box instead of constants.

I also noticed that a constant was used for the width of the static control.
I modified that to use the width of the text you had calculated.
But doing that made the text butt up to the edge of the control.  I solved that by adding a space before and after the actual text string.

Then I adjusted all the positiong to get the combination of the static and the box centered exactly in the window.

Whether it's done your way or my way depends on what the ultimate bitmap is and its size along with what else is around them on the screen.

Just for the fun of it I added a button that shows how to change the text in the STATIC and readjust things.


autodefine "off"
int run
int varW,varH,key,rectwidth
string a$
int l,t,w,h
window win


openwindow win,0,0,500,200,@sysmenu,0,"Text Location",&main
setwindowcolor win,rgb(0,20,50)

a$ = "Add to banned sites"
gettextsize win," "+a$+" ",varW,varH
rectwidth = 30

getclientsize win, l,t,w,h
CONTROL win,@STATIC,a$,(w-varW+rectwidth)/2,30,varW,25,@CTEDITCENTER|0x200,75
rect win,(w-varW-rectwidth)/2,30,rectwidth,25,RGB(255,0,0),RGB(0,0,255)
CONTROL win,@button,"Update",w-90,h-45,70,25,@CTEDITCENTER|0x200,10
run = 1

WAITUNTIL run = 0
closewindow win
END

sub main(),int
select @CLASS
case @IDCREATE
centerwindow win
case @idclosewindow
  run = 0
case @idchar
key = @CODE
if (key = 27) then run = 0
case @IDCONTROL
select @CONTRoLID
case 10
'clear box
rect win,(w-varW-rectwidth)/2,30,rectwidth,25,RGB(0,20,50),RGB(0,20,50)
a$ = "This shows what different text"
gettextsize win," "+a$+" ",varW,varH
setsize win,(w-varW+rectwidth)/2,30,varW,25,75
setcontroltext win,75,a$
rect win,(w-varW-rectwidth)/2,30,rectwidth,25,RGB(255,0,0),RGB(0,0,255)
endselect
endselect
return 0
endsub

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

GWS

Hi Larry,

You've obviously been having fun with this little puzzle  ;D

I must admit I rushed it off and didn't think too much about accuracy ..  ::)

Your method is looking good, but I think maybe the text box needs to be considered separately from the rectangle (or image as it will eventually be).

Then, it will probably need the rectangle/image to keep a few pixels away from the text box to look neat.
It just needs a bit of fiddling with the positioning 'x' co-ordinate to get it just right ..  :)

I imagine Andy will be able to position his image to suit his taste.

all the best, :)

Graham
Tomorrow may be too late ..

LarryMc

Quote from: GWS on July 03, 2012, 12:30:44 PM
You've obviously been having fun with this little puzzle  ;D
I needed a break from the hair-pulling and teeth gnashing I'm doing with this debugger thing for the new IDE.

And like I said, when I woke up this morning my brain stayed asleep and I wouldn't have been able to offer any solution without your posting.

Edit:Speaking of the new IDE, I know it is adaptable for use with Aurora.  If there was someone with enough knowledge to separate the IDE portion of CBasic from the interpreter it could be used there also.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

GWS

Larry,

I wake up most mornings like that ..  ::)

I've had another fiddle with the positioning, and I think this might be right now ..


autodefine "off"
int run
int varW,varH,key,rectwidth
string a$
int l,t,w,h
window win

openwindow win,0,0,500,200,@sysmenu,0,"Text Location",&main
setwindowcolor win,rgb(0,20,50)

a$ = "Add to banned sites"
gettextsize win," "+a$+" ",varW,varH
rectwidth = 30

getclientsize win, l,t,w,h
CONTROL win,@STATIC,a$,(w-varW)/2,30,varW,25,@CTEDITCENTER|0x200,75
rect win,(w-varW)/2-rectwidth-3,30,rectwidth,25,RGB(255,0,0),RGB(0,0,255)
CONTROL win,@button,"Update",w-90,h-45,70,25,@CTEDITCENTER|0x200,10
run = 1

WAITUNTIL run = 0
closewindow win
END

sub main(),int
select @CLASS
case @IDCREATE
centerwindow win
case @idclosewindow
  run = 0
case @idchar
key = @CODE
if (key = 27) then run = 0
case @IDCONTROL
select @CONTRoLID
case 10
'clear box
rect win,(w-varW)/2-rectwidth-3,30,rectwidth,25,RGB(0,20,50),RGB(0,20,50)
a$ = "This shows different text"
gettextsize win," "+a$+" ",varW,varH
setsize win,(w-varW)/2,30,varW,25,75
setcontroltext win,75,a$
rect win,(w-varW)/2-rectwidth-3,30,rectwidth,25,RGB(255,0,0),RGB(0,0,255)
endselect
endselect
return 0
endsub


I never could make head nor tail of how CBasic works.  As soon as I knew it was OOP'y I glazed over.
Why on earth folk don't just use straightforward programming I don't know - it would be much easier to modify and build on.

all the best, :)

Graham
Tomorrow may be too late ..

Andy

Larry and Graham,

Thanks,

Both examples are very good, I think it's down to one person's visual perspective.

Works well for me
, just one thing - if you use:
SETFONT win,fontname,12, weight2, 0, BUTTON_75

the control can become too small to fit the text depending on the font size etc.

But thanks again!!
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

GWS

Andy,

You're welcome ..  :)

Remember to call GETTEXTSIZE after you've used SETFONT .. then it should set the sizes correctly.

all the best, :)

Graham
Tomorrow may be too late ..