1.获取HWND
.NET WinForm获取窗口句柄很方便,this.Handle搞定。
WPF就有些麻烦了,获取方法如下:
引入命名空间:using System.Windows.Interop;
获取方法:new WindowInteropHelper(this).Handle
this是个Window类的实例
而WPF的程序按照默认的情况下只有一个Window类,即主窗口。那么想在增加子窗口(我的情况是需要一个窗口播放视频,现在已经有一个库实现的视频的解码,但需要传入窗口句柄),可以有多种方法。
2.加入WinForm窗口
<UserControl x:Class=”VideoClient.UCRealVideo”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:host=”clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration”
xmlns:wf=”clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms”
Initialized=”UserControl_Initialized”>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=”20″ />
<RowDefinition Height=”*” />
<RowDefinition Height=”20″ />
</Grid.RowDefinitions>
<Button Name=”button1″ Click=”button1_Click” HorizontalAlignment=”Left” Width=”36″>Button1</Button>
<Button Name=”button2″ HorizontalAlignment=”Right” Width=”34″>Button2</Button>
<WindowsFormsHost Grid.Row=”1″>
<wf:Panel x:Name=”video”/>
</WindowsFormsHost>
<Button Name=”button3″ Grid.Row=”2″ Click=”button1_Click” HorizontalAlignment=”Left” Width=”36″>Button1</Button>
<Button Name=”button4″ Grid.Row=”2″ HorizontalAlignment=”Right” Width=”34″>Button2</Button>
</Grid>
</UserControl>