替按钮添加背景颜色和字体颜色
为按钮添加背景颜色和字体颜色
/设置对话框的背景颜色及字体颜色
HBRUSH CMyDlg::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_STATIC)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(255,255,255));//文字背景色
HBRUSH b=CreateSolidBrush(RGB(0,0,0));//控件背景色
return b;
}
//为编辑框添加背景颜色和字体颜色
else if(pWnd->GetDlgCtrlID()==IDC_LIST2)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(255,255,255));//文字背景色
HBRUSH b=CreateSolidBrush(RGB(0,0,0));//控件背景色
return b;
}
//为按钮添加背景颜色和字体颜色
else if(pWnd->GetDlgCtrlID()==IDC_BUTTON1)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(255,255,255));//文字背景色
HBRUSH b=CreateSolidBrush(RGB(0,0,0));//控件背景色
return b;
}
return hbr;
}
大神帮忙看看,怎么静态文本和编辑框都可以改变,按钮不行呢。必须要重写Cbutton类吗
------解决方案--------------------
必须重新CButton类,添加处理DrawItem虚函数,MSDN上有个例子代码
/设置对话框的背景颜色及字体颜色
HBRUSH CMyDlg::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_STATIC)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(255,255,255));//文字背景色
HBRUSH b=CreateSolidBrush(RGB(0,0,0));//控件背景色
return b;
}
//为编辑框添加背景颜色和字体颜色
else if(pWnd->GetDlgCtrlID()==IDC_LIST2)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(255,255,255));//文字背景色
HBRUSH b=CreateSolidBrush(RGB(0,0,0));//控件背景色
return b;
}
//为按钮添加背景颜色和字体颜色
else if(pWnd->GetDlgCtrlID()==IDC_BUTTON1)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(255,255,255));//文字背景色
HBRUSH b=CreateSolidBrush(RGB(0,0,0));//控件背景色
return b;
}
return hbr;
}
大神帮忙看看,怎么静态文本和编辑框都可以改变,按钮不行呢。必须要重写Cbutton类吗
------解决方案--------------------
必须重新CButton类,添加处理DrawItem虚函数,MSDN上有个例子代码
- C/C++ code
// NOTE: CMyButton is a class derived from CButton. The CMyButton // object was created as follows: // // CMyButton myButton; // myButton.Create(_T("My button"), // WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW, // CRect(10,10,100,30), pParentWnd, 1); // // This example implements the DrawItem method for a CButton-derived // class that draws the button's text using the color red. void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { 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)); ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER); ::SetTextColor(lpDrawItemStruct->hDC, crOldColor); }
------解决方案--------------------
- C/C++ code
class CButton : public CWnd { DECLARE_DYNAMIC(CButton) // Constructors public: CButton(); virtual BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID); // Attributes UINT GetState() const; void SetState(BOOL bHighlight); int GetCheck() const; void SetCheck(int nCheck); UINT GetButtonStyle() const; void SetButtonStyle(UINT nStyle, BOOL bRedraw = TRUE); HICON SetIcon(HICON hIcon); HICON GetIcon() const; HBITMAP SetBitmap(HBITMAP hBitmap); HBITMAP GetBitmap() const; HCURSOR SetCursor(HCURSOR hCursor); HCURSOR GetCursor(); #if (_WIN32_WINNT >= 0x501) BOOL GetIdealSize(SIZE* psize); BOOL SetImageList(PBUTTON_IMAGELIST pbuttonImagelist); BOOL GetImageList(PBUTTON_IMAGELIST pbuttonImagelist); BOOL SetTextMargin(RECT* pmargin); BOOL GetTextMargin(RECT* pmargin); #endif // (_WIN32_WINNT >= 0x501) // Overridables (for owner draw only) virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);//[color=#FF0000]here[/color] // Implementation public: virtual ~CButton(); protected: virtual BOOL OnChildNotify(UINT, WPARAM, LPARAM, LRESULT*); };