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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5564016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:34:12  来源:igfitidea点击:

How to make a CStatic control (MFC) transparent?

windowswinapimfcdialog

提问by Christian Ammer

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:

我的应用程序有一个开始对话框,其中的图像填充了整个对话框。另外还有一个 CStatic 控件,它为用户显示一些变量信息。我使用以下代码使 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). I have tried to repaint the background befor calling SetWindowTextimage with GetDlgItem(IDC_STATIC_BILD)->Invalidate(), but then no info text is shown (neither the old nor the new).

当我用 更改静态控件的文本时GetDlgItem(IDC_STATIC_INFO)->SetWindowText,新文本与旧文本重叠(旧文本不会被删除)。我试图在使用 调用SetWindowText图像之前重新绘制背景GetDlgItem(IDC_STATIC_BILD)->Invalidate(),但是没有显示任何信息文本(既不是旧的也不是新的)。

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

采纳答案by Sanja Melnichuk

Hi you can find transparent static sample here

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

回答by Chris Becke

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 本机控件实现透明控件与发生重绘时实现无闪烁控件冲突。