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

ToutEnMasm

#15
There is  windows common dialog box who show you the list of fonts existing in your system.
Choose one and the dialog return you a structure with which you can recreate the font.
Coudn't be more simple.
here is  sample:
Quote
   CreeFont PROC
         Local  retour:DWORD
         Local  logfont:LOGFONT

   mov logfont.lfHeight,-19
   mov logfont.lfWidth,0
   mov logfont.lfEscapement,0
   mov logfont.lfOrientation,0
   mov logfont.lfWeight,400
   mov logfont.lfItalic,0
   mov logfont.lfUnderline,0
   mov logfont.lfStrikeOut,0
   mov logfont.lfCharSet,0
   mov logfont.lfOutPrecision,3
   mov logfont.lfClipPrecision,2
   mov logfont.lfQuality,1   ;0 DEFAULT_QUALITY ,1 DRAFT_QUALITY,2 PROOF_QUALITY
   mov logfont.lfPitchAndFamily,34
   jmp overtext
   NamedFont db "Segoe UI",0      ;LOGFONT
   overtext:
   invoke lstrcpy,addr logfont.lfFaceName,addr NamedFont
   INVOKE     CreateFontIndirect, addr logfont      ;Modifie    logfont:LOGFONT
   mov     retour, eax   ;retourne Hfonte

   ;invoke SendMessage,Hcontrole,WM_SETFONT,Hfonte,FALSE
   FindeCreeFont:
   mov eax,retour
   ret
CreeFont endp


raleeper

Quote from: ToutEnMasm on September 01, 2011, 12:30:47 PM
There is  windows common dialog box who show you the list of fonts existing in your system.
Choose one and the dialog return you a structure with which you can recreate the font.
Coudn't be more simple.
here is  sample:
Quote
   CreeFont PROC
         Local  retour:DWORD
         Local  logfont:LOGFONT

   mov logfont.lfHeight,-19
   mov logfont.lfWidth,0
   mov logfont.lfEscapement,0
   mov logfont.lfOrientation,0
   mov logfont.lfWeight,400
   mov logfont.lfItalic,0
   mov logfont.lfUnderline,0
   mov logfont.lfStrikeOut,0
   mov logfont.lfCharSet,0
   mov logfont.lfOutPrecision,3
   mov logfont.lfClipPrecision,2
   mov logfont.lfQuality,1   ;0 DEFAULT_QUALITY ,1 DRAFT_QUALITY,2 PROOF_QUALITY
   mov logfont.lfPitchAndFamily,34
   jmp overtext
   NamedFont db "Segoe UI",0      ;LOGFONT
   overtext:
   invoke lstrcpy,addr logfont.lfFaceName,addr NamedFont
   INVOKE     CreateFontIndirect, addr logfont      ;Modifie    logfont:LOGFONT
   mov     retour, eax   ;retourne Hfonte

   ;invoke SendMessage,Hcontrole,WM_SETFONT,Hfonte,FALSE
   FindeCreeFont:
   mov eax,retour
   ret
CreeFont endp


What is that windows common dialog box, and what are Hcontrole and Hfonte?

Would that be

EnumFontFamiliesEx hdc,logfont,fenproc,0,0

with logfont.lfCharSet = DEFAULT_CHARSET, and fenproc the callback?

Thanks, Robert

Gunner

If you are going to create a few variations of the same font... Normal, bold, bigger, smaller, then all you have to do is zero out the logfont struc, fill in the info, create the font, save the handle, next font JUST change what needs to be changed create font save handle repeat for each variation. 
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

#18
the EnumFontFamiliesEx function looks at only 3 members of the LOGFONT structure
lfCharSet, lfPitchAndFamily, lfFaceName

if you set them all to 0, it looks through all the fonts (at least, all the fonts with a default char-set)
each font is enumerated only once
i.e., Arial will only show up once, even though there are bold, italic, underlined, etc versions of it

if you set one or more members to some non-zero value, it enumerates only matching fonts

as it happens, i was playing with this function the other day (see attachment)
i called EnumFontFamiliesEx with all members set to 0
in the callback proc for that, i coded another call to EnumFontFamiliesEx
this time, i copied the lfFaceName from the LOGFONT passed to me by the function into another LOGFONT structure
then, i set the lfCharSet and lfPitchAndFamily to 0
the second call enumerates the font family

if you are looking for a font that the user has selected through "appearances" settings, use
        LOCAL   ncm:NONCLIENTMETRICS

        mov     eax,sizeof NONCLIENTMETRICS
        mov     ncm.cbSize,eax
        INVOKE  SystemParametersInfo,SPI_GETNONCLIENTMETRICS,eax,addr ncm,NULL


the NONCLIENTMETRICS structure has 5 LOGFONT members that you can use to create user-selected fonts
lfCaptionFont , lfSMCaptionFont, lfMenuFont, lfStatusFont,  lfMessageFont

then use CreateFontIndirect to create the appropriate font for what you are displaying
        INVOKE  CreateFontIndirect,addr ncm.lfMessageFont
        mov     hFont,eax          ;store the font handle

that makes it simple - you don't have to copy names or worry about whether or not the font exists
also, if the user has poor eyesight or has selected a special font for some other reason, you get what he/she wants
when you are done using the font, call DeleteObject to delete it

i am still learning about this stuff - lol
i think it may be a good idea to check the language to see if it will display English text
if not, get the user-selected size and use Tahoma   :P

if you want to see what Rob is talking about, MichaelW wrote a simple example...
http://www.masm32.com/board/index.php?topic=3835.msg28602#msg28602

ToutEnMasm

Quote
What is that windows common dialog box, and what are Hcontrole and Hfonte?

For the dialog box it is this one

Quote
   ;-----------------------------------------------------------------
   INVOKE     ChooseFont, addr cf   ; Invoke common ChooseFont dialog
   ;------------------------------------------------------------------

The line you refer with Hcontrole is in comment .To use the created handle of font (Hfonte) you need to send  message to the control.
You have just to copy the line comment,uncomment it and replace the Hcontrol by your own handle of control or window.



ToutEnMasm

I have  made a tool who generate this proc.
You can find it here:
/http://www.masm32.com/board/index.php?topic=8542.0
you can find it here \sdkrc7\examples\cherche.exe
USAGE:
put some text in it nd select it.
Used this menus:             "Autre recherches"--->"cherche fonte"

raleeper

Quote from: ToutEnMasm on September 01, 2011, 03:17:31 PM
Quote
What is that windows common dialog box, and what are Hcontrole and Hfonte?

For the dialog box it is this one

Quote
   ;-----------------------------------------------------------------
   INVOKE     ChooseFont, addr cf   ; Invoke common ChooseFont dialog
   ;------------------------------------------------------------------

The line you refer with Hcontrole is in comment .To use the created handle of font (Hfonte) you need to send  message to the control.
You have just to copy the line comment,uncomment it and replace the Hcontrol by your own handle of control or window.




Thank you, ToutEnMasm.  I see.  This will be very useful.  I've just started playing with ChooseFont and so far I don't see a way to select a character set, but I set it up hurriedly, so I probably made a mistake.  Anyway, this is great.
I haven't looked at the tool in your next post, but I expect it to be very useful also.

Thank you again.