如何在原始WinForms控件上实现透明的BackColor?

发布于 2024-10-05 07:22:05 字数 698 浏览 2 评论 0原文

我快要失去理智了。
为什么复选框控件不会与其后面的内容混合?
该问题适用于所有 WinForms 控件,但我使用它作为示例。

一张图片胜过一千个单词:

alt text

还有几句话:
CheckBox 后面是彩色 PictureBox 和一个 Button
CheckBoxBackColor 设置为 Transparent。但不知何故,它决定这意味着它应该共享包含的 FormBackColor (这是它的透明幻觉的想法吗?)。

这在 WinForms 中不可能吗?我可以发誓我以前这样做过。

更新:
我刚刚尝试过这个:
在该表单上,将 CheckBoxBackColor 设置为 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:

alt text

And a few more words:
What's behind the CheckBox are colored PictureBoxes 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2024-10-12 07:22:05

这是 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.

匿名。 2024-10-12 07:22:05

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

太阳公公是暖光 2024-10-12 07:22:05

您可以手动将复选框的背景颜色设置为您想要的颜色吗? (其后面图片框中的值)

“透明”可能意味着与您想要的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文