How do you get a strings width in pixels? like textwidth method on a picturebox, but with out a picturebox or form, and in pixels.
Printable View
How do you get a strings width in pixels? like textwidth method on a picturebox, but with out a picturebox or form, and in pixels.
I can only give you some hints: create a DC using CreateCompatibleDC, use SelectObject to select the font you want to measure (create the font with CreateFont or CreateFontIndirect), use DrawText with the DT_CALCRECT flag to calculate the size of a string in pixels, use the return value of the first SelectObject in another call to SelectObject, and use that return value in a call to DeleteObject (SelectObject returns the previous object, in this case our font) then use DeleteDC to release the device context...
Don't know the exact way to do it in VB though, but at least you've got a starting point now :)
Oh, and try to keep de CreateCompatibleDC calls to a minimum, if you want to measure another string using the same font (or even another one) reuse the old DC, it will save you some execution time (if you need it for a game :))
two ways, use the printer textwidth (but don't actually print anything)...
or: you can use the API function GetTextExtentPoint32, but you need a DC first:
Call GetTextExtentPoint32(hDC, Text, Len(Text), sz)
TextLength = sz.cx
I think you need to multiply it by 16 or something (maybe screen.twipsperpixelx)
:confused:
sz contains the size in pixels, so screen.twipsperpixel would be ok.