How do you change the font of a static? I know you can change the fon't of the entire dialog, but I can't see how to change the font of just one static. This would be helpful in making a big Title at the top of the Dialog or something. :bg
Send WM_SETFONT to it. Make sure the last parameter is 1 to redraw the control immediately.
invoke SendMessage, hStatic, WM_SETFONT, hFont, 1
Cheers,
Zooba :U
Quote from: zooba on July 18, 2006, 11:21:53 PM
Send WM_SETFONT to it. Make sure the last parameter is 1 to redraw the control immediately.
invoke SendMessage, hStatic, WM_SETFONT, hFont, 1
By the way, I used to pair this with "GetStockObject" function to get a handle to a font, but all of six fonts, provided by "GetStockObject" seem like the same old "System" to me. And I couldn't even get a fixed-pitch font with it. Did I misunderstood something?
akelanuk,
Trying to make sense of these APIs can be very difficult for me, also, as I am not a C type guy. Anyway, you need to set the proper flags in the fnObject by using OR to set the various flags. The one to set to tell what type of font group you want is
Quote
ANSI_FIXED_FONT Windows fixed-pitch (monospace) system font.
ANSI_VAR_FONT Windows variable-pitch (proportional space) system font.
DEVICE_DEFAULT_FONT Windows NT only: Device-dependent font.
DEFAULT_GUI_FONT Windows 95 only: Default font for user interface objects such as menus and dialog boxes.
OEM_FIXED_FONT Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font.
SYSTEM_FONT System font. By default, Windows uses the system font to draw menus, dialog box controls, and text. In Windows versions 3.0 and later, the system font is a proportionally spaced font; earlier versions of Windows used a monospace system font.
SYSTEM_FIXED_FONT Fixed-pitch (monospace) system font used in Windows versions earlier than 3.0. This stock object is provided for compatibility with earlier versions of Windows.
If you do not set one of these flags, SYSTEM_FONT is assumed and is all that will be available. If you wish to use more than one font group, use OR as I said before:
ANSI_FIXED_FONT OR ANSI_VAR_FONT OR DEFAULT_GUI_FONT OR OEM_FIXED_FONT OR SYSTEM_FONT for example, would make all the fonts available to you.
If you intend to change the font used by menus, dialog boxes, and other user interface objects, etc. make sure you include DEFAULT_GUI_FONT
Most people will use GetStockObject to select a BRUSH and overlook the font selection capablities it has so to select a brush and set available fonts, use:
Quote
invoke GetStockObject, WHITE_BRUSH OR ANSI_FIXED_FONT OR ANSI_VAR_FONT OR DEFAULT_GUI_FONT OR \
OEM_FIXED_FONT OR SYSTEM_FONT
If you have additional questions I or someone else will help
Paul
here's what I do:
in WM_CREATE for your main window
create a font (you get an hfont, which can be used by
any control - so not 1 font per control(!))
create your static(s), button(s) etc.
use SendMessage to set their font e.g.
INVOKE SendMessage,hstatic,WM_SETFONT,hfont,TRUE
I usually use Tahoma, and create it thus:
sub eax,eax
INVOKE CreateFont,-11,eax,eax,eax,eax,eax,eax,eax,eax,\
eax,eax,eax,eax,OFFSET sTahoma
I don't really understand some of the parameters, but this seems
to work for my windows...
I use CreateFontIndirect after filling in the LOGFONT Structure
;---------- [Create the Edit Font] ----------
invoke lstrcpy, addr lf.lfFaceName, addr FontName
mov lf.lfHeight, -12
mov lf.lfWeight, 600
invoke CreateFontIndirect, addr lf
mov hFontE, eax ; Edit font
;---------- [Create the Static Font] --------
mov lf.lfHeight, -10
mov lf.lfWeight, 500
invoke CreateFontIndirect, addr lf
mov hFontS, eax ; Static font
If you want to use a bold effect, you need the lfWeight to be set accordingly, 400=normal, 700=bold
Values should be in increments of 100. If this value is not set in the structure, 400 is assumed.
lf LOGFONT <?>
LOGFONT <-10,0,0,0,600,FALSE,FALSE,FALSE,0,0,0,0,0,'Tahoma'>
The LOGFONT structure looks like this:
LOGFONTA STRUCT
lfHeight DWORD ?
lfWidth DWORD ?
lfEscapement DWORD ?
lfOrientation DWORD ?
lfWeight DWORD ?
lfItalic BYTE ?
lfUnderline BYTE ?
lfStrikeOut BYTE ?
lfCharSet BYTE ?
lfOutPrecision BYTE ?
lfClipPrecision BYTE ?
lfQuality BYTE ?
lfPitchAndFamily BYTE ?
lfFaceName BYTE LF_FACESIZE dup(?)
LOGFONTA ENDS
LOGFONT equ <LOGFONTA>
lfHeight is used to set the pointsize and works like this,
-10=8pt., -12=9pt., -13=10pt., -15=11pt., etc.
Don't forget to delete the font objects when you are done in the WM_CLOSE section add the following:
invoke DeleteObject, hFontE
invoke DeleteObject, hFontS
Paul
Quote from: PBrennick on July 19, 2006, 02:38:19 PM
If you do not set one of these flags, SYSTEM_FONT is assumed and is all that will be available. If you wish to use more than one font group, use OR as I said before:
ANSI_FIXED_FONT OR ANSI_VAR_FONT OR DEFAULT_GUI_FONT OR OEM_FIXED_FONT OR SYSTEM_FONT for example, would make all the fonts available to you.
If you intend to change the font used by menus, dialog boxes, and other user interface objects, etc. make sure you include DEFAULT_GUI_FONT
Most people will use GetStockObject to select a BRUSH and overlook the font selection capablities it has so to select a brush and set available fonts, use:
invoke GetStockObject, WHITE_BRUSH OR ANSI_FIXED_FONT OR ANSI_VAR_FONT OR DEFAULT_GUI_FONT OR \
OEM_FIXED_FONT OR SYSTEM_FONT
You can't actually OR these together. Replacing the equates in your code above looks like this:
invoke GetStockObject, 0 OR 11 OR 12 OR 17 OR 10 OR 13
MSDN also specifies that fnObject "can be
one of the following objects" (emphasis added)
(You may have been thinking of the fdwQuality and fdwPitchAndFamily fields in CreateFont, which behave this way)
I have found that DEFAULT_GUI_FONT returns MS Sans Serif regardless of the OS version (2K+ (IIRC) are supposed to use Tahoma, Vista has a new one). Attempting to load a font named "MS Shell Dlg 2" will get Tahoma or the user specified font. If that doesn't work use GetStockObject.
Cheers,
Zooba :U
Sorry, That part was untested and that limitation was not clear to me as the method I explained is pretty standard in APIs. I wonder if successive calls can solve that limitation.
There has to be a way to select a brush and a font or else it is really pointless to use it at all. I apologize for being unable to test this myself, to much on my plate right now. Still, I try to help whenever and wherever I can.
Paul
I've just found it. Windows will send a WM_CTLCOLORSTATIC message to the main window asking for a brush. The brush is not destroyed by the system, so it is best to create a brush at the same time as the control, store it somewhere, return it when asked for, and destroy it at the same time as the control.
Note that an edit control which is disabled or locked will send this message rather than a WM_CTLCOLOREDIT message. lParam contains the handle to the control for which the brush will be used, so check that first.
To summarise:
Send a WM_SETFONT to the static control to set the font.
Respond to a WM_CTLCOLORSTATIC message to set the colour.
Cheers,
Zooba :U
Thank you zooba for summing it up. Once again, your help makes everything that much easier. And thanks everyone else for your contributions. :bg :U
I am having a problem with this. I have a bunch of static controls on a dialog. I want to use a bold font, I have tried CreateFont & CreateFontIndirect and get a bold font but no matter what params I give it I get the same result, same font it is boldface but it is too tall. Even switching the font names I still get the same font. I have some program I found that will let you select a font and give you the params for a LOGFONT sruct, I even tried filling it out manually
lf LOGFONT <-11,0,0,0,700,0,0,0,0,3,2,1,34,"Microsoft Sans Serif">
lf LOGFONT <0,0,0,0,0,0,0,0,0,0,0,0,0,"Microsoft Sans Serif">
Dosent matter I Get This instead of This
Anybody have any Ideas of what I am screwing up?
Dustyh1981
Read the CreateFont (http://msdn2.microsoft.com/en-us/library/ms534214(VS.85).aspx) MSDN page, especially the nHeight parameter. It explains the transformation you need to do to make the font appear the size you want.
Cheers,
Zooba :U
I appreciate the reply but I checked MSDN before I posted.... It does not matter what I put in the nHeight to. I get the same result. Also I can not change font names. I have tried every font available on my computer and it still shows the same font. Nothing I do makes any diffrence, should not making a change to my LOGFONT make a diffrence in the result.
here is how I have it coded,
.data
FontBold LOGFONT <-11,0,0,0,700,0,0,0,0,3,2,1,34,'Microsoft Sans Serif'>; Nothing I change here makes any diffrence what so ever.
.code
invoke CreateFontIndirect,addr FontBold
mov hFont,eax
invoke SendDlgItemMessage,hWnd,IDC_STATIC_1,WM_SETFONT,0,hFont
have also tried it like this
sFont db 'Microsoft Sans Serif',00
invoke CreateFont,-11,0,0,0,700,0,0,0,0,3,2,1,34, addr sFont
I see now. You're creating the font fine (AFAICT), but using the WM_SETFONT message incorrectly.
Quote from: MSDNwParam
Handle to the font (HFONT). If this parameter is NULL, the control uses the default system font to draw text.
lParam
The low-order word of lParam specifies whether the control should be redrawn immediately upon setting the font. If this parameter is TRUE, the control redraws itself.
You're passing the font handle in lParam and passing NULL (0) in wParam. You should pass the font handle in wParam. lParam should be zero (FALSE) if the control is not yet visible (ie. you're setting the font in your initialisation) or one (TRUE) if the control is already visible (ie. you're changing the font while the program is running).
Cheers,
Zooba :U
Thank you. I feel kinda bad now, guess I need to open my eyes when I read and I would see stuff like that. I am just glad there are good ppl like yourself to help amatuers like myself. I have no formal knollege of programming of any kind, trying to self-educate. Anyway thanks again.
DrPepUr