目录
介绍
在WPF中,如果你需要在WPF中使用Winform控件,你将需要使用WindowsFormsHost
作为宿主,具体的原理是,WindowsFormsHost
在当前的位置以及尺寸,在占用一块区域叠加Winform控件。这里具体可以参考HwndHost
.
问题
问题:在WPF半透明窗口中显示WindowsformHost
-
思路1:
AllowTransparent
在WPF中,窗口最常见的透明方案是通过AllowTransparent=True
来达到窗口半透明。这个方法会导致在WindowsFormHost
中根据句柄播放视频没有画面。 -
思路2:使用
WindowChrome
通过查阅了大佬们的文章:
WPF 制作高性能的透明背景异形窗口(使用 WindowChrome 而不要使用 AllowsTransparency=True) - walterlv
使用 WPF 做一个可以逼真地照亮你桌面的高性能阳光 - walterlv
基于林德熙与walterlv两位大佬提出半透明背景方法, 并组合通过WindowsFormHost
能够使出现神奇的效果。
方案
WindowsFormHost黑色不透明 与 窗口颜色叠加
WindowsFormsHost.Background ='Transparent'
<Window x:Class="HighPerformanTransparentWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
ResizeMode="NoResize"
WindowStyle="None"
Background="#00FFFFFF"
AllowsTransparency="False"
Title="WindowChrome测试"
Height="420" Width="640">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="-1" />
</WindowChrome.WindowChrome>
<Border Background="#8080D8D8" CornerRadius="20">
<WindowsFormsHost Background="Transparent" Margin="20">
<wf:Panel x:Name="panel"/>
</WindowsFormsHost>
</Border>
</Window>
WindowsFormHost黑色 不透明 与 半透明窗口颜色不叠加
WindowsFormsHost.Background ='#00FFFFFF'
任意透明的颜色值,都将为黑色
wf:Panel.BackColor="Transparent"
<Window x:Class="HighPerformanTransparentWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
ResizeMode="NoResize"
WindowStyle="None"
Background="#00FFFFFF"
AllowsTransparency="False"
Title="WindowChrome测试"
Height="420" Width="640">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="-1" />
</WindowChrome.WindowChrome>
<Border Background="#8080D8D8" CornerRadius="20">
<WindowsFormsHost Background="#00FFFFFF" Margin="20">
<wf:Panel x:Name="panel" BackColor="#FF0000"/>
</WindowsFormsHost>
</Border>
</Window>
WindowsFormHost 下的Winform控件半透明
WindowsFormsHost.Background ='Black'
任意非透明颜色
wf:Panel.BackColor="#80FF0000"
这里透明度取值为01~FE, FE差不多为实色, 如果是FF则会变成一种高反差效果?
<Window x:Class="HighPerformanTransparentWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStyle="None"
Background="{x:Null}"
AllowsTransparency="False"
Title="WindowChrome测试"
Height="420" Width="640">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="-1" />
</WindowChrome.WindowChrome>
<Border Background="#80000000" CornerRadius="20">
<WindowsFormsHost Background="Black" Margin="20">
<wf:Panel x:Name="panel" BackColor="Transparent" />
</WindowsFormsHost>
</Border>
</Window>
WindowsFormHost下的Winform控件半透明反差效果
WindowsFormsHost.Background =‘Black’ ` 任意非透明颜色
wf:Panel.BackColor="#FFFF0000"
or red
这个就补贴代码了,替换上面的颜色值即可!
闪烁
在窗口不透明的时候会WindowsFormsHost
内的控件会出现闪烁。
例如从屏幕边缘拖动窗口回出现下面的效果:
当窗口不透明的时候,基于WindowChrome 修改窗口大小变化也会出现闪烁:
// 基于这种情况可以使用ControlzEx的WindowChromeBehaviors的处理修改窗口闪烁
// 但是从屏幕边缘移动窗口也会出现闪烁,以及窗口不支持半透明显示
<Window xmlns:bev="http://schemas.microsoft.com/xaml/behaviors"
xmlns:cz="clr-namespace:ControlzEx.Behaviors;assembly=ControlzEx"
ResizeMode="NoResize"
WindowStyle="None">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="-1" CaptionHeight="40" NonClientFrameEdges="None" ResizeBorderThickness="1" UseAeroCaptionButtons="False" />
</WindowChrome.WindowChrome>
<bev:Interaction.Behaviors>
<cz:WindowChromeBehavior TryToBeFlickerFree="True" />
</bev:Interaction.Behaviors>
</Window>
总结
总结
可参考如下表:
WF不透明黑色 叠加窗体颜色 | WF不透明黑色 | WF半透明 | WF半透明反差 | |
---|---|---|---|---|
半透明颜色 | N/A | N/A | N/A | 任意 |
WindowsformHost | Transparent or {x:Null} | #00FFFFFF 任意透明值 | Black | Black |
WinformPanel | N/A | N/A | #80FF0000半透明值。 | 任意实色值, Black则为全透明 |
是否闪烁 | 否 | 是 | 是 | 是 |