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
Printable View
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
Where did you see this "MeasureString" used? .NET? I believe it is a .NET GDI+ only function.
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.
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
Thanks, I'll give it a shot.
Np, I had it working on setting a combos width depending on its largest string length. :D
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.
:D
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.
Ooh Thanks