一、修改对话框背景色:
1、为目标对话框添加WM_CTRCOLOR消息;
2、位目标Dlg类定义一个CBrush类型的私有成员变量:m_brushBg,并在其构造函数中将此画刷初始为
一个淡蓝色画刷(画刷颜色可任意修改):m_brushBg.CreateSolidBrush(RGB(95, 235, 245));
3、在OnCtlColor响应函数返回时返回自定义画刷:m_brushBg。
m_brushStatic1.CreateSolidBrush(RGB(95, 235, 245));
2、在OnCtlColor函数中添加如下代码:
if (pWnd->GetDlgCtrlID() == IDC_STATIC1)//如果是静态编辑框
{
pDC->SetTextColor(RGB(0, 0, 0));//修改字体的颜色
pDC->SetBkMode(TRANSPARENT);//把字体的背景变成透明的
return m_brushStatic1;//返回背景色
}
m_brushEdit1.CreateSolidBrush(RGB(45, 255, 100));
2、在OnCtlColor函数中添加如下代码:
if(pWnd->GetDlgCtrlID()==IDC_EDIT1)//如果是编辑框
{
pDC->SetTextColor(RGB(255,0,0));//设置编辑框字体的颜色
pDC->SetBkColor(RGB(255,255,0));//设置字体背景颜色
return m_brush;
}
afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);ON_WM_DRAWITEM())
2、重载OnDrawItem消息函数,添加代码(即可实现对所有button按钮颜色修改):
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
CString strText;
((CButton *)GetDlgItem(nIDCtl))->GetWindowText(strText);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
//if (lpDrawItemStruct->itemState&ODS_SELECTED)
{
CBrush brush(RGB(255, 80, 80));
dc.FillRect(&(lpDrawItemStruct->rcItem), &brush);
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
}
dc.Detach();
1、为目标对话框添加WM_CTRCOLOR消息;
2、位目标Dlg类定义一个CBrush类型的私有成员变量:m_brushBg,并在其构造函数中将此画刷初始为
一个淡蓝色画刷(画刷颜色可任意修改):m_brushBg.CreateSolidBrush(RGB(95, 235, 245));
3、在OnCtlColor响应函数返回时返回自定义画刷:m_brushBg。
二、修改静态编辑框Static Text背景色:
1、为静态编辑框添加一个CBrush类型的私有成员变量:m_brushStatic1,在构造函数中初始化:m_brushStatic1.CreateSolidBrush(RGB(95, 235, 245));
2、在OnCtlColor函数中添加如下代码:
if (pWnd->GetDlgCtrlID() == IDC_STATIC1)//如果是静态编辑框
{
pDC->SetTextColor(RGB(0, 0, 0));//修改字体的颜色
pDC->SetBkMode(TRANSPARENT);//把字体的背景变成透明的
return m_brushStatic1;//返回背景色
}
三、修改编辑框Edit Control背景色和字体:
1、为静态编辑框添加一个CBrush类型的私有成员变量:m_brushEdit1,在构造函数中初始化:m_brushEdit1.CreateSolidBrush(RGB(45, 255, 100));
2、在OnCtlColor函数中添加如下代码:
if(pWnd->GetDlgCtrlID()==IDC_EDIT1)//如果是编辑框
{
pDC->SetTextColor(RGB(255,0,0));//设置编辑框字体的颜色
pDC->SetBkColor(RGB(255,255,0));//设置字体背景颜色
return m_brush;
}
四、修改button按钮背景色:
1、将所有button修改为owner draw 类型,(在适当的位置添加下面的语句:afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);ON_WM_DRAWITEM())
2、重载OnDrawItem消息函数,添加代码(即可实现对所有button按钮颜色修改):
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
CString strText;
((CButton *)GetDlgItem(nIDCtl))->GetWindowText(strText);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
//if (lpDrawItemStruct->itemState&ODS_SELECTED)
{
CBrush brush(RGB(255, 80, 80));
dc.FillRect(&(lpDrawItemStruct->rcItem), &brush);
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
}
dc.Detach();