Click to See Complete Forum and Search --> : String Length
KingDavid
Mar 3rd, 2002, 11:50 PM
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.
PsychoMark
Mar 4th, 2002, 02:24 AM
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 :))
Si_the_geek
Mar 4th, 2002, 03:59 AM
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:
CornedBee
Mar 5th, 2002, 07:38 AM
sz contains the size in pixels, so screen.twipsperpixel would be ok.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.