Default font size for all controls in dialog
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Default font size for all controls in dialog

  1. #1
    Join Date
    May 2006
    Posts
    28

    Default font size for all controls in dialog

    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.
    Last edited by sabitha; February 27th, 2007 at 05:44 AM.

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

  3. #3
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Default font size for all controls in dialog

    Quote Originally Posted by sabitha
    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.
    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.

    But the first code snippet in the faq should do what you want!

    Regards,

    Laitinen

  4. #4
    Join Date
    May 2006
    Posts
    28

    Re: Default font size for all controls in dialog

    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);

  5. #5
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Default font size for all controls in dialog

    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);

  6. #6
    Join Date
    May 2006
    Posts
    28

    Re: Default font size for all controls in dialog

    I should not set font size for individual controls. I have to do this by setting font size to dialog.

  7. #7
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Default font size for all controls in 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;
    }
    Har Har

  8. #8
    Join Date
    May 2006
    Posts
    28

    Re: Default font size for all controls in dialog

    Thank you. It is working fine.

  9. #9
    Join Date
    Mar 2007
    Posts
    20

    Re: Default font size for all controls in dialog

    Hi

    What if I'm not using MFC??

    I'm using C++ to write my GUI.

    Thanks

  10. #10
    Join Date
    Jul 2011
    Posts
    2

    Re: Default font size for all controls in dialog

    You may also use a loop for iterating through all of the controls, rather than calling EnumChildWindows():

    In OnInitDialog() write:
    Code:
      for(CWnd *pControl=GetWindow(GW_CHILD);pControl;pControl=pControl->GetWindow(GW_HWNDNEXT))
        pControl->SetFont(&m_font);
    where m_font is a member of the dialog which gets not out of scope as long the dialog exists.

  11. #11
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,381

    Re: Default font size for all controls in dialog

    Did you try to use SendMessageToDescendants instead?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center