The MASM Forum Archive 2004 to 2012
Welcome, Guest. Please login or register.
June 04, 2023, 11:48:02 AM

Login with username, password and session length
Search:     Advanced search
128553 Posts in 15254 Topics by 684 Members
Latest Member: mottt
* Home Help Search Login Register
+  The MASM Forum Archive 2004 to 2012
|-+  General Forums
| |-+  The Campus
| | |-+  Changing font size in an EDIT control
« previous next »
Pages: 1 [2] Print
Author Topic: Changing font size in an EDIT control  (Read 16348 times)
dedndave
Member
*****
Posts: 12523


Re: Changing font size in an EDIT control
« Reply #15 on: May 10, 2012, 01:15:50 PM »

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
Code:
        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   Tongue
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
Logged
Ryan
New Member
*
Gender: Male
Posts: 15


Re: Changing font size in an EDIT control
« Reply #16 on: May 11, 2012, 11:34:25 PM »

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.

Code:
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?
Logged
dedndave
Member
*****
Posts: 12523


Re: Changing font size in an EDIT control
« Reply #17 on: May 12, 2012, 12:02:23 AM »

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

but - try this, anyways
Code:
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   Tongue
API calls may (and usually do) trash EAX, ECX, and EDX

so - this would also work
Code:
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
Logged
Ryan
New Member
*
Gender: Male
Posts: 15


Re: Changing font size in an EDIT control
« Reply #18 on: May 12, 2012, 12:08:02 AM »

Ah.  That makes sense.  Thank you!
Logged
Pages: 1 [2] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP The MASM Forum Archive 2004 to 2012 | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!