Results 1 to 9 of 9

Thread: [RESOLVED] MeasureString API

  1. #1

    Thread Starter
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Resolved [RESOLVED] MeasureString API

    Hi,

    I just searched for about 2 hours to no avail. Can anyone give me the declaration for the MeasureString API? I need to use it in VB6

    Thanks

    r0ach™
    Don't forget to rate the post

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MeasureString API

    Where did you see this "MeasureString" used? .NET? I believe it is a .NET GDI+ only function.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: MeasureString API

    Hmmmm. Not good. I've used it in .NET before. I just need to use an alternative to the Form.TextWidth() VB6 function since I'm working in a class, and don't want to create a form just for this purpose.

    r0ach™
    Don't forget to rate the post

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MeasureString API

    I pulled up an old project of mine where I got code for measuring a strings length using a couple of GDI APIs and put together this for you. Just place it in a module and call by passing the form's handle the text resides on and the text to be measured.


    Code:
    Option Explicit
    
    Private Type SIZE
        cx As Long
        cy As Long
    End Type
    
    Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, _
    ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) As Long
    Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Const ANSI_VAR_FONT = 12
    
    Public Function GetFontDialogUnits(ByVal Formhwnd As Long, ByVal sComboText As String) As Double
    '<08/08/2003 - VB/OUTLOOK GURU>
        Dim hFont As Long
        Dim hFontOrig As Long
        Dim dblWidth As Double
        Dim hdc As Long
        Dim sTemp As String
        Dim sz As SIZE
    
        hdc = GetDC(Formhwnd)
        hFont = GetStockObject(ANSI_VAR_FONT)
        hFontOrig = SelectObject(hdc, hFont&)
        GetTextExtentPoint32 hdc, sComboText, Len(sComboText), sz
        dblWidth = sz.cx
        '<RESET & DELETE>
        SelectObject hdc, hFontOrig
        DeleteObject (hFont)
        ReleaseDC Formhwnd, hdc
        GetFontDialogUnits = dblWidth
    End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: MeasureString API

    Thanks, I'll give it a shot.

    r0ach™
    Don't forget to rate the post

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MeasureString API

    Np, I had it working on setting a combos width depending on its largest string length.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: [RESOLVED] MeasureString API

    Haha. That's almost what I need to do. I'm using a ThirdParty grid control that works a bit like a spreadsheet. Our client wants the dropdownbox to be the width of the column, unless the textwidth exceeds that of the column width in which case it should stretch to the largest string length. I'm managing everything in a class, so I don't want to use/depend on any external forms for this.

    r0ach™
    Don't forget to rate the post

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] MeasureString API



    If you pass the handle of the third party controls window then it should be accurate if the font unsed in the third party control is the same as whats used on the form.

    One thing I didnt add in but is easy is that when you have more then 8 items in a cbo it adds the scrollbar which is about 23 pixels in width. You may want to add an extra 23 to the dlb results if the items in it are greater then 8 or dynamically read the dropdown displayable items count.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: [RESOLVED] MeasureString API

    Ooh Thanks

    r0ach™
    Don't forget to rate the post

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