解决透明static控件重叠问题

xiaoxiao2026-05-28  4

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;} 通过上述代码使static控件实现透明效果,但是这时候通过setWindowText()改变static控件的内容时,会发现出现了文字重叠在一起,调用次数越多,重叠越严重,黑成一团。研究了老久,终于找到解决方法 :D [color=cyan]方法1:RedrawWindow()[/color] 在控件需要改变文字的代码后面加入这个函数即可,如下: GetDlgItem(IDC_STATIC)->SetWindowText("your string");GetDlgItem(IDC_STATIC)->GetParent()->RedrawWindow(); 这个方法比较奏效,但是有时候窗口刷新太频繁,一闪一闪,效果不太好。幸好有高人指点,可以用局部刷新来实现。 [color=cyan]方法2:局部刷新[/color] 可以自定义一个函数如下: void YourDlg::RefreshControl(UINT uCtlID){ CRect rc; GetDlgItem(uCtlID)->GetWindowRect(&rc); ScreenToClient(&rc); InvalidateRect(rc); } 每次改变控件内容后调用下这个函数即可,这个方法比较推荐。
转载请注明原文地址: https://www.6miu.com/read-5049525.html

最新回复(0)