Dynamically changing the font (size) of controls and dialog boxes in VC)

Source: Internet
Author: User
1. The font setting of the VC dialog box is valid for all controls. First, use CreateFont to create a font object, and then call the SetFont of the control.

Example:
1. Change the static style ID, for example, IDC_STATIC1
2. Add an Edit control and create an associated control m_editControl.
3. Add the following code to OnInitDialog:
CFont * f;

F = new CFont;

F-> CreateFont (16, // nHeight

0, // nWidth

0, // nEscapement

0, // nOrientation

FW_BOLD, // nWeight

TRUE, // bItalic

FALSE, // bUnderline

0, // cStrikeOut

ANSI_CHARSET, // nCharSet

OUT_DEFAULT_PRECIS, // nOutPrecision

CLIP_DEFAULT_PRECIS, // nClipPrecision

DEFAULT_QUALITY, // nQuality

DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily

_ T ("Arial"); // lpszFac


GetDlgItem (IDC_STATIC1)-> SetFont (f); note that we use the CFont pointer instead of the common CFont local variable. In non-MFC programs, first, use CreateFont to create a font handle, and then use SendMessage to send the control WM_SETFONT message. Assign the font handle to the control.

Instance download: http://www.china-askpro.com/download/CtrlFont.zip

2. However, the font size of the entire dialog box or window does not work with the SetFont () function of the dialog box or window. you can traverse the settings of each control during initialization, but here is another simple method to use the callback function:
: Call the system API: EnumChildWindows (). Pass in the callback function and re-define the font. (the first parameter is not required. It would have been used)
)
1) in the document view structure, call: EnumChildWindows () in CMainFrame: OnCreate (). Change the font of all windows and subwindows.
2) Call: EnumChildWindows () in OnInitDialog () in the dialog box. Change all controls in the dialog box.

The callback function is as follows:

// LParam is a pointer to CFont object
BOOL _ stdcall SetChildFont (HWND hwnd, LPARAM lparam)
{
CFont * pFont = (CFont *) lparam;
CWnd * pWnd = CWnd: FromHandle (hwnd );
PWnd-> SetFont (pFont );
Return TRUE;
}

Use 1:

BOOL CAboutDlg: OnInitDialog ()

{
CDialog: OnInitDialog ();

// TODO: Add extra initialization here
: EnumChildWindows (m_hWnd,: SetChildFont, (LPARAM) g_Font.GetFont ());

Return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages shold return FALSE
}


Use 2:

Int CMainFrame: OnCreate (maid)
{
If (CFrameWnd: OnCreate (lpCreateStruct) =-1)
Return-1;

If (! M_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |
! M_wndToolBar.LoadToolBar (IDR_MAINFRAME ))
{
TRACE0 ("Failed to create toolbar \ n ");
Return-1; // fail to create
}

If (! M_wndStatusBar.Create (this) |
! M_wndStatusBar.SetIndicators (indicators,
Sizeof (indicators)/sizeof (UINT )))
{
TRACE0 ("Failed to create status bar \ n ");
Return-1; // fail to create
}

M_wndToolBar.EnableDocking (CBRS_ALIGN_ANY );
EnableDocking (CBRS_ALIGN_ANY );
DockControlBar (& m_wndToolBar );

: EnumChildWindows (m_hWnd,: SetChildFont, (LPARAM) g_Font.GetFont ());

Return 0;
}

(Very easy to use. Unlike the spam setfont () in mfc, there is no response when setting the dialog box !)

Instance download: http://www.codeproject.com/gdi/SetFont/SetFont_demo.zip

3. How can we implement it in mfc? When the system font increases, the font on the dialog box also increases accordingly? (Thank you very much)

// IconFont
LOGFONT logFont;
Int size = sizeof (LOGFONT );
Bool isGood = SystemParametersInfo (SPI_GETICONTITLELOGFONT, size, & logFont, 0 );
If (isGood = true)
{
CFont * f;
F = new CFont;
Const LOGFONT * pFont = new LOGFONT (logFont );
F-> CreateFontIndirectW (pFont );
//: EnumChildWindows (m_hWnd,: SetChildFont, (LPARAM) f );
}

// Other Font
NONCLIENTMETRICS ncm = new NONCLIENTMETRICS ();
Bool isGood = SystemParametersInfo (SPI_GETNONCLIENTMETRICS, sizeof (NONCLIENTMETRICS), ref ncm, 0 );
If (isGood = true)
{
LOGFONT logFont2;
// LogFont2 = ncm. lfntCaptionFont); // CaptionFont // logFont2 = ncm. lfntSMCaptionFont; // CaptionFont_Small
// LogFont2 = ncm. lfntMenuFont; // MenuFont
// LogFont2 = ncm. lfntStatusFont; // StatusFont
LogFont2 = ncm. lfntMessageFont; // MessageFont
CFont * f;
F = new CFont;
Const LOGFONT * pFont = new LOGFONT (logFont2 );
F-> CreateFontIndirectW (pFont); //: EnumChildWindows (m_hWnd,: SetChildFont, (LPARAM) f );
}

The above is to obtain the system font size and then call the second method above. All fonts on the form will change with the system font size!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.