對話框背景色設定,重載OnPaint:
void CMonitorDialog::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此處添加消息處理程式代碼
// 不為繪圖消息調用 CDialog::OnPaint()
CRect rect;
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(85,85,85)); //設定為黑色背景
//CDialog::OnPaint();
}
MFC簡單皮膚顔色繪制(SetDialogBkColor不再被支援,看 WM_CTLCOLOR消息如何百變控件顔色)
[轉]
WM_CTLCOLOR消息用來完成對EDIT, STATIC, BUTTON等控件設定背景和字型顔色, 其用法如下:
1.首先在自己需要設定界面的對話框上點選右鍵-->建立類向導-->加入WM_CTLCOLOR消息-->自動生成OnCtlColor()函數, 此函數可以對本對話框的控件的界面外觀做修飾, 用法如下:
将類向導産生的函數做如下修改:
HBRUSH CDialogColor::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC,pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// 設定顯示字型
CFont * cFont=new CFont;
cFont->CreateFont(16,0,0,0,FW_SEMIBOLD,FALSE,FALSE,0,
ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
DEFAULT_PITCH&FF_SWISS,"Arial");
// 對特定的控件做修改
switch()
{
case CTLCOLOR_STATIC: //對所有靜态文本控件的設定
{
pDC->SetBkMode(TRANSPARENT);
//設定背景為透明
pDC->SetTextColor(RGB(255,255,0)); //設定字型顔色
pWnd->SetFont(cFont); //設定字型
HBRUSH B = CreateSolidBrush(RGB(125,125,255));
//建立畫刷
return (HBRUSH) B; //傳回畫刷句柄
}
case CTLCOLOR_EDIT: //對所有編輯框的設定
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,255,0));
pWnd->SetFont(cFont);
HBRUSH B = CreateSolidBrush(RGB(125,125,255));
return (HBRUSH) B;
}
default:
return CDialog::OnCtlColor(pDC,pWnd, nCtlColor);
}
}
注:case的類别有以下幾種:
CTLCOLOR_BTN 按鈕控件
CTLCOLOR_DLG 對話框
CTLCOLOR_EDIT 編輯框
CTLCOLOR_LISTBOX 清單框
CTLCOLOR_MSGBOX 消息框
CTLCOLOR_SCROLLBAR 滾動條
CTLCOLOR_STATIC 靜态文本
2.你可能覺得對所有的控件使用統一的界面設定覺得不自由, 其實VC同樣可以對特定的ID的控件進行設定, 方法如下:
switch (pWnd->GetDlgCtrlID())
{
//針對ID為IDC_CTL1, IDC_CTL2和IDC_CTL3的控件進行同樣的設定
case IDC_CTL1:
case IDC_CTL2:
case IDC_CTL3:
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,255, 0));
pWnd->SetFont(cFont);
HBRUSH B = CreateSolidBrush(RGB(125,125,255));
return (HBRUSH) B;
}
default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
HBrush用法
HBRUSH hbr;
第一種: hbr= CreateSolidBrush(RGB(255,0,0)); //單色的畫刷
第二種: hbr= (HBRUSH)GetStockObject(BLACK_BRUSH); //隻能取特定顔色的畫刷,如BLACK_BRUSH,GRAY_BRUSH等刷
第三種: hbr= CreatePatternBrush(HBITMAP hbmp); //得到位圖畫刷
第四種: hbr = CreateHatchBrush(int fnStyle, COLORREF clrref) //建立一種帶陰影的畫刷
第五種: hbr= CreateBrushIndirect(LOGBRUSH); //通過LOGBRUSH結構體來取畫刷
typedef struct tagLOGBRUSH {
UINT lbStyle; //畫刷類型
COLORREF lbColor; //顔色
LONG lbHatch; //陰影
} LOGBRUSH, *PLOGBRUSH;
第六種: hbr= HBRUSH CreateDIBPatternBrush( //通過與裝置無關位圖建立一個畫刷
HGLOBAL hglbDIBPacked, // handle to DIB
UINT fuColorSpec // color table data
);
例如:
HBRUSH CAfdView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if (pWnd->GetDlgCtrlID()==IDC_STATIC1)
{
pDC->SetTextColor(RGB(200,0,0));
pDC->SetBkColor(RGB(0,0,255));
static HBRUSH hBrush = CreateSolidBrush(RGB(222,0,255));
return hBrush;
}
// TODO: Return a different brush if the default is not desired
else
return hbr;
}
改變對話框背景色
HBRUSH CDqfDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
if(nCtlColor == CTLCOLOR_DLG)
{
CBrush *brush;
brush = new CBrush(RGB(221,221,221));
return (HBRUSH)(brush->m_hObject);
}
return hbr;
}
另外:
HBRUSH B = CreateSolidBrush(RGB(125,125,255));
return (HBRUSH) B;
可能會引起GDI資源洩漏,解決方法請參考:http://blog.csdn.net/seven407/article/details/7497550