修改视图背景

清泛编译
75.修改视图背景
How do I change the background color of a view?

To change the background color for a CView, CFrameWnd, or CWnd object, process the WM_ERASEBKGND message. The following code shows how: 

BOOL CSampleView::OnEraseBkgnd(CDC* pDC)
{
  // Set brush to desired background color.
  CBrush backBrush(RGB(255, 128, 128));
  // Save old brush.
  CBrush* pOldBrush = pDC->SelectObject(&backBrush);
  CRect rect;
  pDC->GetClipBox(&rect);   // Erase the area needed.
  pDC->PatBlt(rect.left, rect.top, rect.Width(), 
  rect.Height(), PATCOPY);
  pDC->SelectObject(pOldBrush);
  return TRUE;
}

I solved the problem like this:

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));
}
本文导航

MFC 总结 技巧

分享到:
评论加载中,请稍后...
App Inventor 2 中文网,少儿编程首选平台!
回到顶部