1. Getting character widths in Win32--for centering and such
I am working on a progress bar routine thing for **WIN32**, and am
figuring out a way to find the length **in pixels** of a string to be
posted in the progress bar (63 %). I have looked through the Win32
API help file from Inprise (Borland), and I have tried 2 methods, both
not working (I think). First I tried in GDI32.DLL GetCharWidth32(),
and when all was said and done, I got numbers like 88 and 0 and 32.
It may be the type of memory I allocate()d, or the way I'm peek()ing
into memory. Anyway, I have all that I've done in a TEST.EXW file.
First, you need to make a <slight> modification to win32lib.ew (.15c)
-- winspool=linkDLL("winspool.dll")
--below is code added by me
global constant Length32=
linkProc(gdi32,"GetCharWidth32A", {C_POINTER,C_UINT,C_UINT,C_POINTER})
--below here is normal
Second, here is the .exw file I used to test this.
--TEST.EXW
without warning
include win32lib.ew
include machine.e
--the below window is used for a dummy for the getDC()
constant win=create(Window,"",0,0,0,10,10,0),
--allocate the buffer of 10 bytes
buffer=allocate(10)
--put into memory the widths for numbers 0(zero)-9
c_proc(Length32,{getDC(win),'0','9',buffer})
sequence w
--get from memory widths of the numbers 0(zero)-9
w=peek({buffer,10})
for x=1 to 10 do
--use the console window to display widths with
--a space between them.
print(1,w[x])--show the width
puts(1," ")--add a space
end for
include get.e --for wait_key()
if wait_key() then end if --end program at keypress
**I'm sorry if the program doesn't get shown right. The editor I'm
using this at YAHOO is showing me this in a monospaced font. you may
see it kinda wavy.
Thanks,
Mike Hurley
_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com
2. Re: Getting character widths in Win32--for centering and such
- Posted by Daniel Berstein <daber at PAIR.COM>
Mar 14, 1999
-
Last edited Mar 15, 1999
--=====================_921476570==_
At 06:43 PM 14-03-1999 , you wrote:
>I am working on a progress bar routine thing for **WIN32**, and am
>figuring out a way to find the length **in pixels** of a string to be
>posted in the progress bar (63 %). I have looked through the Win32
>API help file from Inprise (Borland), and I have tried 2 methods, both
>not working (I think). First I tried in GDI32.DLL GetCharWidth32(),
>and when all was said and done, I got numbers like 88 and 0 and 32.
>It may be the type of memory I allocate()d, or the way I'm peek()ing
>into memory. Anyway, I have all that I've done in a TEST.EXW file.
I posted some time ago a routine for getting the size (height and width) a
string will use when rendered on a given DC (using the current seleted
font). I improved it a bit to make the conversion from logical to device
device units. The final units depends on the actual DC (600dpi printer or
72dpi screen?) and the current mapping mode of the DC. For screen DC's
you'll get it in pixels, unless you're using a wierd mapping mode.
Function returns -1 on error or a {width, heigth} sequence on success.
To use it with win32lib use something like:
include win32lib.ew
include textsize.ew
atom hdc
object size
hdc = getDC(YourControlID)
size = GetTextSize(hdc, "MyString")
if atom(size) then
-- Oops.. error!
else
-- size[1] = text width
-- size[2] = text height
end if
---------------------------------
--=====================_921476570==_
include dll.e
include machine.e
global function GetTextSize(atom hdc, sequence text)
atom gdi32, dllGetTextExtentPoint32, dllLPtoDP
atom pstring, psize
integer width, height, result
gdi32 = open_dll("gdi32.dll")
dllGetTextExtentPoint32 = define_c_func(gdi32, "GetTextExtentPoint32A",
{C_INT, C_POINTER, C_INT, C_POINTER}, C_INT)
dllLPtoDP = define_c_func(gdi32, "LPtoDP", {C_INT, C_POINTER, C_INT}, C_INT)
pstring = allocate_string(text)
psize = allocate(8)
result = c_func(dllGetTextExtentPoint32, {hdc, pstring, length(text),
psize})
free(pstring)
if not result then
free(psize)
return -1
end if
result = c_func(dllLPtoDP, {hdc, psize, 1})
width = peek4s(psize)
height = peek4s(psize+4)
free(psize)
if not result then
return -1
end if
return {width, height}
end function
--=====================_921476570==_
Regards,
Daniel Berstein
[ daber at pair.com ]
--=====================_921476570==_--