wpf WindowsFormsHost上加控件的方法
作者:互联网
方法1:使用多个WindowsFormsHost,后面的会叠加到前面的上面
<Window x:Class="WpfApplication1.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:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="100" Width="225">
<Grid>
<WindowsFormsHost>
<wf:Label Text="ABCDEFGHIJKLMN"/>
</WindowsFormsHost>
<WindowsFormsHost HorizontalAlignment="Left" VerticalAlignment="Top" >
<ElementHost>
<Label Background="Red" Content="XXX"/>
</ElementHost>
</WindowsFormsHost>
</Grid>
</Window>
方法2:窗口叠加的方法
WindowsFormsHost是WPF中承载windows form类型的控件,它的优先级特别高,在同一个窗口上的其他类型控件都能被它盖在下边。
假定有两个wpf的窗口:MainWindow 和FloatWindow,其中MainWindow是承载WindowsFormsHost的窗口,FloatWindow是上边浮动的窗口。
在MainWindow的Loaded事件里初始化浮动窗口,并实现窗口尺寸大小和位置变化,详细见如下代码:
_floatWin = new FloatWindow(this);
_floatWin.Owner = this;
_floatWin.ShowInTaskbar = false;
_floatWin.Show();
_floatWin.Left = this.Left;
_floatWin.Top = this.Top;
_floatWin.Width = this.Width;
_floatWin.Height = this.Height;
以上就可以让浮动窗口和底下的窗口保持大小和位置一致,之后设置浮动窗口的透明效果;
此外还需要在浮动窗口的Xaml里设置主窗口的以下这两个属性:
AllowsTransparency="True" Background="Transparent"
之后上边浮动窗口就可以随意添加控件,设置控件的Opacity属性,就可以实现控件半透明。
标签:浮动,控件,WindowsFormsHost,窗口,上加,FloatWindow,wpf,floatWin 来源: https://blog.csdn.net/technologyleader/article/details/112654590