Click to See Complete Forum and Search --> : please help a student in visual c++. how to get the size of a cstring of text


help student
April 26th, 1999, 08:08 AM
i need to select a cstring of text by clicking on any part of the text. the only problem is that the text can be of any length. how can i detect the area that the whole cstring of text covered?

one way is this: i create a CRect variable and this CRect variable in turn calls a function which inflate the rectangle which is supposed to contain the text by using this command, rect.inflate(x,y). this function will then return the size of the rectangle to the CRect variable.
then using the CRect variable.PtInRect(point) where point is the point where the mouse is click on, this function will return true if the mouse was click on the text. but how will i know how much to inflate the rectangle by? what value of x and y should i used since the cstring of text can be any length.

the main problem is how can i get the actual size of the text in terms of its x and y coordinates? or how do i get the x and y coordinates of the first and last letter of the cstring of text?

please help me
best regards

SKB
April 26th, 1999, 08:51 AM
If you are using an edit box, then you should be able to get the edit box to return the SelectedTex for you.

help student
April 26th, 1999, 09:40 AM
but i am not using an edit box. what i am doing is that i am writing some text on the windows area and i need to know the size of the area on which the cstring of the text is on. maybe the x an y coordinates of the first and last letter

Mike Weber
April 26th, 1999, 10:22 AM
Look at GetTextMetrics or something like that. If I remember correctly it will fill a structure that has a average character with member. It also depends on the font you are using. If you want to know which character was clicked on, then you should use a mono-spaced font (non-proportional). Otherwise you can use the average character width and multiply that by the number of characters in the string. That will give you the area of the string. Good luck

April 26th, 1999, 12:05 PM
What you need to do is to use CDC::DrawText(...) with the parameter DT_CALCRECT, with this flag set the method will return the size of the string instead of drawing the text on the dc. Take a look at the help files to get more informations.

Michael Hagen