How can I get text's width and height in pixels (I can't use TextWidth and TextHeight)?
There's one API function for this, but I don't know how to use it! That function was, if I remember correctly, GetTextMetrics.
Printable View
How can I get text's width and height in pixels (I can't use TextWidth and TextHeight)?
There's one API function for this, but I don't know how to use it! That function was, if I remember correctly, GetTextMetrics.
Getting the texts width and heigth using GetTextMetrics is almost undoable. Consider different widths for every character, and don't forget there can be a overlapping of characters (eg some fonts have the upper part of the f invading the space of the next character).
What is wrong with TextWidth and TextHeight? If the scalemode isn't set to pixels, you could do a conversion.
To give you an idea of the complexity of GetTextMetrics, It uses a TEXTMETRIC structure which is defined as follows.
Code:Type TEXTMETRIC ' 53 Bytes
tmHeight As Long
tmAscent As Long
tmDescent As Long
tmInternalLeading As Long
tmExternalLeading As Long
tmAveCharWidth As Long
tmMaxCharWidth As Long
tmWeight As Long
tmOverhang As Long
tmDigitizedAspectX As Long
tmDigitizedAspectY As Long
tmFirstChar As Byte
tmLastChar As Byte
tmDefaultChar As Byte
tmBreakChar As Byte
tmItalic As Byte
tmUnderlined As Byte
tmStruckOut As Byte
tmPitchAndFamily As Byte
tmCharSet As Byte
End Type
Well, why not post the rest of the code it sounds like something I could use too.
I haven't got any code. It just looked it up because you mentioned it. The API declaration is :
Public Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long
Call it with the device context of the object that has the font selected, pass a variable of type TEXTMETRIC and it will fill the structure for you. To get the actual textwidth is a different thing. Maybe you can use the tmAveCharWidth, but this will only give you an average.
So how do you do it then???
I'd like to be able to resize a label or textbox to exactly fit the text no matter the font etc.
...and say resize it on the text_change event
As far as the label is concerned, why not use the .AutoSize property of it? This will adjust the size of the label to the text that it contains.
Imar
grrrr.
I'm not really looking for a quick work around (I know this isn't my thread but I understand his point).
lets say I want to resize all the columns of a Flexgrid by the width of the widest row entry in that column (like excel/explorer does) on double click of column header seperator.
??????????????????????????????????????????
I need this width and height for DirectX.
>??????????????????????????????????????????
I agree:)