CBursh 画刷改变背景色

CBrush有三种:

m_brush.CreateSolidBrush(RGB(255, 0, 255));    //颜色画刷

m_brush.CreateHatchBrush(HS_CROSS, RGB(255, 255, 0));   //阴影画刷

m_brush.CreatePatternBrush(&m_bmp);    图片画刷

使用画刷改变背景色,需要用到WM_CTLCOLOR 在窗口显示之前改变窗口的颜色,先得到旧的画刷句柄,并且返回自己的画刷

HBRUSH CBrushDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);


// TODO:  Change any attributes of the DC here
// TODO:  Return a different brush if the default is not desired
if(m_brush.GetSafeHandle() && nCtlColor == CTLCOLOR_DLG)
{
return m_brush;
}
else
       return hbr;
}

然后就可以改变使用自己的画刷改变窗口颜色,这里使用组合框来选择颜色,在组合框内容发生变化的事件中改变背景

void CBrushDlg::OnCbnSelchangeComColor()
{
// TODO: Add your control notification handler code here
if(m_brush.GetSafeHandle())
m_brush.DeleteObject();
switch(m_cmb_color.GetCurSel())
{
case 0:
m_brush.CreateSolidBrush(RGB(255, 0, 255));
break;
case 1:
m_brush.CreateHatchBrush(HS_CROSS, RGB(255, 255, 0));
break;
case 2:
m_brush.CreatePatternBrush(&m_bmp);
break;
default:
break;
}
Invalidate(TRUE);                //使当前窗口失效,以便重新绘制窗口,使改变生效。
}

你可能感兴趣的:(CBursh 画刷改变背景色)