The method of font setting in VC + + _c language

Source: Internet
Author: User
Tags int size set background

VC + + static text font change
The window has 2 functions that are related to the font:
Cwnd::getfont () and SetFont (cfont*, BOOL);
1) cfont* Pfont = m_static. GetFont ();

2) LogFont LogFont;
Pfont->getlogfont (&logfont);

3 to LogFont Direct manipulation modify the font options inside
//such as Logfont.lfunderline = 1; set underline
logfont.lfheight=30; Font size settings
strcpy (Logfont.lffacename, "italics _gb2312"); Font settings

4) Pfont->detach ();
The purpose of step fourth is to disassociate the hfont contained in the Pfont, or else Pfont cannot call the immediately create function.

5) Pfont->createfontindirect (&logfont);
M_static. SetFont (Pfont);

6) Pfont->detach ();
The hfont that had to be loaded in the Pfont had to be lifted again because the 5th step had already assigned Hfont to M_static. Pfont's task has been completed, should not hold hfont resources, it can not be destroyed hfont, because M_static is using this hfont, so must be detach () to disassociate.

The change of font color in VC + +
In the OnCtlColor function, the following code:

Copy Code code as follows:

Hbrush Cdlg_signin::onctlcolor (cdc* PDC, cwnd* pwnd, UINT nCtlColor)
{
Hbrush HBR = Cdialogex::onctlcolor (PDC, pwnd, nCtlColor);
Todo:change any attributes to the DC here
if (nCtlColor = = ctlcolor_static)
{
if (pwnd->getdlgctrlid () = = Idc_regard)
{
Pdc->settextcolor (RGB (255,0,0));
Pdc->setbkcolor (RGB (251, 247, 200));//Set Text background color
Pdc->setbkmode (transparent);/Set Background transparent
}
}
Todo:return a different brush if the default is not desired
return HBR;
}

the macros for other controls are defined as:
Ctlcolor_btn Button control
Ctlcolor_dlg dialog box
Ctlcolor_edit edit Box
Ctlcolor_listbox List control
Ctlcolor_msgbox Message Control
Ctlcolor_scrollbar scroll bar control
Ctlcolor_static Static controls

VC dynamically change the Control and dialog box fonts.
1
VC dialog box font settings are valid for all controls, you cannot change the font of a static text alone. For your problem, you need to first use CreateFont to create a Font object, and then call the control SetFont, you can.

Example:
1, change the static style of the ID, such as: Idc_static1
2, add an edit control, establish an associated control M_editcontrol.
3, add the following code in the OnInitDialog:
Copy Code code as follows:

CFont * F;
f = new CFont;
F->createfont (//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);
CWnd *cwnd = GetDlgItem (IDC_STATIC1);
Cwnd->setfont (&font);
CWND->SETWINDOWTEXTW (L "Setting the required content");

Note that here we are using the CFont pointer, rather than the ordinary CFont local variables, in non-MFC programs, first with CreateFont to create a font handle, and then use SendMessage to the control Wm_setfont message, Assign the established font handle to the past.

2 But the size of the entire dialog box or window's font, using the SetFont () function of the dialog box or window does not have any effect. You can iterate through each control individually while initializing, but here's another simple way to use callback functions:
: Invokes the system's Api:::enumchildwindows (). , passing in the callback function and the redefined font. (the first parameter does not have to manage Ah, originally has AH)

1) CMainFrame::OnCreate () in the document view structure, called:: Enumchildwindows (). Implement all window and child window font changes

2 OnInitDialog () in the dialog box. Called:: Enumchildwindows (). Change all controls on the dialog window.
The callback functions are as follows:

Copy Code code 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:
Copy Code code as follows:

BOOL Caboutdlg::oninitdialog ()
{
CDialog::OnInitDialog ();
Todo:add Extra initialization here
:: Enumchildwindows (M_hwnd,:: Setchildfont, (LPARAM) G_font.getfont ());
return TRUE; Return TRUE unless your set the focus to a control
Exception:ocx Property Pages should return FALSE
}

Use 2:
Copy Code code as follows:

int CMainFrame::OnCreate (lpcreatestruct lpcreatestruct)
{
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) | |
!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 MFC in the Garbage SetFont (), Set the dialog box without a little reaction!)

3How to implement in MFC, when the system's font becomes larger, the dialog box above the font is also the corresponding larger?
Copy Code code as follows:

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 get the size of the system font, and then call the second method above.
All fonts on a form change with the size of the system font.

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.