通过MFC的类向导,添加WM_CTLCOLOR、WM_PAINT、WM_DRAWITEM消息,对应的函数分别如下:
1、控件颜色 HBRUSH CMain::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
可以设置编辑框、静态文本等的背景颜色,同时也可以设置文本颜色
HBRUSH CMain::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if(pWnd->GetDlgCtrlID( ) == (IDC_STATIC)) //主界面
{
//pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(30,100,200)); //设置文本颜色
pDC->SetBkColor(RGB(200,230,255)); //字体背景色
return HBRUSH(GetStockObject(HOLLOW_BRUSH));
}
return hbr;
}
2、背景颜色 void CMain::OnPaint()
void CMain::OnPaint()
{
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(200,230,255)); //设置背景颜色
}
3、按钮颜色 void CMain::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
按钮属性需要改为Owner Draw,这种方法只能实现很简陋的按键立体效果和颜色的更改,同时也可以更改按钮字体的颜色
void CMain::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
CString strText;
((CButton *)GetDlgItem(nIDCtl))->GetWindowText(strText);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);//透明
if(nIDCtl==IDC_Main_FFGL)
{
if(GetDlgItem(IDC_Main_FFGL)->IsWindowEnabled()) //当按钮不操作 & 按钮可用
{
CBrush brush(RGB(30, 200, 255)); //内背景画刷
CPen m_BoundryPen(0,2,RGB(80,80,80)); //边框画笔
CBrush m_BackgroundBrush=RGB(140,200,255); //大背景画刷
dc.FillRect(&(lpDrawItemStruct->rcItem), &m_BackgroundBrush);//利用画刷brush,填充大矩形框
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
POINT pt;
//画按钮的外边框,它是一个半径为10的圆角矩形
pt.x = 10;
pt.y = 10;
CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
pDC->RoundRect(&rect, pt);
pDC->SelectObject(hOldPen);
rect.DeflateRect(3, 3, 3, 3); //缩进
CBrush *pOldBrush = pDC->SelectObject(&m_BackgroundBrush);
pDC->Rectangle(rect); //画矩形
pDC->SelectObject(pOldBrush);
pDC->FillRect(rect,&brush ); //填充内矩形
//因为这里进行了重绘,所以文字也要重绘
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),&lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
}
if(lpDrawItemStruct->itemState&ODS_SELECTED & GetDlgItem(IDC_Main_FFGL)->IsWindowEnabled()) //如果按钮可用 & 点击
{
CBrush brush(RGB(0, 160, 230)); //内背景画刷
CPen m_BoundryPen(0,1,RGB(80,80,80)); //边框画笔
CBrush m_BackgroundBrush=RGB(140,200,255); //大背景画刷
dc.FillRect(&(lpDrawItemStruct->rcItem), &m_BackgroundBrush);//利用画刷brush,填充矩形框
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
POINT pt;
//画按钮的外边框,它是一个半径为5的圆角矩形
pt.x = 10;
pt.y = 10;
CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
pDC->RoundRect(&rect, pt);
pDC->SelectObject(hOldPen);
rect.DeflateRect(4, 4, 3, 3);
CBrush *pOldBrush = pDC->SelectObject(&m_BackgroundBrush);
pDC->Rectangle(rect);
pDC->SelectObject(pOldBrush);
pDC->FillRect(rect,&brush );
//因为这里进行了重绘,所以文字也要重绘
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),&lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
}
if(!GetDlgItem(IDC_Main_FFGL)->IsWindowEnabled()) //不可用,显示灰色
{
CBrush brush(RGB( 190, 190, 200)); //内背景画刷
CPen m_BoundryPen(0,2,RGB(80,80,80)); //边框画笔
CBrush m_BackgroundBrush=RGB(140,200,255); //大背景画刷
dc.FillRect(&(lpDrawItemStruct->rcItem), &m_BackgroundBrush);//利用画刷brush,填充矩形框
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
POINT pt;
//画按钮的外边框,它是一个半径为5的圆角矩形
pt.x = 10;
pt.y = 10;
CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
pDC->RoundRect(&rect, pt);
pDC->SelectObject(hOldPen);
rect.DeflateRect(3, 3, 3, 3);
CBrush *pOldBrush = pDC->SelectObject(&m_BackgroundBrush);
pDC->Rectangle(rect);
pDC->SelectObject(pOldBrush);
pDC->FillRect(rect,&brush );
//因为这里进行了重绘,所以文字也要重绘
pDC->SetTextColor(RGB(255,255,255)); //设置文本颜色
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),&lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
}
dc.Detach();
}
}