Change the Push Button background color

zhaozj2021-02-17  83

Change the Push Button background color

Change the Push Button background color

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

PUBLIC:

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 status

CRECT FOCUSRECT, BTNRECT; // Two rectangles, indicating dashed rectangles and buttons rectangles when entering focus

FocusRect.copyRect (& lpdis-> rcitem);

BtnRect.copyRect (& lpdis-> rcitem);

//

// Adjust the broken line rectangle

//

FocusRect.Left = 4;

FocusRect.right - = 4;

FocusRect.top = 4;

FocusRect.bottom - = 4;

//

// Before you are on the text

//

Const int buffs = 512;

Tchar buffer [bufsize];

GetWindowText (Buffer, bufsize);

// Use m_fg, m_bg color utilize Draw3DRect (...) to draw button border

// FillRect (...) fill button inside

// DrawText (...) Drawing text

/ / According to the current status adjustment

//

IF (State & ODS_FOCUS) {

......... // Get the input focus, draw a dotted line through the FocusRect

IF (State & ODS_SELECTED) {

..... / Press, draw the lower border

}

}

ELSE IF (State & ODS_Disabled) {

// Failure, through m_disabled_fg, m_disabled_bg redraw 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-31489.html

New Post(0)