简体   繁体   English

如何使CStatic控件(MFC)透明?

[英]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. 另外,还有一个CStatic控件,该控件为用户显示一些变量信息。 I made the CStatic control transparent with following code: 我使用以下代码使CStatic控件透明:

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). 当我使用GetDlgItem(IDC_STATIC_INFO)->SetWindowText更改静态控件的文本时,新文本与旧文本重叠(不删除旧文本)。 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). 我尝试过用GetDlgItem(IDC_STATIC_BILD)->Invalidate()调用SetWindowText图像重绘背景,但是没有显示任何信息文本(旧的或新的都没有)。

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. 解决方案: Sanja的codeproject-link中的方法2(改编)对我有用

GetDlgItem(IDC_STATIC_INFO)->SetWindowText(tmp);
CRect rect;
GetDlgItem(IDC_STATIC_INFO)->GetWindowRect(&rect);
ScreenToClient(&rect);
InvalidateRect(&rect);
UpdateWindow();

嗨,你可以在这里找到透明的静态样本

This answer is related to the Windows API rather than MFC framework, but the concepts translate easilly: 这个答案与Windows API而不是MFC框架有关,但是这些概念很容易解释:

Correct way to do transparent buttons in WINAPI 在WINAPI中做透明按钮的正确方法

Your problem is that achieving transparent controls using Win32 native controls conflicts with achieving flicker free controls when repainting occurs. 您的问题是,使用Win32本机控件实现透明控件与重新绘制时实现无闪烁控件冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM