Set the font and color of MFC

Source: Internet
Author: User

 

Set Font
Function prototype:
Bool createfont (INT nheight, int nwidth, int weight, int norientation, int nweight, byte bitalic, byte bunderline, byte weight, byte ncharset, byte noutprecision, byte nclipprecision, byte nquality, byte npitchandfamily, lpctstr lpszfacename );

Parameter description:
Nheight: font height. case 1,> 0: font height (device coordinates); 2, = 0: default font. 3. <0: the absolute value of this value is height.
Nwidth: font width.
Nescapement: the skew of text lines.
Norientation: the skew of the character baseline.
Nweight: font width:
. Fw_dontcare
. Fw_thin
. Fw_extralight
.....
Bitalic: whether the font is Italic
Bunderline: whether the font is underlined
Cstrikeout: indicates whether the font contains strikethrough.
Ncharset: font Character Set
. Ansi_charset
. Default_charset
. Symbol_charset .....
Noutprecision: output precision of Characters
Nclipprecision: Specifies the character cropping precision.
Nquality: character output quality
Npitchandfamily: Character spacing and font family (low-level spacing and High-Level Family)
Lpszfacename: font name
[Program Implementation]
Suppose you already have a dialog box project named my. There is an edit control with ID = idc_edit1.
Class cmydlg: Public cdialog
{
Public:
Cfont * m_font; // it is best to use pointers. I don't seem to be able to use object calls. I don't know why!
M_font = new cfont; // remember to delete m_font at the end;
........
};
Bool ctmydlg: oninitdialog ()
{
Cdialog: oninitdialog ();

// Todo: add extra initialization here
// Cfont m_font;
M_font-> createfont (-11, 0, 0, 100, false, false, 0, ansi_charset, out_default_precis, clip_default_precis, default_quality, ff_swiss, "Arial ");
Cedit * m_edit = (cedit *) getdlgitem (idc_edit1 );
M_edit-> setfont (m_font, false );
Return true; // return true unless you set the focus to a control
}
Note: In oninitdialog (), remove the "// cfont m_font;"/"before and the cfont m_font in the class declaration. What is the result? Try it by yourself.
Change the edit font color!
Hbrush cbuttondlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );

// Todo: change any attributes of the DC here
If (nctlcolor = ctlcolor_edit)
{
If (pwnd-> getdlgctrlid () = idc_edit1)
{
PDC-> settextcolor (RGB (255,255, 0 ));
PDC-> setbkcolor (RGB (251,247,200 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brush.getsafehandle ();
}
}

// Todo: return a different brush if the default is not desired
Return HBr;
}

Cbrush m_brushedit;
M_brushedit.createsolidbrush (RGB (255,255, 0 ));

Add the following to the DLG: onctlcolor function:
If (nctlcolor = ctlcolor_edit ){
PDC-> settextcolor (RGB (255, 0, 0); // text color
PDC-> setbkcolor (RGB (255,255,200); // text background color
Return (hbrush) m_brushedit.getsafehandle (); // the color of the edit box
}

Change the dialog box background

Cbitmap m_bkgndbmp;
M_bkgndbmp.loadbitmap (idb_bitmap1 );

Bool cbuttondlg: onerasebkgnd (CDC * PDC)
{
Crect rcclient;
Getclientrect (& rcclient );
Bitmap bm;
M_bkgndbmp.getbitmap (& BM );
CDC memdc;
Memdc. createcompatibledc (PDC );
Cbitmap * poldbmp = memdc. SelectObject (& m_bkgndbmp );
PDC-> stretchblt (0, 0, rcclient. Width (), rcclient. Height (), & memdc, 0, BM. bmwidth, BM. bmheight, srccopy );
Memdc. SelectObject (poldbmp );
Memdc. deletedc ();
Return true;

// Return cdialog: onerasebkgnd (PDC );
}

In an MFC application, you can use the onctlcolor () function to change the background color of the control. The method is to set the required color in this function, and then return a paint brush handle to re-paint the control background color. The onctlcolor () function processes the control background color by capturing the corresponding control message. Common messages include:

Ctlcolor_dlg dialog box

Ctlcolor_edit edit box

Ctlcolor_listbox list box

Ctlcolor_msgbox message box

Ctlcolor_scrollbar Slider

Ctlcolor_static static text box, rectangle, etc.

The following sample code shows how to change the background color of the preceding controls:

Afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor); // original definition of the overloaded Method *. h
Afx_msg bool onerasebkgnd (CDC * PDC); // dialog box background
......
Begin_message_map (cbuttondlg, cdialog)

On_wm_ctlcolor () // creates a message ing *. cpp
On_wm_erasebkgnd () // sets the image background of the dialog box.

End_message_map ()

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.