Change the Push Button background color

zhaozj2021-02-11  194

Change the Push Button background color

Wenyi

In Windows Edit, Staticbox's background colors can be changed by handling WM_CTLCOLOR messages, but Push Button is not.

The only way is to use the OwnerDraw style button. The method described in this paper is to use CButton's derived class.

class CCButton: public CButton {DECLARE_DYNAMIC (CCButton) public: CCButton (); virtual ~ CCButton (); BOOL CCButton :: Attach (const UINT nID, CWnd * pParent) protected: virtual void DrawItem (LPDRAWITEMSTRUCT lpDIS); // Required weight The functions of the load pub: colorRef m_fg, m_bg, m_disabled_fg, m_disabled_bg; // The four colors are text, background, time of failure, time, failure.

Implement DrawItem

Void ccbutton :: DrawItem (LPDRAWITEMSTRUCT LPDIS) {CDC * PDC = CDC :: fromHandle (LPDIS-> HDC); // ??????? DC uint state = lpdis-> itemState; // Get State CRECT FOCUSRECT, BtnRect; //, two rectangles, indicate the dashed rectangle and button rectangular focusRect.copyRect (& lpdis-> rcitem); btnRect.copyRect (& lpdis-> rcitem); // // Adjust the dotted rectangle // FocusRect. LEFT = 4; FocusRect.right - = 4; FocusRect.top = 4; FocusRect.bottom - = 4; // // AppAe = 512; Tchar Buffer [buffs]; getWindowText Buffer, bufsize; // use m_fg, m_bg color utilization Draw3DRect (...) draw button border

// FillRect (...) fill button inside

// DrawText (...) Drawing text // Depending on the current status adjustment // if (State & ODS_FOCUS) {... // gets the input focus, draw a dotted line IF through the FocusRect (State & ODS_Selected ) {...../ is pressed, draw the lower border}} else if (state & ods_disabled) {// invalid, through m_disabled_fg, m_disabled_bg redraw button inside}}

CCButton is a CButton derived class with all member functions of CButton, but it is necessary to use the BS_OWNERDRAW style when creating.

If the button is not dynamically generated, use the attach function to make CCButton instead of the original button window process.

Bool CCButton :: Attach (const Uint Nid, CWND * PPARENT)

{

Getdlgitem (NID) -> ModifyStyle (0, BS_OWNERDRAW, 0);

IF (! Subclassdlgitem (NID, PPARENT))

Return False;

Return True;

}

Join the following lines as in the initdialog (...) in a dialog box.

{// Assume that m_cbtn is a member variable IDC_BUTTON1 as a button ID value

m_cbbtn.attach (idc_button1, this);

}

转载请注明原文地址:https://www.9cbs.com/read-3828.html

New Post(0)