win11之透明WPF窗口的不透明
先说重点:
透明的窗口在WIN11上摸到了边缘,会出现一个毛玻璃背景让本身透明的窗口不再透明(仅在win11 触摸设备上出现)
我们都知道在WPF写一个透明窗口,而UI不透明
<Window x:Class="TransparentWindowDemo.MainWindow"
...
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
Loaded="Window_Loaded"
IsHitTestVisible="True"
Title="MainWindow" Height="450" Width="800">
<Grid Background="#01FFFFFF" >
<!-- 中间不透明的方块 -->
<Border Width="200" Height="200"
Background="DodgerBlue"
CornerRadius="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
MouseLeftButtonDown="Border_MouseLeftButtonDown"/>
</Grid>
</Window>
核心就在
AllowsTransparency="True"
Background="Transparent"
由于我需要的是没内容的地方透明但鼠标不穿透,但是有的人可能需要,我就加了假透明 透明度1
<Grid Background="#01FFFFFF" >
上面的窗口不会触发上述说的BUG,但是
public MainWindow()
{
InitializeComponent();
WindowChrome.SetWindowChrome(this,new WindowChrome() { GlassFrameThickness = new Thickness(-1), CaptionHeight = 0 });
}
之后就触摸边缘时出现了不再透明了。
同时推荐一下 WPF界面反编译神器Snoop
开源地址https://github.com/snoopwpf/snoopwpf
其实对比之后发现核心是在
System.Windows.Shell.WindowChrome.SetWindowChrome(this, null);
上面这些都是无边框,且也无法移动标题栏的一类的功能,如果想加这些效果
在一个文章里看到 说是使用WPF.Shell.Integration.Library,
引用文章
追加知识
进一步发现,这样设置也不会出现BUG
var chrome = System.Windows.Shell.WindowChrome.GetWindowChrome(this);
if (chrome != null)
{
chrome.ResizeBorderThickness = new Thickness(0); // 或 very small, 比如 1
WindowChrome.SetWindowChrome(this, chrome);
}

浙公网安备 33010602011771号