MFC对话框static、radio和checkbox背景透明

一般是这么做

1.对话框添加两个消息处理。

afx_msg BOOL OnEraseBkgnd(CDC* pDC);  
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);  

 

2.这样来写具体实现

View Code
 1 BOOL CConfigDlg::OnEraseBkgnd(CDC* pDC)
 2 {
 3     CRect rt;
 4     GetClientRect( &rt);
 5     HDC hdc = pDC->GetSafeHdc();
 6     HDC hMemdc = CreateCompatibleDC( hdc);
 7     HBITMAP hMembmp = CreateCompatibleBitmap( hdc, rt.Width(), rt.Height());
 8     HBITMAP hOldbmp = (HBITMAP)SelectObject( hMemdc, hMembmp);
 9     Graphics graphics(hMemdc);
10     graphics.Clear( Color(254,190,216,233));
11     BitBlt( hdc, 0,0,rt.Width(), rt.Height(), hMemdc, 0,0,SRCCOPY);
12     graphics.ReleaseHDC( hMemdc);
13     SelectObject( hMemdc, hOldbmp);
14     DeleteDC( hMemdc);
15     DeleteObject( hMembmp);
16      return TRUE;
17 }
View Code
 1 HBRUSH CConfigDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
 2 {
 3     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 4 
 5     switch (pWnd->GetDlgCtrlID())
 6     {
 7     case IDC_RADIO_R1:
 8         pDC->SetBkMode(TRANSPARENT);//背景透明
 9         //pDC->SetTextColor(RGB(255,0,0)); //字体颜色
10         return   HBRUSH(GetStockObject(NULL_BRUSH));
11     case IDC_RADIO_R2:
12         pDC->SetBkMode(TRANSPARENT);
13         return   HBRUSH(GetStockObject(NULL_BRUSH));
14     case IDC_RADIO_R3:
15         pDC->SetBkMode(TRANSPARENT);
16         return   (HBRUSH)GetStockObject(NULL_BRUSH);
17     case IDC_CHECK_OPEN:
18         pDC->SetBkMode(TRANSPARENT);
19         return   (HBRUSH)GetStockObject(NULL_BRUSH);
20     default:
21         pDC->SetBkMode(TRANSPARENT);
22         return   (HBRUSH)GetStockObject(NULL_BRUSH);
23     }
24 
25 //     if (( nCtlColor == IDC_RADIO_R1 )||( nCtlColor == IDC_RADIO_R2 )||( nCtlColor == IDC_RADIO_R3 )||( nCtlColor == IDC_CHECK_OPEN )) 
26 //这种写法不行
27 //调试的时候可以看出来 nCtlColor 不会等于 IDC_RADIO_R1 IDC_RADIO_R2以及IDC_RADIO_R3等
28 //     {
29 //         pDC->SetBkMode(TRANSPARENT);
30 //         pDC->SetTextColor(RGB(255,255,0));
31 //         //return (HBRUSH)::GetStockObject( NULL_BRUSH);
32 //         return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
33 //     }
34     return hbr;
35 }

我这里有3个radio ---> IDC_RADIO_R1,IDC_RADIO_R2,IDC_RADIO_R3和一个checkbox ---> IDC_CHECK_OPEN

一般情况下,这几种控件就是透明的。

但是在windows xp使用xp风格主题下,checkbox会变成黑色背景。如果NULL_BRUSH换成GRAY_BRUSH,checkbox会变成灰色背景。

所以这里可以定义一个和背景色相同的画刷,来替换NULL_BRUSH。

或者将checkbox的chaption设为空,后面放一个static文本框来写checkbox的chaption。

posted @ 2013-04-11 15:49  水上云天  阅读(3808)  评论(0编辑  收藏  举报