看看这个,希望对你有帮助。 HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { // Call the base class implementation first! Otherwise, it may // undo what we're trying to accomplish here. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // Are we painting the IDC_MYSTATIC control? We can use // CWnd::GetDlgCtrlID() to perform the most efficient test. if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC) { // Set the text color to red pDC->SetTextColor(RGB(255, 0, 0)); // Set the background mode for text to transparent // so background will show thru. pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object hbr = m_brush; } return hbr; }
HBRUSH CDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { // Call the base class implementation first! Otherwise, it may // undo what we are trying to accomplish here. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // Are we painting the IDC_MYSTATIC control? We can use // CWnd::GetDlgCtrlID() to perform the most efficient test. if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC) { // Set the text color to red. pDC->SetTextColor(RGB(255, 0, 0)); // Set the background mode for text to transparent // so background will show thru. pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object. hbr = m_brush; } return hbr;
HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we're trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0)); // Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object
hbr = m_brush;
} return hbr;
}
{
// Call the base class implementation first! Otherwise, it may
// undo what we are trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red.
pDC->SetTextColor(RGB(255, 0, 0)); // Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT); // Return handle to our CBrush object.
hbr = m_brush;
} return hbr;
谢谢各位
{
DECLARE_DYNAMIC(CCButton)
public:
CCButton();
virtual ~CCButton();
BOOL CCButton::Attach(const UINT nID, CWnd* pParent)protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);//必需重载的函数public:
COLORREF m_fg, m_bg, m_disabled_fg, m_disabled_bg;//四种颜色分别为文字,背景,失效时文字,失效时背景
};
实现DrawItemvoid CCButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);//???????DC UINT state = lpDIS->itemState; //得到状态
CRect focusRect, btnRect;//两个矩形,表示得当输入焦点时的虚线矩形和按钮矩形
focusRect.CopyRect(&lpDIS->rcItem);
btnRect.CopyRect(&lpDIS->rcItem); //
//调整虚线矩形
//
focusRect.left += 4;
focusRect.right -= 4;
focusRect.top += 4;
focusRect.bottom -= 4; //
// 得当Button上文字
//
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);
// 使用m_fg, m_bg颜色利用 Draw3dRect(...)绘制按钮边框 // FillRect(...)填充按钮内部 // DrawText(...)绘制文字 //根据当前状态调整显示
//
if (state & ODS_FOCUS) {
.........//得到输入焦点,通过focusRect画虚线
if (state & ODS_SELECTED){
.....// 被按下,绘制下陷边框
}
}
else if (state & ODS_DISABLED) {
//失效,通过m_disabled_fg, m_disabled_bg 重绘按钮内部
}
}
CCButton是CButton派生类,具有CButton的全部成员函数,但在创建时需要使用BS_OWNERDRAW风格。如果按钮不是动态生成,使用Attach函数使CCButton代替原来按钮的窗口过程。BOOL CCButton::Attach(const UINT nID, CWnd* pParent)
{
GetDlgItem(nID)->ModifyStyle(0,BS_OWNERDRAW,0); if (!SubclassDlgItem(nID, pParent))
return FALSE; return TRUE;
} 如在一对话框的InitDialog(...)中加入下面几行{// 假定 m_cbBtn为成员变量 IDC_BUTTON1为一按钮ID值 m_cbBtn.Attach(IDC_BUTTON1,this);}
在拉拉的观点中我不明白m_brush的意思。