Wednesday, March 07, 2007

Determine the Width and Height of the Text...

CDC::GetTextExtent and CDC::GetTabbedTextExtent functions enable an application to determine the width, in logical units, of a string of characters rendered in a proportional font. (Use GetTabbedTextExtent if the string contains tab characters.) Given a character position n, you can compute the corresponding caret position by calling GetTextExtent or GetTabbedTextExtent to find the cumulative width of the first n characters. If the string "Hello, world" is displayed at the position specified by a CPoint object named point and dc is a device context object, the following statements position the caret between the "w" and "o" in "world":

CSize size = dc.GetTextExtent (_T ("Hello, w"), 8);
SetCaretPos (CPoint (point.x + size.cx, point.y));



GetTextExtent returns a CSize object whose cx and cy members reflect the string's width and height.

No comments: