解決透明static控制項重疊問題

來源:互聯網
上載者:User

 

  1. HBRUSH CStadus::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)    
  2. {   
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);   
  4.        
  5.     // TODO: Change any attributes of the DC here
      
  6.     if (nCtlColor == CTLCOLOR_STATIC)   
  7.     {   
  8.         pDC->SetTextColor(RGB(0, 0, 0));//設定成你背景的顏色
      
  9.         pDC->SetBkMode(0);//透明   
  10.         return (HBRUSH)::GetStockObject(NULL_BRUSH);   
  11.     }   
  12.     // TODO: Return a different brush if the default is not desired
      
  13.     return hbr;   
  14. }  
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()
在控制項需要改變文字的代碼後面加入這個函數即可,如下:

  1. GetDlgItem(IDC_STATIC)->SetWindowText("your string");   
  2. GetDlgItem(IDC_STATIC)->GetParent()->RedrawWindow();   
GetDlgItem(IDC_STATIC)->SetWindowText("your string");GetDlgItem(IDC_STATIC)->GetParent()->RedrawWindow(); 

這個方法比較奏效,但是有時候視窗重新整理太頻繁,一閃一閃,效果不太好。幸好有高人指點,可以用局部重新整理來實現。
方法2:局部重新整理
可以自訂一個函數如下: void YourDlg::RefreshControl(UINT uCtlID)   

  1. {      
  2.     CRect   rc;      
  3.     GetDlgItem(uCtlID)->GetWindowRect(&rc);    
  4.     ScreenToClient(&rc);      
  5.     InvalidateRect(rc);      
  6. }     
void YourDlg::RefreshControl(UINT uCtlID){   CRect   rc;   GetDlgItem(uCtlID)->GetWindowRect(&rc); ScreenToClient(&rc);   InvalidateRect(rc);   }   

每次改變控制項內容後調用下這個函數即可,這個方法比較推薦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.