afx_msg HBRUSH OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtlColor);
参数:
CTLCOLOR_BTN 按钮控件
CTLCOLOR_DLG 对话框
CTLCOLOR _ EDIT 编辑框
CTLCOLOR_LISTBOX 列表控件
CTLCOLOR_MSGBOX 消息控件
CTLCOLOR_SCROLLBAR 滚动条控件
CTLCOLOR_STATIC 静态控件
HBRUSH CMyDlg :: OnCtlColor ( CDC * pDC , CWnd * pWnd , UINT nCtlColor ){HBRUSH hbr = CDialog :: OnCtlColor ( pDC , pWnd , nCtlColor );// TODO: Change any attributes of the DC hereif (nCtlColor==CTLCOLOR_STATIC)//如果当前控件属于静态文本{pDC-> SetTextColor(RGB(255,0,0)); //字体颜色//pDC->SetBkMode(TRANSPARENT); //设置字体背景为透明pDC-> SetBkColor(RGB(0, 0, 255)); //字体背景色}else if (nCtlColor==CTLCOLOR_BTN) //如果当前控件属于按钮{pDC-> SetTextColor(RGB(255,0,0)); //字体颜色//pDC->SetBkMode(TRANSPARENT); //设置字体背景为透明pDC-> SetBkColor(RGB(0, 0, 255)); //字体背景色}// TODO: Return a different brush if the default is not desiredreturn hbr;}
![VC修改控件背景颜色 - 很轻松! - dingmz_frc - dingmz_frc的博客](http://img1.ph.126.net/YIy9-I6WtlGj0IVlz8hztg==/1671961361761340593.jpg)
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(255,0,0)); //字体颜色 pDC->SetBkMode(TRANSPARENT); //设置字体背景为透明 //pDC-> SetBkColor(RGB(0, 0, 255)); //字体背景色 } else if (nCtlColor==CTLCOLOR_BTN) //如果当前控件属于按钮 { pDC-> SetTextColor(RGB(255,0,0)); //字体颜色 pDC->SetBkMode(TRANSPARENT); //设置字体背景为透明 //pDC-> SetBkColor(RGB(0, 0, 255)); //字体背景色 } // TODO: Return a different brush if the default is not desired return hbr; }
![VC修改控件背景颜色 - 很轻松! - dingmz_frc - dingmz_frc的博客](http://img1.ph.126.net/0TU8JoN_PKKyTJL2LhMIOw==/895371901016640556.jpg)
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)//如果当前控件属于静态文本 { return (HBRUSH)GetStockObject(NULL_BRUSH); } else if (nCtlColor==CTLCOLOR_BTN) //如果当前控件属于按钮 { return (HBRUSH)GetStockObject(NULL_BRUSH); } // TODO: Return a different brush if the default is not desired return hbr; }
![VC修改控件背景颜色 - 很轻松! - dingmz_frc - dingmz_frc的博客](http://img2.ph.126.net/m-ybdZcCVTIWD9zq33QmqA==/3384455120068958525.jpg)
这样不能实现修改按钮背景的效果。因为,我们定义的是局部的 m_bkBrush,这样在函数OnCtlColor返回时 m_bkBrush就会被销毁释放掉,所以我们需要定义全局的 m_bkBrush或将 m_bkBrush定义成 CMyDlg的成员变量。此外必须设置按钮的 OwnerDraw属性为TRUE!不然按钮的背景不会被修改。HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here else if (nCtlColor==CTLCOLOR_BTN) //如果当前控件属于按钮 { CBrush m_bkBrush; //准备一把主窗口背景刷子
m_bkBrush.CreateSolidBrush(RGB(0xff,0x00,0x00)); //创建一把黄色的背景刷子 return m_bkBrush; //返回刚才创建的背景刷子 } // TODO: Return a different brush if the default is not desired return hbr; }
![VC修改控件背景颜色 - 很轻松! - dingmz_frc - dingmz_frc的博客](http://img0.ph.126.net/16eLBhu41eRNIR_ItbipKg==/120752765108915559.jpg)
HBRUSH CDSCAMDEMODlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here //对特定的控件做修改 switch (pWnd->GetDlgCtrlID()) { case IDC_STATIC1: pDC->SetTextColor(RGB(0,0,0)); //设置字体颜色 pDC->SetBkColor(RGB(0,0,0)); //设置字体背景颜色 break; case IDC_STATIC2://此段可忽略 break; default: break; }
}
HBRUSH CDSCAMDEMODlg::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_DLG) //如果是CTLCOLOR_EDIT edit背景色能改变吗? { m_bkBrush.CreateSolidBrush(RGB(255,0,0)); //创建一把黄色的背景刷子 return m_bkBrush; //返回刚才创建的背景刷子 }
return hbr;
}