WPF 窗口透明度(包括非客户端区域)

发布于 2024-10-14 07:35:58 字数 1235 浏览 9 评论 0原文

有没有办法使 WPF 窗口透明而不丢失非客户区域(边框、标题栏、关闭/最小化/最大化按钮)?

将“AllowsTransparency”设置为“true”需要将“WindowStyle”设置为“None”(如 此答案),这会删​​除非客户区域。

一位 WPF 开发人员 发表了博客关于透明窗口在 WPF 中如何工作,以及为什么很难实现对非客户区透明度的支持。

无论您的窗口样式如何,透明 WPF 窗口都没有任何可见的非客户区域。这对于许多旨在创建自定义窗口形状的场景来说很好,但对于只想“淡入”普通窗口的人来说可能会很烦人。

那么,仅使用 WPF 的解决方案似乎是不可能的。

调用本机 SetLayeredWindowAttributes 函数 并传递正如预期的那样,WPF 窗口的句柄和 LWA_ALPHA 没有任何效果。

我能想到的唯一其他方法是在 Win32(或可能是 WinForms)窗口中托管 WPF 内容。但是,我怀疑尝试这样做会导致空域问题

WPF分层窗口在不同操作系统上具有不同的功能... WPF 不支持透明度颜色键,因为 WPF 无法保证渲染您请求的确切颜色,特别是在硬件加速渲染时。

我不确定我是否正确阅读了上面的内容,但听起来尝试托管具有透明度的 WPF 内容是不可能的。

有什么想法吗?

透明 Notepad2 窗口

Is there any way to make a WPF window transparent without losing the non-client area (borders, title bar, close/minimise/maximise buttons)?

Setting 'AllowsTransparency' to 'true' requires that 'WindowStyle' be set to 'None' (as explained in this answer), which removes the non-client area.

One of the WPF developers blogged about how transparent windows work in WPF, and why it would have been difficult to implement support for non-client area transparency.

No matter what your window styles may suggest, transparent WPF windows do not have any visible non-client area. This is fine for many scenarios where the intent is to create a custom window shape, but it can be annoying for people who just want to "fade in" a normal window.

A WPF-only solution, then, seems out of the question.

Calling the native SetLayeredWindowAttributes function and passing a WPF window's handle and LWA_ALPHA has no effect, as expected.

The only other approach I can think of is hosting WPF content within a Win32 (or possibly WinForms) window. I suspect trying to do this will result in airspace issues, however.

WPF layered windows have different capabilities on different operating systems ...
WPF does not support transparency color keys, because WPF cannot guarantee to render the exact color you requested, particularly when rendering is hardware-accelerated.

I'm not sure if I'm reading the above correctly, but it sounds like trying to host WPF content featuring transparency is not possible.

Any ideas?

Transparent Notepad2 Window

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

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

发布评论

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

评论(3

初懵 2024-10-21 07:35:58

您可以使用 ms 中名为 WPF Chrome 的库自定义非客户端区域。查看这篇文章:'自定义窗口 Chrome'

使用这个库,您可以使 chrome 透明并更改更多内容。

You can customize non-client area using library from ms called WPF Chrome. Check this article: 'Custom Window Chrome'.

With this library you can make chrome transparent and change many more things.

总以为 2024-10-21 07:35:58

Fluidkit 具有玻璃窗的实现,我认为这就是您所追求的。

Fluidkit has an implmentation of a glass window, which I think is what you're after.

人心善变 2024-10-21 07:35:58

实现此目的的唯一方法是使用 setWindowCompositionAttribute() 方法并将其设置为无效状态。

看看这段代码: https://github.com/riverar/sample-win32- acrylblur

而不是设置composition属性将

ACCENT_ENABLE_ACRYLICBLURBEHIND

其设置为

ACCENT_INVALID_STATE

它将使客户区完全透明。

注意:SetWindowCompositionAttribute() 是未记录的 API 的一部分。它可以随时更改。

The only way to achieve this is by using setWindowCompositionAttribute() method and setting it to invalid state.

Just take a look at this code: https://github.com/riverar/sample-win32-acrylicblur

Instead of setting composition attribute to

ACCENT_ENABLE_ACRYLICBLURBEHIND

set it to

ACCENT_INVALID_STATE

It will make the client area completely transparent.

Note: the SetWindowCompositionAttribute() is a part of undocumented API. It can be changed any time.

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