Re: Getting character widths in Win32--for centering and such

new topic     » goto parent     » topic index » view thread      » older message » newer message

--=====================_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==_--

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu