How to make a CStatic control (MFC) transparent?

16,924

Hi you can find transparent static sample here

Share:
16,924
Christian Ammer
Author by

Christian Ammer

Updated on June 04, 2022

Comments

  • Christian Ammer
    Christian Ammer almost 2 years

    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();