设置对话框、static和group的背景色和字体颜色

对话框背景色设置,重载OnPaint:

void CMonitorDialog::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CDialog::OnPaint()
/*CRect rect;
GetClientRect(rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackgroud;
bmpBackgroud.LoadBitmap(IDB_BITMAP_MBACK);


BITMAP bitmap;
bmpBackgroud.GetBitmap(&bitmap);
CBitmap * pbmpold = dcMem.SelectObject(&bmpBackgroud);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);*/


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

你可能感兴趣的:(struct,table,mfc,button,attributes)