Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a CStatic control (MFC) transparent?

My application has a start dialog with an image which fills the whole dialog. Additionaly there is a CStatic control, which displays some variable information for the user. I made the CStatic control transparent with 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 the static control with GetDlgItem(IDC_STATIC_INFO)->SetWindowText, the new text overlaps the old text (the old text is not deleted). I have tried to repaint the background befor calling SetWindowText image with GetDlgItem(IDC_STATIC_BILD)->Invalidate(), but then no info text is shown (neither the old nor the new).

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

Thanks for your help!

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

GetDlgItem(IDC_STATIC_INFO)->SetWindowText(tmp);
CRect rect;
GetDlgItem(IDC_STATIC_INFO)->GetWindowRect(&rect);
ScreenToClient(&rect);
InvalidateRect(&rect);
UpdateWindow();
like image 530
Christian Ammer Avatar asked Apr 06 '11 09:04

Christian Ammer


1 Answers

Hi you can find transparent static sample here

like image 51
Sanja Melnichuk Avatar answered Nov 02 '22 14:11

Sanja Melnichuk