windows 如何使用不透明的子控件(或子窗口)创建透明窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785332/
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 12:20:53  来源:igfitidea点击:

How to create a transparent window with non-transparent child controls (or child windows)?

windowswinapiwindowtransparent

提问by Manoj Awasthi

First, I'm working on Win32 with C++.

首先,我正在使用 C++ 处理 Win32。

I have been trying to implement a transparent window with a child window which should remain 100% opaque. It seems that child controls cannot have opacity better(lower) than the parent and if I make my parent 100% transparent then my child control also inherits the transparency.

我一直在尝试实现一个带有子窗口的透明窗口,该窗口应该保持 100% 不透明。似乎子控件的不透明度不能比父控件更好(更低),如果我让我的父控件 100% 透明,那么我的子控件也会继承透明度。

Code that I use -

我使用的代码 -

SetLayeredWindowAttributes(GetParent(parentWindowHwnd), 0, 0, LWA_COLORKEY, LWA_ALPHA); 

Any help?

有什么帮助吗?

Please also reply if I am not very clear with my question.

如果我对我的问题不是很清楚,也请回复。

回答by Maurice Flanagan

You can't do this unfortunately, child windows always have the same opacity as their parent. The Google Desktop Toolbar had a neat trick to give the illusion of an opaque textbox on a translucent background. They created two top level windows, one of the background and one for the textbox. Then they set the background window as the owner (not the parent) of the texbox. They then set the background as transparent. It's quite a bit of work to get right, but it's the only way to do it without rendering the whole thing yourself using UpdateLayeredWindow.

不幸的是,您不能这样做,子窗口始终与其父窗口具有相同的不透明度。Google 桌面工具栏有一个巧妙的技巧,可以在半透明背景上产生不透明文本框的错觉。他们创建了两个顶级窗口,一个是背景,一个是文本框。然后他们将背景窗口设置为texbox的所有者(而不是窗口)。然后他们将背景设置为透明。要做到这一点需要做很多工作,但这是唯一一种无需自己使用 UpdateLayeredWindow 渲染整个内容的方法。

回答by binarybob

I'm certain you can do it the other way round, i.e. an opaque main window with a transparent child window, so the reverse may also be true. Have you tried unsettingthe WS_EX_LAYEREDflag for the childwindow and then redrawing it to make it completely opaque again?

我确信你可以反过来做,即一个不透明的主窗口和一个透明的子窗口,所以反过来也可能是正确的。您是否尝试过取消设置窗口的WS_EX_LAYERED标志,然后重新绘制它以使其再次完全不透明?

// Remove WS_EX_LAYERED from the window styles
SetWindowLong(hChildWnd, 
              GWL_EXSTYLE,
              GetWindowLong(hChildWnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);

RedrawWindow(hChildWnd, 
             NULL, 
             NULL, 
             RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);

The MSDN page on Layered Windowsmay also help.

分层 Windows上的 MSDN 页面也可能有所帮助。

回答by Kobi

You cannot make a child control less transparent than its parent. The usual approach here is to give your window an irregular shape: not a rectangle. This is much older that alpha transparency - Think about a clock application: you can make your window circular if you wanted, even on windows 95.
This could be done using the Windows API function SetWindowRgn:
A simple (vb) exampleGoogle results

您不能使子控件的透明度低于其父控件。这里通常的方法是给你的窗口一个不规则的形状:不是矩形。这比 alpha 透明度要古老得多 - 想想一个时钟应用程序:即使在 Windows 95 上,您也可以根据需要使窗口成为圆形。
这可以使用 Windows API 函数来完成SetWindowRgn
一个简单的 (vb)示例Google 结果