Results 1 to 1 of 1

Thread: Get the width and height of a text without having a Form

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Get the width and height of a text without having a Form

    I created this class as an answer to a question asked in the General VB Forum and thought that it could fit here. This class has a TextWidth and a TextHeight property which works the same way as it does for a Form, a PictureBox, or the Printer object. The only difference is that you don't need any Forms or Controls to use it. So if you need to messure the text length (in pixels) from within an ActiveX DLL this is the code to use.

    The class expose a Font object property and the TextWidth/Height methods, so it's very easy to use:
    VB Code:
    1. Dim oFont As CTextSize
    2. Set oFont = New CTextSize
    3.  
    4. With oFont.Font
    5.     .Name = "Times New Roman"
    6.     .Size = 12
    7.     .Bold = True
    8. End With
    9. MsgBox oFont.TextWidth("Hello World!")
    The Font object is initilized as MS Sans Serif 8pt, which is used if you don't change it.

    EDIT: Corrected a possible memory leak.
    Attached Files Attached Files
    Last edited by Joacim Andersson; Jun 2nd, 2005 at 11:03 PM.

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