- HBRUSH CStadus::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
-
- // TODO: Change any attributes of the DC here
- if (nCtlColor == CTLCOLOR_STATIC)
- {
- pDC->SetTextColor(RGB(0, 0, 0));//設定成你背景的顏色
- pDC->SetBkMode(0);//透明
- return (HBRUSH)::GetStockObject(NULL_BRUSH);
- }
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }
HBRUSH CStadus::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);// TODO: Change any attributes of the DC hereif (nCtlColor == CTLCOLOR_STATIC){pDC->SetTextColor(RGB(0, 0, 0));//設定成你背景的顏色pDC->SetBkMode(0);//透明return (HBRUSH)::GetStockObject(NULL_BRUSH);}// TODO: Return a different brush if the default is not desiredreturn hbr;}
通過上述代碼使static控制項實現透明效果,但是這時候通過setWindowText()改變static控制項的內容時,會發現出現了文字重疊在一起,調用次數越多,重疊越嚴重,黑成一團。研究了老久,終於找到解決方案
方法1:RedrawWindow()
在控制項需要改變文字的代碼後面加入這個函數即可,如下:
- GetDlgItem(IDC_STATIC)->SetWindowText("your string");
- GetDlgItem(IDC_STATIC)->GetParent()->RedrawWindow();
GetDlgItem(IDC_STATIC)->SetWindowText("your string");GetDlgItem(IDC_STATIC)->GetParent()->RedrawWindow();
這個方法比較奏效,但是有時候視窗重新整理太頻繁,一閃一閃,效果不太好。幸好有高人指點,可以用局部重新整理來實現。
方法2:局部重新整理
可以自訂一個函數如下: void YourDlg::RefreshControl(UINT uCtlID)
- {
- CRect rc;
- GetDlgItem(uCtlID)->GetWindowRect(&rc);
- ScreenToClient(&rc);
- InvalidateRect(rc);
- }
void YourDlg::RefreshControl(UINT uCtlID){ CRect rc; GetDlgItem(uCtlID)->GetWindowRect(&rc); ScreenToClient(&rc); InvalidateRect(rc); }
每次改變控制項內容後調用下這個函數即可,這個方法比較推薦。