I Supposed to set font size for all controls in dialog at runtime. I tried SetFont(pFont) in OnInitDialog function. But the font size is not changing. Please Suggest me. Thanks.
Printable View
I Supposed to set font size for all controls in dialog at runtime. I tried SetFont(pFont) in OnInitDialog function. But the font size is not changing. Please Suggest me. Thanks.
Hi,
Take a look at this : MFC General: How do I change the font of a control?
Regards,
Laitinen
In addition to my first answer. What is pFont? Is is a member variable of your class? If not it will go out of scope as soon as OnInitDialog finishes and therefore would not have any effect.Quote:
Originally Posted by sabitha
But the first code snippet in the faq should do what you want!
Regards,
Laitinen
pFont is CFont pointer object.
nTextFontHeight = 20;
m_cFontDialog.CreateFont(nTextFontHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, NULL, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, _T("MS Shell Dlg"));
CFont *pFont = &m_cFontDialog;
SetFont(pFont);
Change your code to this;
Code://In header File
CFont m_Font;
//In OnInitDialog
m_Font.CreateFont(nTextFontHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, NULL, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, _T("MS Shell Dlg"));
//Set the font on a button with ID = ID_BUTTON1
GetDlgItem(ID_BUTTON1)->SetFont(&m_Font);
I should not set font size for individual controls. I have to do this by setting font size to dialog.
Setting the dialog's font size has no effect on the font size of its controls. You need to set the font for each of them explicitly. Obviously doing this by hand is tedious an error prone.
What you could do is use the EnumChildWindows functions to enumerate all the children of the dialog and have the callback function set the font.
Code:
BOOL CALLBACK EnumChildProc( HWND hwnd,LPARAM lParam)
{
CWnd* wnd = CWnd::FromHandle( hwnd);
wnd->SetFont( (CFont*) lParam);
return TRUE;
}
BOOL CYourDialog::OnInitDialog( )
{
CDialog::OnInitDialog();
...
// create and setup the font
CFont font;
...
EnumChildWindows( GetSafeHwnd(), EnumChildProc, (long) &font);
return TRUE;
}
Thank you. It is working fine.
Hi
What if I'm not using MFC??
I'm using C++ to write my GUI.
Thanks
You may also use a loop for iterating through all of the controls, rather than calling EnumChildWindows():
In OnInitDialog() write:
where m_font is a member of the dialog which gets not out of scope as long the dialog exists.Code:for(CWnd *pControl=GetWindow(GW_CHILD);pControl;pControl=pControl->GetWindow(GW_HWNDNEXT))
pControl->SetFont(&m_font);
Did you try to use SendMessageToDescendants instead?