改变背景颜色,有两种方法:
1、在app的初始化函数中调用:
void SetDialogBkColor( COLORREF clrCtlBk = RGB(192, 192, 192), COLORREF clrCtlText = RGB(0, 0, 0) );
2、在对话框类的OnCtlColor函数中:
if(nCtlColor == CTLCOLOR_DLG)
{
CBrush *brush;
brush = new CBrush(RGB(221,221,221));
return (HBRUSH)(brush->m_hObject);
}
设置背景位图:
void CTestDlg::OnPaint()
{
CRect r;
GetClientRect(&r);
CPaintDC dc(this);
CRect rect=CRect(0,0,r.Width()/5,r.Height()/3);
//GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP2);
BITMAP bitMap;
bmpBackground.GetBitmap(&bitMap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY);
}
1.改变对话框的背景色
在C…App类中的InitInstance()里添加
SetDialogBkColor(RGB(0,192,0),RGB(0,0,0));
2.如果想改变静态文本或单选按钮的背景色可以用你说的那个获得控件ID,然后设置背景色,具体步骤:
(1)响应对话框类的WM_CTLCOLOR消息生成OnCtlColor函数
(2)为对话框类添加成员变量CBrush m_brush;
并在初始化函数中初始化m_brush.CreateSolidBrush(RGB(0,255,0));
(3)在OnCtlColor函数中添加代码以改变控件的文字颜色和背景色
switch(pWnd->GetDlgCtrlID())
{
case(IDC_INPUT):
pDC->SetTextColor(RGB(255,0,192));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
case(IDC_EDIT1):
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
case(IDC_CHOICE):
pDC->SetTextColor(RGB(255,128,0));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
case(IDC_RADIO1):
pDC->SetTextColor(RGB(255,0,20));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
default:
break;
}
3.如果想改变按钮的背景色,简直太难了,你要重写两个类,还需要在网上下,孙鑫的视频教程中也简单介绍了这个,可只是改变按钮的文字颜色