Results 1 to 8 of 8

Thread: Getting Text's height and width

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    29

    Question

    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.

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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

  3. #3
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    ma,usa
    Posts
    485

    Smile

    Well, why not post the rest of the code it sounds like something I could use too.

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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.

  5. #5
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    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

  6. #6
    Guest
    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

  7. #7
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Question

    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.

    ??????????????????????????????????????????


  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    29

    Exclamation

    I need this width and height for DirectX.

    >??????????????????????????????????????????
    I agree

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width