Results 1 to 11 of 11

Thread: How to use API to calculate the width and height of a Label's caption?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    8

    How to use API to calculate the width and height of a Label's caption?

    I'm a VB beginner, I know how to use the TextWidth and TextHeight of a Form or Picturebox, but I don't know how to use API to calculate the width and height of a Label's caption.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to use API to calculate the width and height of a Label's caption?

    This isn't a straightforward type of question, nor answer.

    First, labels have a width and height property. Granted that doesn't give you the space required for the actual text. And if the label is allowed to wordwrap, things can get complicated more quickly.

    Maybe you can give more details to what you are trying to do? Otherwise, look at these APIs & VB examples for: GetTextExtentPoint32 & DrawText
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: How to use API to calculate the width and height of a Label's caption?

    If it were me, I'd "borrow" the form's TextWidth and TextHeight methods to figure it out.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    8

    Re: How to use API to calculate the width and height of a Label's caption?

    Quote Originally Posted by LaVolpe View Post
    This isn't a straightforward type of question, nor answer.

    First, labels have a width and height property. Granted that doesn't give you the space required for the actual text. And if the label is allowed to wordwrap, things can get complicated more quickly.

    Maybe you can give more details to what you are trying to do? Otherwise, look at these APIs & VB examples for: GetTextExtentPoint32 & DrawText
    Quote Originally Posted by Elroy View Post
    If it were me, I'd "borrow" the form's TextWidth and TextHeight methods to figure it out.

    Thank you, LaVolpe and Elroy.

    My current approach is this:

    Code:
    Private Sub Command3_Click()
    Dim FontName As String:     Dim FontSize As Single
    Dim FontBold As Boolean
    
    With Me
        FontName = .FontName
        FontSize = .FontSize
        FontBold = .FontBold
        
        .FontName = Label1.FontName
        .FontSize = Label1.FontSize
        .FontBold = Label1.FontBold
        
        MsgBox .TextWidth(Label1.Caption)
        
        .FontName = FontName
        .FontSize = FontSize
        .FontBold = FontBold
    End With
        
    End Sub

    I hope to achieve the following function:

    Code:
    Public Function getTextWidth(objFont As StdFont, Text As String) As Single
    
    End Function

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to use API to calculate the width and height of a Label's caption?

    If memory serves there is an auto size property for labels. You could in theory have a label that is not visible and set to auto and may be able to get the size from that.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to use API to calculate the width and height of a Label's caption?

    Quote Originally Posted by yfhao View Post
    I hope to achieve the following function:
    Code:
    Public Function getTextWidth(objFont As StdFont, Text As String) As Single
    
    End Function
    If the string to be measured is for display on a form/control, maybe also pass along to your function a VB object with a hDC property, i.e., the form, a picbox, etc. Then steps are truly simple, no APIs needed

    - cache original font in passed hDC object, i.e., Set tmpFont = passedObj.Font
    - put passed font into that object: Set passedObj.Font = objFont
    - use TextWidth,TextHeight as needed
    - replace original font: Set passedObj.Font = tmpFont
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: How to use API to calculate the width and height of a Label's caption?

    And this was suppose to be added to post #3, but I was trying to add it just as the forums went down for maintenance.

    Something like the following (a default Form1, with a single Label1 on it):

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Label1.Caption = "asdf asdf asdf asdf asdf asdf asdf asdf"
    End Sub
    
    Private Sub Label1_Click()
        Dim fnt As StdFont
        Dim sc As Long
        Dim w As Long
        Dim h As Long
        '
        Set fnt = Form1.Font                    ' Save form's Font so we can put it back.
        sc = Form1.ScaleMode                    ' Save form's ScaleMode so we can put it back.
        Set Form1.Font = Label1.Font            ' Set form's font to caption's font.
        Form1.ScaleMode = vbTwips               ' We'll get/report things in twips.
        '
        w = Form1.TextWidth(Label1.Caption)     ' Get width.
        h = Form1.TextHeight(Label1.Caption)    ' Get height.
        '
        Form1.ScaleMode = sc                    ' Put back ScaleMode.
        Set Form1.Font = fnt                    ' Put back Font.
        '
        MsgBox "In twips, Label1's width is " & Format$(w) & " and its height is " & Format$(h) & "."
    End Sub
    
    
    Now, this won't account for any WordWrap in the Label1. That's an entirely different problem.

    Also, you could have specified the units in the TextWidth/TextHeight calls, rather than the way I did it, but 6-of-one-half-dozen-of-another.

    Enjoy,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    8

    Re: How to use API to calculate the width and height of a Label's caption?

    Quote Originally Posted by DataMiser View Post
    If memory serves there is an auto size property for labels. You could in theory have a label that is not visible and set to auto and may be able to get the size from that.
    AutoSize seems to be a good solution, but it only works with VB.Lable, not for TextBox and CommandButton.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    8

    Re: How to use API to calculate the width and height of a Label's caption?

    Quote Originally Posted by LaVolpe View Post
    If the string to be measured is for display on a form/control, maybe also pass along to your function a VB object with a hDC property, i.e., the form, a picbox, etc. Then steps are truly simple, no APIs needed

    - cache original font in passed hDC object, i.e., Set tmpFont = passedObj.Font
    - put passed font into that object: Set passedObj.Font = objFont
    - use TextWidth,TextHeight as needed
    - replace original font: Set passedObj.Font = tmpFont
    Thank you for your reply!

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    8

    Re: How to use API to calculate the width and height of a Label's caption?

    Quote Originally Posted by Elroy View Post
    And this was suppose to be added to post #3, but I was trying to add it just as the forums went down for maintenance.

    Something like the following (a default Form1, with a single Label1 on it):

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Label1.Caption = "asdf asdf asdf asdf asdf asdf asdf asdf"
    End Sub
    
    Private Sub Label1_Click()
        Dim fnt As StdFont
        Dim sc As Long
        Dim w As Long
        Dim h As Long
        '
        Set fnt = Form1.Font                    ' Save form's Font so we can put it back.
        sc = Form1.ScaleMode                    ' Save form's ScaleMode so we can put it back.
        Set Form1.Font = Label1.Font            ' Set form's font to caption's font.
        Form1.ScaleMode = vbTwips               ' We'll get/report things in twips.
        '
        w = Form1.TextWidth(Label1.Caption)     ' Get width.
        h = Form1.TextHeight(Label1.Caption)    ' Get height.
        '
        Form1.ScaleMode = sc                    ' Put back ScaleMode.
        Set Form1.Font = fnt                    ' Put back Font.
        '
        MsgBox "In twips, Label1's width is " & Format$(w) & " and its height is " & Format$(h) & "."
    End Sub
    
    
    Now, this won't account for any WordWrap in the Label1. That's an entirely different problem.

    Also, you could have specified the units in the TextWidth/TextHeight calls, rather than the way I did it, but 6-of-one-half-dozen-of-another.

    Enjoy,
    Elroy

    Thank you for your reply!

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to use API to calculate the width and height of a Label's caption?

    Quote Originally Posted by yfhao View Post
    AutoSize seems to be a good solution, but it only works with VB.Lable, not for TextBox and CommandButton.
    Yes that is true but the question here was about a label without mention of any other type of control.

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