如何设置有背景颜色的文本

清泛编译
49.如何设置有背景颜色的文本
(1)[解决方法]
  用到了CDC::SetBkMode();
 
 [程序实现] 
  void CMyView::OnDraw(CDC* pDC)
  {
    CMyDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    CRect rcView;//加這兩句
    GetClientRect(rcView);
    // TODO: add draw code for native data here
    CString str (_T("Perfect Text...")); 
    pDC->SetBkMode(TRANSPARENT); 
    rcView.OffsetRect (1,1); 
    pDC->SetTextColor(RGB (0,0,0)); 
    pDC->DrawText(str,str.GetLength(),rcView,DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
    rcView.OffsetRect(-1,-1); 
    pDC->SetTextColor(RGB (255,0,0)); 
    pDC->DrawText(str,str.GetLength(),rcView,DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
  }
(2) 建立名为My的SDI或MDI,并响应WM_ERASEBKGND.
  BOOL CMyView::OnEraseBkgnd(CDC* pDC) 
  {
   // TODO: Add your message handler code here and/or call default
   CBrush Brush (RGB(114,147,171)); 
   // Select the brush into the device context . 
   CBrush* pOldBrush = pDC->SelectObject(&Brush); 
   // Get the area that needs to be erased . 
   CRect ViewClip; 
   pDC->GetClipBox(&ViewClip); 
   //Paint the area. 
   pDC->PatBlt(ViewClip.left,ViewClip.top,ViewClip.Width(),ViewClip.Height(),PATCOPY); 
   //Unselect brush out of device context . 
   pDC->SelectObject (pOldBrush ); 
   // Return nonzero to half fruther processing . 

   return TRUE;
   return CView::OnEraseBkgnd(pDC);
  }
  此方法也适合基类是EditView的SDI或MDI的情况,但是字体的颜色和底色不行.建议用WM_CTLCOLOR.

本文导航

MFC 总结 技巧

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