Change the background color of a widget (such as cview, cframewnd, or cwnd)

Source: Internet
Author: User

Abstract:

In VC programming, you can change the background color of controls (such as cview, cframewnd, or cwnd) by processing specific messages. However, if you want to change the color of a button, you can only use the self-drawn button (you can also use the bitmap button, which is not described here) and not change it through onctlcolor.


Body:

1. 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:

// Cmydialog. h Definition

Class cmydialog: Public cdialog // derive your own dialog box class
{
........

// Implementation

Protected:

// Generated message map Functions

// {Afx_msg (cmydialog)

Afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor );

.......

//} Afx_msg

Declare_message_map ()
};


// Cmydialog. cpp Definition

......

Hbrush cmydialog: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)

{

Switch (nctlcolor ){

Case ctlcolor_edit:

Case ctlcolor_msgbox:

Case ctlcolor_dlg:

Case ctlcolor_edit: // Add the control message you want to change the background color.

PDC-> setbkmode (transparent );

Hbrush B = createsolidbrush (color); // color is the color you want to set

Return (hbrush) B;

Default: // set the default color and background for other controls.

Return cdialog: onctlcolor (PDC, pwnd, nctlcolor );

}}

Note: 1. You can process the preceding messages separately to implement different background colors for different controls.

2. This method is not applicable to button controls.


2. Custom buttons are used for different colors.

The following describes how to customize a square Color button:

Step 1: derive your own button class.

// Ccolorbutton. h

Class ccolorbutton: Public cbutton

{

Declare_dynamic (ccolorbutton)

Public:

Ccolorbutton ();

Virtual ~ Ccolorbutton ();


Bool attach (const uint NID, cwnd * pparent,

Const colorref bgcolor = RGB (192,123,192), // the background color of the button

Const colorref fgcolor = RGB (1, 1, 1), // text color

);
Protected:

Virtual void drawitem (lpdrawitemstruct lpdis); // redefine the virtual function drawitem

Void drawframe (CDC * DC, crect R); // draw a button box

Void drawfilledrect (CDC * DC, crect R, colorref color); // fill the button box

Void drawline (CDC * DC, crect endpoints, colorref color );

Void drawline (CDC * DC, long left, long top, long right, long bottom, colorref color );

Void drawbuttontext (CDC * DC, crect R, const char * Buf, colorref textcolor );

// Draw the text on the button


Colorref getfgcolor () {return m_fg ;}

Colorref getbgcolor () {return m_bg ;}

PRIVATE:

Colorref m_fg, m_bg;

};

# Endif


Step 2: define functions

// Ccolorbutton. cpp

......

// Ccolorbutton

Implement_dynamic (ccolorbutton, cbutton)

Ccolorbutton: ccolorbutton ()

{}

Ccolorbutton ::~ Ccolorbutton ()

{

}
// Define the attach () function

Bool ccolorbutton: attach (const uint NID, cwnd * pparent, const colorref bgcolor, const colorref fgcolor)

{

If (! Subclassdlgitem (NID, pparent ))

Return false;

M_fg = fgcolor;

M_bg = bgcolor;

Return true;

}

// Reload drawitem ()

Void ccolorbutton: drawitem (lpdrawitemstruct lpdis)

{

CDC * PDC = CDC: fromhandle (lpdis-> HDC );

Uint state = lpdis-> itemstate;

Crect focusrect, btnrect;

Focusrect. copyrect (& lpdis-> rcitem); // select the dotted box of the button

Btnrect. copyrect (& lpdis-> rcitem );

// Set the dotted box selected by the button

Focusrect. Left + = 4;

Focusrect. Right-= 4;

Focusrect. Top + = 4;

Focusrect. Bottom-= 4;


// Subject

Const int bufsize = 512;

Tchar buffer [bufsize];

Getwindowtext (buffer, bufsize );


// Draw and mark the button

Drawfilledrect (PDC, btnrect, getbgcolor ());

Drawframe (PDC, btnrect );

Drawbuttontext (PDC, btnrect, buffer, getfgcolor ());


// If the button is selected, draw the selected dotted box on it

If (State & ods_focus ){

Drawfocusrect (lpdis-> HDC, (lprect) & focusrect );
}
}

Void ccolorbutton: drawframe (CDC * DC, crect R)

{// Draw a button. You can customize this function to create a button of different shapes.

Drawline (DC, R. Left, R. Top, R. Right, R. Top, RGB (255,255,255 ));
Drawline (DC, R. Left, R. Top, R. Left, R. Bottom, RGB (255,255,255 ));
// Draw the peripheral frame of the button below to make the button stereoscopic

Drawline (DC, R. Left + 1, R. Bottom-1, R. Right, R. Bottom-1, RGB (1, 1, 1 ));

// Draw the left and upper borders of the button

Drawline (DC, R. Right-1, R. Top + 1, R. Right-1, R. Bottom, RGB (1, 1, 1 ));
// Draw the right and bottom borders of the button
}
// Fill the button box with colors

Void ccolorbutton: drawfilledrect (CDC * DC, crect R, colorref color)

{

Cbrush B;

B. createsolidbrush (color );

DC-> fillrect (R, & B );

}

// Drawline is used to draw a button. It is a Multistate function.

Void ccolorbutton: drawline (CDC * DC, crect endpoints, colorref color)

{
......
}
Void ccolorbutton: drawline (CDC * DC, long left, long top, long right, long bottom, colorref color)

{
......
}

// Draw the new text

Void ccolorbutton: drawbuttontext (CDC * DC, crect R, const char * Buf, colorref textcolor)

{

Colorref prevcolor = Dc-> settextcolor (textcolor );

DC-> setbkmode (transparent );

DC-> drawtext (BUF, strlen (BUF), R, dt_center | dt_vcenter | dt_singleline );

DC-> settextcolor (prevcolor );

}


Step 3: reference a custom class

Customize any dialog box ccolordlg and draw a key control on it. The ID is idok.

// Ccolordlg. h

Class ccolordlg: Public cdialog

{

.....

// Implementation

Protected:

Ccolorbutton m_btnok;

}


// Ccolordlg. cpp

.......

Bool ccolorbtnsampledlg: oninitdialog ()

{

Cdialog: oninitdialog ();

.......

Verify (m_btnok.attach (idok, this, red, blue, yellow ));

.......

}

Related Article

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.