在 C++ 中创建透明窗口Win32
我正在创建一个非常简单的 Win32 C++ 应用程序,其唯一目的是仅显示半透明的 PNG。窗口不应该有任何镶边,并且所有不透明度都应该在 PNG 本身中控制。
我的问题是,当窗口下的内容发生变化时,窗口不会重新绘制,因此 PNG 的透明区域与应用程序最初启动时窗口下的内容“粘在一起”。
这是我设置新窗口的行:
hWnd = CreateWindowEx(WS_EX_TOPMOST, szWindowClass, szTitle, WS_POPUP, 0, height/2 - 20, 40, 102, NULL, NULL, hInstance, 0);
对于对 RegisterClassEx 的调用,我将其设置为背景:
wcex.hbrBackground = (HBRUSH)0;
这是我的 WM_PAINT 消息处理程序:
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
Gdiplus::Graphics graphics(hdc);
graphics.DrawImage(*m_pBitmap, 0, 0);
EndPaint(hWnd, &ps);
break;
}
需要注意的一件事是应用程序始终停靠在屏幕的左侧,并且不动。但是,当用户打开、关闭或移动应用程序下面的窗口时,应用程序下面的内容可能会发生变化。
当应用程序第一次启动时,它看起来很完美。 PNG 的透明(和半透明)部分完美地显示出来。但是,当应用程序下面的背景发生变化时,背景不会更新,它只是与应用程序首次启动时保持不变。事实上,当背景改变时,WM_PAINT(或WM_ERASEBKGND不会被调用)。
我已经玩了很长一段时间了,已经接近 100% 正确,但还没有完全达到。例如,我尝试将背景设置为 (HBRUSH) NULL_BRUSH,并尝试处理 WM_ERASEBKGND。
当窗口下面的内容发生变化时,可以做什么来让窗口重新绘制?
I'm creating what should be a very simple Win32 C++ app whose sole purpose it to ONLY display a semi-transparent PNG. The window shouldn't have any chrome, and all the opacity should be controlled in the PNG itself.
My problem is that the window doesn't repaint when the content under the window changes, so the transparent areas of the PNG are "stuck" with what was under the window when the application was initially started.
Here's the line where I setup the new window:
hWnd = CreateWindowEx(WS_EX_TOPMOST, szWindowClass, szTitle, WS_POPUP, 0, height/2 - 20, 40, 102, NULL, NULL, hInstance, 0);
For the call to RegisterClassEx, I have this set for the background:
wcex.hbrBackground = (HBRUSH)0;
Here is my handler for WM_PAINT message:
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
Gdiplus::Graphics graphics(hdc);
graphics.DrawImage(*m_pBitmap, 0, 0);
EndPaint(hWnd, &ps);
break;
}
One thing to note is that the application is always docked to the left of the screen and doesn't move. But, what's underneath the application may change as the user opens, closes or moves windows under it.
When the application first starts, it looks perfect. The transparent (and simi-transparent) parts of the PNG show through perfectly. BUT, when the background underneath the application changes, the background DOESN'T update, it just stays the same from when the application first started. In fact, WM_PAINT (or WM_ERASEBKGND does not get called when the background changes).
I've been playing with this for quite a while and have gotten close to getting 100% right, but not quite there. For instance, I've tried setting the background to (HBRUSH) NULL_BRUSH and I've tried handling WM_ERASEBKGND.
What can be done to get the window to repaint when the contents under it changes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用本系列第 1 部分和第 2 部分中的代码,我能够准确地完成我想要的操作:
这些博客文章讨论的是在 Win32 C++ 中显示启动屏幕,但它与我需要做的几乎相同。我相信我缺少的部分是,我需要使用
UpdateLayeredWindow
函数具有正确的BLENDFUNCTION
参数。我将粘贴下面的 SetSplashImage 方法,该方法可以在上面链接的第 2 部分中找到:I was able to do exactly what I wanted by using the code from Part 1 and Part 2 of this series:
Those blog posts are talking about displaying a splash screen in Win32 C++, but it was almost identical to what I needed to do. I believe the part that I was missing was that instead of just painting the PNG to the window using GDI+, I needed to use the
UpdateLayeredWindow
function with the properBLENDFUNCTION
parameter. I'll paste the SetSplashImage method below, which can be found in Part 2 in the link above:使用 SetLayeredWindowAttributes 存档功能,这允许您设置遮罩颜色它将变得透明,从而使背景显示出来。
您还需要使用分层标志配置您的窗口,例如:
之后就相当简单了:
当您的 PNG 包含要与背景混合的半透明像素时,这会变得更加复杂。您可以尝试查看这篇 CodeProject 文章中的方法:
Cool, Semi-transparent and Shaped Dialogs with Windows 2000 及更高版本的标准控件
Use the SetLayeredWindowAttributesarchive function, this allows you to set a mask color that will become transparent, thus allowing the background to show through.
You will also need to configure your window with the layered flag, e.g.:
After that it's fairly simple:
When your PNG contains semi-transparent pixels that you want to blend with the background, this becomes more complicated. You could try looking at the approach in this CodeProject article:
Cool, Semi-transparent and Shaped Dialogs with Standard Controls for Windows 2000 and Above