重载 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;}

评论