如何在原始WinForms控件上实现透明的BackColor?
我快要失去理智了。
为什么复选框控件不会与其后面的内容混合?
该问题适用于所有 WinForms 控件,但我使用它作为示例。
一张图片胜过一千个单词:
还有几句话:CheckBox
后面是彩色 PictureBox
和一个 Button
。CheckBox
的 BackColor
设置为 Transparent
。但不知何故,它决定这意味着它应该共享包含的 Form
的 BackColor
(这是它的透明幻觉的想法吗?)。
这在 WinForms 中不可能吗?我可以发誓我以前这样做过。
更新:
我刚刚尝试过这个:
在该表单上,将 CheckBox
的 BackColor
设置为 Transparent
,然后更改包含的 BackColor
>Form 为其他颜色,CheckBox
将匹配该 BackColor
。什么……?
I'm about to lose my mind here.
Why won't the checkbox control blend with what's behind it?
The question applies to all WinForms controls, but I'm using this as an example.
A picture is worth a thousand words:
And a few more words:
What's behind the CheckBox
are colored PictureBox
es and a Button
.
The CheckBox
's BackColor
is set to Transparent
. But somehow it decides that that means it should share the BackColor
of the containing Form
(is that its idea of the illusion of transparency?).
Is this not possible in WinForms? I could swear I did this before.
UPDATE:
I just tried this:
On that form, set the CheckBox
's BackColor
to Transparent
, then change the BackColor
of the containing Form
to some other color, and the CheckBox
will match that BackColor
. What the.......?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 Windows 窗口控件的副作用。窗口负责绘制自身,OnPaintBackground 和 OnPaint 方法负责处理。
该渲染模型不能很好地支持透明度。通过使用分层窗口支持真正的透明度。这是由视频适配器实现的,Windows 使用它的硬件覆盖功能。但这仅适用于顶级窗口,不适用于子窗口。请注意 Form.Opacity 和 Form.TransparencyKey 属性。
通过技巧来部分支持透明度。控件可以通过要求父窗口首先在控件窗口内绘制自身来伪造它。这会产生背景像素,然后可以在其上进行绘制。将 BackColor 属性设置为 Color.Transparent 可以为支持此功能的控件启用此技巧。所有 ButtonBase 派生类都这样做。但不是本机 Windows 控件的包装控件。
“询问父窗口”是这个技巧中的缺陷在屏幕截图中显现出来的地方。您正在看到表单像素。堆叠效果不起作用,它从不考虑 Z 顺序中的任何中间窗口,只考虑父窗口。这是可以修复的,但非常难看,有一篇知识库文章显示了代码。
另外值得注意的是 WPF 没有此限制。控件不是窗口,它们通过将自身绘制在父级之上来呈现。油漆层。透明度现在是微不足道的,只是不要绘画。
This is a side effect of controls being Windows windows. A window is responsible for drawing itself, the OnPaintBackground and OnPaint methods take care of that.
This rendering model doesn't support transparency well. There is support for true transparency by using layered windows. That's implemented by the video adapter, Windows uses it hardware overlay feature. But that only works for toplevel windows, not child windows. Note the Form.Opacity and Form.TransparencyKey properties.
There's partial support for transparency through a trick. A control can fake it by asking the parent window to draw itself first inside the control window. That produces the background pixels, it can then draw on top of that. Setting the BackColor property to Color.Transparent enables this trick for controls that support this. All of the ButtonBase derived classes do. But not controls that are wrappers for native Windows controls.
"Asking the parent window" is where the flaw in this trick becomes visible in your screen shot. You are seeing the form pixels. Stacking effects don't work, it never considers any intermediary window in the Z-order, only the parent. This is fixable but very ugly, there's a KB article that show the code.
Also notable is that WPF doesn't have this restriction. Controls are not windows, they render by painting themselves on top of the parent. Layers of paint. Transparency is now trivial, just don't paint.
Bob Powell 写了一篇关于透明控件的优秀文章。查看:
https://web.archive。 org/web/20141227200000/http://bobpowell.net/transcontrols.aspx
Bob Powell has written an excellent article about transparent controls. Check it out:
https://web.archive.org/web/20141227200000/http://bobpowell.net/transcontrols.aspx
您可以手动将复选框的背景颜色设置为您想要的颜色吗? (其后面图片框中的值)
“透明”可能意味着与您想要的 MS 不同的东西。
另外,尝试更改图片框的 zorder(置于前面)并查看是否会更改复选框的基础颜色。
Can you set the backcolor of the checkbox manually to the color you want? (The value in the picturebox behind it)
'Transparent' may mean something different from what you want to MS.
Also, try changing the zorder of the pictureboxes (bring to front) and see if that changes the checkbox's underlying color.