WPF 与 Winforms 中的窗口透明度

发布于 2024-09-13 08:50:54 字数 247 浏览 8 评论 0原文

为什么我必须在 WPF 表单上将 WindowStyle 属性设置为 None 才能获得透明度,但在 Winforms 中我可以在任何表单上执行此操作,并保留边框,标准按钮等?显然 API 支持这一点,所以我不清楚 WPF 有什么特殊之处会导致这个问题。

我猜测 WPF 正在跳过一些 DirectX 或 OpenGL 的障碍,而 Winforms 只是通过 API 设置窗口的 alpha,但我可能大错特错了。

Why is it that I have to set the WindowStyle property to None on a WPF form to get transparency, but in Winforms I can do it on any form, and retain borders, standard buttons, etc? Clearly the API supports this, so I'm not clear on what's special about WPF that would make this an issue.

I'm guessing that WPF is jumping through some DirectX or OpenGL hoops, while Winforms is just setting the alpha for the window via the API, but I could be way off base.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

行雁书 2024-09-20 08:50:54

同意,这很严厉:

   private void VerifyConsistencyWithAllowsTransparency(WindowStyle style)
   {
       if (AllowsTransparency && style != WindowStyle.None)
       {
           throw new InvalidOperationException(SR.Get(SRID.MustUseWindowStyleNone));
       }
   }

WPF 使用与 Windows 窗体(分层窗口)完全相同的机制来实现这一点。没有明显的理由表明它在 WPF 中不能以同样的方式工作。从 Window.cs 中提取的代码片段简单地排除了这一点。然而,UsesPerPixelOpacity 属性有一个提示:

当您启用每像素不透明度时,系统不再绘制非客户区域。这是因为 UsesPerPixelOpacity 的预期目的是显示在互操作场景中工作的非矩形顶级 UI,而显示矩形非客户区域违背了该目的。

我猜是“互操作场景”。

Agreed, this is heavy handed:

   private void VerifyConsistencyWithAllowsTransparency(WindowStyle style)
   {
       if (AllowsTransparency && style != WindowStyle.None)
       {
           throw new InvalidOperationException(SR.Get(SRID.MustUseWindowStyleNone));
       }
   }

WPF uses the exact same mechanism to implement this as Windows Forms, layered windows. There is no obvious reason it wouldn't work the same way in WPF. The code snippet, lifted from Window.cs, simply rules it out. There is however one hint from the UsesPerPixelOpacity property:

When you enable per-pixel opacity, the system no longer draws the non-client area. This is because the intended purpose of UsesPerPixelOpacity is to show non-rectangular top-level UI that works in interoperation scenarios, and showing the rectangular non-client area defeats that purpose.

"interoperation scenarios", I guess.

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