Results 1 to 4 of 4

Thread: Measuring the Graphical Width of a String

  1. #1

    Thread Starter
    Addicted Member New2VB's Avatar
    Join Date
    Jun 2000
    Location
    Los Angeles
    Posts
    161

    Question

    I'm trying to develop a textbox that expands as the text in it wraps, but I am having difficulty figuring out a generic method for identifying the graphical width of a string without knowing the font that is being used.

    I have read about the ClientDC.GetTextExtent method in C++. Is there something like that in VB? What properties should I be looking at?

    I appreciate everyones' assistance.

    Regards,
    New2VB


  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    Hi,

    Im not really sure if this will be of any help...but thought I'd tell u anyway.

    There's an API GetTetExtentPoint - but I couldn't get any info on what it actually does.

    hope this is what u were looking for.

  3. #3
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Yep, that's the one:

    Code:
    'BAS Module
    
    Public Declare Function GetTextExtentPoint Lib "gdi32" Alias "GetTextExtentPointA" (ByVal hdc As Long, ByVal lpszString As String, ByVal cbString As Long, lpSize As txtSize) As Long
    
    Type TxtSize
        x as Long
        y as Long
    End Type
    
    'Form Module
    
    'Me.ScaleMode = 3 Or Use Screen.TwipsPerPixel
    ...
    Dim Size as txtSize
    Dim Str as String
    Str = Text1.Text
    Call FindTextLength(Me.hDC, Str, LenB(Str), Size)
    
    Text1.Width = Size.y
    I think that'll do the trick.

    I haven't tested it or anything so you might need to tweak it a bit.

    Hope it helps,

    Me
    Courgettes.

  4. #4
    Addicted Member
    Join Date
    Apr 2000
    Location
    Sheffield, England.
    Posts
    136
    Code:
    Private Sub Text1_Change()
        Dim MinWidth As Long
        
        MinWidth = 256
        
        Text1.Width = TextWidth(Text1.Text) + MinWidth
    End Sub
    Visual Basic 6 Enterprise Edition + SP4

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