// TODO: Change any attributes of the DC here if(nCtlColor==CTLCOLOR_DLG){ hbr=(HBRUSH)m_brush; } // TODO: Return a different brush if the default is not desired return hbr; } m_brush为CYourView的一个成员变量,类型为CBrush 在构造函数中初始化这个变量 m_brush.CreateSolidBrush(RGB(255,0,0));2.把CMainframe窗口的WS_THICKFRAME风格去掉就可
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style &=~(WS_THICKFRAME|WS_MAXIMIZEBOX ); return TRUE; }
HBRUSH CYourView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_DLG){
hbr=(HBRUSH)m_brush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
m_brush为CYourView的一个成员变量,类型为CBrush
在构造函数中初始化这个变量 m_brush.CreateSolidBrush(RGB(255,0,0));2.把CMainframe窗口的WS_THICKFRAME风格去掉就可
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style &=~(WS_THICKFRAME|WS_MAXIMIZEBOX ); return TRUE;
}
能不能给我一段代码?
谢谢
如何改变视图的背景色?
[解答]
你可以通过处理WM_ERASEBKGND消息来改变CView、CFrameWnd或CWnd对象的背景色。请看如下的程序段: BOOL CSampleView::OnEraseBkgnd(CDC* pDC)
{
// 设置所要求背景色的刷子
CBrush backBrush(RGB(255, 128, 128));
// 保存旧刷子
CBrush* pOldBrush = pDC->SelectObject(&backBrush);
CRect rect;
pDC->GetClipBox(&rect); // 擦除所需的区域
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(pOldBrush);
return TRUE;
}
而我则用如下方法解决这个问题:
HBRUSH dlgtest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
switch (nCtlColor)
{
case CTLCOLOR_BTN:
case CTLCOLOR_STATIC:
{
pDC->SetBkMode(TRANSPARENT);
}
case CTLCOLOR_DLG:
{
CBrush* back_brush;
COLORREF color;
color = (COLORREF) GetSysColor(COLOR_BTNFACE);
back_brush = new CBrush(color);
return (HBRUSH) (back_brush->m_hObject);
}
}
return(CFormView::OnCtlColor(pDC, pWnd, nCtlColor));
}
COLORREF color;
color = (COLORREF) GetSysColor(COLOR_BTNFACE);
back_brush = new CBrush(color);
return (HBRUSH) (back_brush->m_hObject);
这样可能会有内存泄露,第一是new完CBrush后没有Delete,第二是GDI对象没有被删除.
界面刷新多次了就惨了