News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Changing font size in an EDIT control

Started by Ryan, May 10, 2012, 01:19:53 AM

Previous topic - Next topic

dedndave

you can use GetDeviceCaps to get the "pixels per logical inch" setting
then use a little formula to convert between point size and height
normally, the LOGPIXELSY value is 96 dpi - but don't count on it
        LOCAL   hdcDesktop  :HDC
        LOCAL   uLogPixelsY :UINT

        INVOKE  GetDC,HWND_DESKTOP
        mov     hdcDesktop,eax
        INVOKE  GetDeviceCaps,eax,LOGPIXELSY
        mov     uLogPixelsY,eax
        INVOKE  ReleaseDC,HWND_DESKTOP,hdcDesktop


once you have the "pixels per logical inch" value...
lfHeight = -(PointSize * uLogPixelsY) / 72

i find it easier to stick a constant into the lfHeight member, like 8   :P
if i want it larger, i stick a larger number in there - lol
in the end, what you are after is a font that will allow your text to fit in a box
the other approach is to use GetTextExtentPoint32 to measure the font and make your boxes fit the text

Ryan

Per dedndave and jj's suggestion, I declared global variables in the data? section to hold the font handles that I use in the program, instead of creating a new font for each change.


InitFont proc fontSize:dword
    local hdcDesktop:HDC
   
    invoke GetDC,0
    mov hdcDesktop,eax
    invoke GetDeviceCaps, eax, LOGPIXELSY
    invoke MulDiv,fontSize,eax,72
    neg eax
    invoke CreateFont,eax,0,0,0,0,0,0,0,0,0,0,0,0,0
    invoke ReleaseDC,0,hdcDesktop
    ret
InitFont endp


This gets executed once at the beginning for each font size I use.  I took into account dedndave's last example of invoking ReleaseDC at the end of the proc.  Releasing the DC appears to void my fonts that I create.  If I comment out the invoking of ReleaseDC, things work as expected.

How crucial is it that I release the DC?

dedndave

hmmmm....
something is amiss   :P
the only reason you need the DC is for GetDeviceCaps
releasing it should have no affect on the font handles

but - try this, anyways
InitFont proc fontSize:dword
    local hdcDesktop:HDC
   
    invoke GetDC,0
    mov hdcDesktop,eax
    invoke GetDeviceCaps, eax, LOGPIXELSY
    push eax
    invoke ReleaseDC,0,hdcDesktop
    pop eax
    invoke MulDiv,fontSize,eax,72
    neg eax
    invoke CreateFont,eax,0,0,0,0,0,0,0,0,0,0,0,0,0
    ret
InitFont endp


EDIT: i see what was happening
the font handle you returned in EAX was being destroyed by the ReleaseDC call   :P
API calls may (and usually do) trash EAX, ECX, and EDX

so - this would also work
InitFont proc fontSize:dword
    local hdcDesktop:HDC
   
    invoke GetDC,0
    mov hdcDesktop,eax
    invoke GetDeviceCaps, eax, LOGPIXELSY
    invoke MulDiv,fontSize,eax,72
    neg eax
    invoke CreateFont,eax,0,0,0,0,0,0,0,0,0,0,0,0,0
    push eax
    invoke ReleaseDC,0,hdcDesktop
    pop eax
    ret
InitFont endp

Ryan

Ah.  That makes sense.  Thank you!