正文

如何使static控件背景透明(MFC)2008-09-11 09:23:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lost/38200.html

分享到:

重载 OnCtrlColor,此方法也可以设置文本框和静态文件背景色,背景色就是那个HBRUSH返回的颜色。 1. 在对话框的头文件中加入 afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); 2. 在对话框的cpp文件中加入  BEGIN_MESSAGE_MAP(CtransparentDlg, CDialog)     ON_WM_CTLCOLOR()     //}}AFX_MSG_MAP END_MESSAGE_MAP()  HBRUSH CtransparentDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {     HBRUSH   hBrush   =   CDialog::OnCtlColor(pDC,   pWnd,   nCtlColor);         if(nCtlColor == CTLCOLOR_STATIC)     {         pDC->SetBkMode(TRANSPARENT);            return   (HBRUSH)::GetStockObject(NULL_BRUSH);        }     return hBrush; }   Extra example: HBRUSH CMydilog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  if(pWnd->GetDlgCtrlID() == IDC_STATIC1) //对指定的控件设属性,你也可以用上面的方法针对所有的标签 {    pDC->SetTextColor(RGB(0,0,0) );//多此一举,你可以设文字其它的颜色    pDC->SetBkMode(TRANSPARENT);    return HBRUSH(GetStockObject(HOLLOW_BRUSH)); }   HBRUSH DlgCountCon::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {   // Call the base class implementation first! Otherwise, it may   // undo what we're trying to accomplish here.   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);    // Are we painting the IDC_MYSTATIC control? We can use   // CWnd::GetDlgCtrlID() to perform the most efficient test.   if (pWnd->GetDlgCtrlID() == IDC_EDIT_state)   {      // Set the text color to red      //pDC->SetTextColor(RGB(0, 255, 0));       // Set the background mode for text to transparent       // so background will show thru.      pDC->SetBkMode(TRANSPARENT);       // Return handle to our CBrush object      return (HBRUSH)GetStockObject(NULL_BRUSH);   }    return hbr;} return hbr;}  

阅读(78) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册