How to make transparent CStatic (MFC) transparent?

My application has a start dialog box with an image that fills the entire dialog. In addition, there is a CStatic control that displays some variable information for the user. I made a transparent CStatic using the following code:

HBRUSH CStartbildDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { if(pWnd->GetDlgCtrlID() == IDC_STATIC_INFO) { pDC->SetBkMode(TRANSPARENT); return reinterpret_cast<HBRUSH>(::GetStockObject(NULL_BRUSH)); } else return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } 

When I change the text of a static control using GetDlgItem(IDC_STATIC_INFO)->SetWindowText , the new text overlaps the old text (the old text is not deleted). I tried to redraw the background to call the SetWindowText image using GetDlgItem(IDC_STATIC_BILD)->Invalidate() , but then the info text is displayed (neither old nor new).

Do you know how I can make static control transparent so that I can also override it with new text?

Thank you for your help!

Solution: Method 2 (adapted) from Sanja's codeproject-link worked for me.

 GetDlgItem(IDC_STATIC_INFO)->SetWindowText(tmp); CRect rect; GetDlgItem(IDC_STATIC_INFO)->GetWindowRect(&rect); ScreenToClient(&rect); InvalidateRect(&rect); UpdateWindow(); 
+4
source share
2 answers

Hi, you can find a transparent static sample here.

+3
source

This answer is related to the Windows API, not the MFC card, but the concepts translate easily:

The right way to create transparent buttons in WINAPI

Your problem is that achieving transparent controls using the built-in Win32 controls conflicts with achieving flicker-free controls when redrawing.

0
source

Source: https://habr.com/ru/post/1346884/


All Articles