News:

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

Set font of window using code, not resource files

Started by zemtex, July 23, 2011, 09:45:51 PM

Previous topic - Next topic

zemtex

Do I have to use CreateFont and WM_SETFONT to set a default font on my window and all controls within it?

Can't I load a preset font that is commonly used in windows or do I have to run through the rather large CreateFont function?
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

Gunner

You have to set the font for each control and window otherwise windows will use the default system font...

Easy way to do it... create a callback like so:
SetControlFonts proc hWin:DWORD, lParam:DWORD
invoke SendMessage, hWin, WM_SETFONT, HANDLE TO YOUR FONT HERE, FALSE

mov eax, TRUE
ret
SetControlFonts endp


next, in each WM_CREATE AFTER all the controls have been created, call
; ##### set all fonts
invoke EnumChildWindows, hWin, addr SetControlFonts, NULL


now if you want underline, bold, diff size, you will have to set those controls after your call to enumchildwindows
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

zemtex

#2
Thanks. EnumChildWindow is a useful function  :U but as far as I know WM_SETFONT sets font for all controls in the dialog automatically, I don't think it is neccesary to use EnumChildWindow

(I am merely reading ms docs, I haven't actually checked if it is so, but I appreciate your help, you are probably right, I merely speculate here based on an inconsistent ms documentation  :U
I should proibably just trust your words and go for it, but I have a habit of always reading documentation before I try to do something, even with samples)
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

dedndave

that's a nice little trick, Rob....
....now that i understand why it's so useful   :P

:U

i passed hFont to the EnumChildProc as lParam   :bg

SetControlFonts PROTO :HWND,:LPARAM
;
;

;WM_INITDIALOG

        mov     eax,sizeof NONCLIENTMETRICS
        mov     ncm.cbSize,eax
        INVOKE  SystemParametersInfo,SPI_GETNONCLIENTMETRICS,eax,addr ncm,NULL
        INVOKE  CreateFontIndirect,addr ncm.lfMessageFont
        mov     hDlgFont,eax
        INVOKE  EnumChildWindows,hWnd,SetControlFonts,eax
;
;
;********************************************************************

        OPTION  PROLOGUE:None
        OPTION  EPILOGUE:None

SetControlFonts PROC hWnd:HWND,lParam:LPARAM

        push    TRUE
        INVOKE  SendMessage,[esp+20],WM_SETFONT,[esp+16],0
        pop     eax
        ret     8

SetControlFonts ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

;********************************************************************

dedndave

in fact, i may pass a pointer to the stack frame and use it to set font, text, control sizes - the whole ball of wax - lol

Gunner

Yeah, I can see many uses for it.  Makes it real easy to use the whole ball of wax!  :toothy
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

i have another little problem
i would like to store a stack frame pointer from the creating routine for access in DlgProc
i am thinking of creating one or two "dummy" controls to store the address   :bg
i can use some of the ID bits and some of the style bits
i am trying to avoid using any global variables

it may not be necessary - we'll see how it goes

qWord

Quote from: dedndave on August 28, 2011, 01:48:33 AMi am trying to avoid using any global variables
use the class and window extra memory (WNDCLASSEX.cbClsExtra/cbWndExtra)
FPU in a trice: SmplMath
It's that simple!

Gunner

Why use "dummy" controls?  You don't have to create un-needed controls.  Every control has "extra" memory - GWL_USERDATA  Just pick some controls to store the addresses and use SetWindowLong

invoke SetWindowLong, CONTROL_HANDLE, GWL_USERDATA, ADDRESS_HERE

then just use GetWindowLong to get that stored address.  Simple storage for addresses/pointers  :bg
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

ahhhhhh
thanks guys   :U

i wasn't liking the other method - it sounded really hoakie - lol
it hadn't dawned on me to use the extra memory of the dialog box - DOH !!!

Tedd

Quote from: zemtex on July 23, 2011, 09:45:51 PM
Can't I load a preset font that is commonly used in windows or do I have to run through the rather large CreateFont function?

invoke GetStockObject, DEFAULT_GUI_FONT
mov hFont,eax

No snowflake in an avalanche feels responsible.

baltoro

ZEMTEX,   
If you can get a copy of Charles Petzold's, Programming Windows, Fifth Edition, 1998, you will find it describes almost every aspect of Windows programming, clearly and comprehensively. It's written for C and C++ programmers, and, it comes with a CD that has numerous examples of windows programs,...both the source code and the compiled programs. If you have a version of Visual Studio, you can load the source files directly and compile immediately.
...It's,...like,...cooler than Dave,...almost,... :eek
Baltoro

raleeper

I'm probably missing something basic, but can't you just have a single LOGFONT structure and a single code block or procedure:

    invoke    CreateFontIndirect,logfont
    invoke    SelectObject, hdc, eax
    mov       [hfont], eax


and when you want change fonts, just write the new values into the structure and call or use the code?

I assume the handle remains valid after the structure used to create it is changed or destroyed.

Thanks, Robert


Tedd

Yes, once you have the font handle, the logfont structure is no longer needed - so you can reuse it as you wish.
If you're changing fonts, remember to delete the previous font first :wink
No snowflake in an avalanche feels responsible.

raleeper

Quote from: Tedd on September 01, 2011, 11:36:57 AM
Yes, once you have the font handle, the logfont structure is no longer needed - so you can reuse it as you wish.
If you're changing fonts, remember to delete the previous font first :wink

What happens if you don't delete it, but just select another into the device context, then perhaps a character later, select the first back in (using its handle, which you've saved)?

Another question:

Can you create all the fonts in winmain and save the handles globally, so you don't have to recreate them in the windows procedure?