Set the color and font of the MFC window

Source: Internet
Author: User
1. Change the background color of the dialog box and add a member variable PRIVATE: cbrush m_brush; to the class of the dialog box. Add the following code to the class constructor: m_brush.createsolidbrush (RGB (255,); // initialize to Blue to add a wm_ctlcolor message response function to the dialog box, message response function specially colored for controls. The Code is as follows: hbrush csettingdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
// Return HBr;/* specifically block the image brush returned by the system */
Return m_brush;/* return the selected blue paint brush */
} 2. If we want to set the background color and text color of a dialog box control, we only need to change the above return m_brush; To: If (pwnd-> getdlgctrlid () = idc_line_style) /* If the control name is idc_line_style */
{
PDC-> settextcolor (RGB (0,255, 0);/* set the text color in the color container to green * // PDC-> setbkmode (transparent ); /* set the text background to transparent. Otherwise, the text background is regarded as the default color */PDC-> setbkcolor (RGB (, 0, 0)./* set the background color of the text, note: If the above sentence is written, the background color is invalid */
Return m_brush;/* return the custom image brush handle of 1. Note that although the returned image brush handle is required, a cbrush object is placed here without errors. This is because cbrush has an hbrush overload */
}
Return HBr;/* Otherwise, the system's default paint brush handle */Note: If you want to set the text background color of a single line of edit, you need to use setbkcolor, and setbkmode is transparent, the text background is still the default color of the system. 3. Based on the above principle, when the dialog box control responds to the onpaint message, a static text is displayed, showing the text in the Set Font. Add a static text box named idc_text. Add a font member variable PRIVATE: cfont font; initialize the font in the constructor of the dialog box: m_font.createpointfont (200, "文 "); Return HBr in 2; add the following code before: If (pwnd-> getdlgctrlid () = idc_text)/* if it is a static text box referred to by the ID number */
{
PDC-> SelectObject (& m_font);/* adds the drawing device to the desired font. */
} Note that because the onpaint message response function is triggered before the control is drawn, the hbrush paint brush handle returned by it carries the attributes we set. 3. If you want to change the text color of the OK button, the above method will not work. We need to create a new ctestbtn class inherited from the cbutton class, use the Class Wizard to add a class's drawitem virtual function, and add the following code (which can be copied from the cbutton example ): // todo: add your code to draw the specified item
Uint ustyle = dfcs_buttonpush; // This Code only works with buttons.
Assert (lpdrawitemstruct-> ctltype = odt_button); // If drawing selected, add the pushed style to drawframecontrol.
If (lpdrawitemstruct-> itemstate & ods_selected)
Ustyle | = dfcs_pushed; // draw the button frame.
: Drawframecontrol (lpdrawitemstruct-> HDC, & lpdrawitemstruct-> rcitem,
Dfc_button, ustyle); // get the button's text.
Cstring strtext;
Getwindowtext (strtext); // draw the button text using the text color red.
Colorref croldcolor =: settextcolor (lpdrawitemstruct-> HDC, RGB (255, 0, 0);/* set the font of the button to red. Save the returned original color temporarily */
: Drawtext (lpdrawitemstruct-> HDC, strtext, strtext. getlength (),
& Lpdrawitemstruct-> rcitem, dt_singleline | dt_vcenter | dt_center );
: Settextcolor (lpdrawitemstruct-> HDC, croldcolor); then, right-click and select OK, and create an associated member variable m_btntest in the Wizard to inherit from the newly created ctestbtn class. On the "OK" Page, select "> styles-> owner draw". Otherwise, the "OK" button still inherits the default cbutton class.

 

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.