修改view类的背景色

若要改变CView,CFrameWnd或CWnd对象的背景颜色需要处理WM_ERASEBKGND消息,下面就是一个范例代码:

 1 BOOL   CSampleView::OnEraseBkgnd(CDC*   pDC)   
 2 {   
 3 
 4     //设置brush为希望的背景颜色 
 5     CBrush   backBrush(RGB(255,   128,   128));   
 6 
 7     //保存旧的brush   
 8     CBrush*   pOldBrush   =   pDC-> SelectObject(&backBrush);   
 9     CRect   rect;   
10     pDC-> GetClipBox(&rect);   
11 
12     //画需要的区域 
13     pDC-> PatBlt(rect.left,   rect.top,   rect.Width(),   rect.Height(),   PATCOPY);   
14     pDC-> SelectObject(pOldBrush);   
15 
16     return   TRUE; 
17 
18 }   

若要改变CFromView继承类的背景颜色,下面是一个范例代码:

 1 HBRUSH   CMyFormView::OnCtlColor(CDC*   pDC,   CWnd*   pWnd,   UINT   nCtlColor)   
 2 { 
 3 
 4     switch   (nCtlColor)   
 5     {   
 6 
 7     case   CTLCOLOR_BTN:   
 8     case   CTLCOLOR_STATIC:   
 9         {   
10 
11             pDC-> SetBkMode(TRANSPARENT);   
12             //不加任何处理或设置背景为透明 
13 
14         }   
15     case   CTLCOLOR_DLG:   
16         {   
17 
18             CBrush*   back_brush;   
19             COLORREF   color;   
20             color   =   (COLORREF)   GetSysColor(COLOR_BTNFACE);   
21             back_brush   =   new   CBrush(color);   
22             return   (HBRUSH)   (back_brush-> m_hObject); 
23 
24         } 
25 
26     }   
27 
28     return(CFormView::OnCtlColor(pDC,   pWnd,   nCtlColor)); 
29 
30 } 

 

你可能感兴趣的:(修改view类的背景色)