|
-
Mar 4th, 2002, 12:50 AM
#1
Thread Starter
Addicted Member
String Length
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.
-
Mar 4th, 2002, 03:24 AM
#2
Fanatic Member
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 )
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Mar 4th, 2002, 04:59 AM
#3
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)
-
Mar 5th, 2002, 08:38 AM
#4
sz contains the size in pixels, so screen.twipsperpixel would be ok.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|