wpf 指定区域通过句柄调用C++绘制视频

本文来自cjcxjstc的博客,原文地址:http://hi.baidu.com/cjcxjstc/blog/item/87f425a99377adeb1f17a27e.html

 
 
WPF中的HWND
2011-05-12 13:31

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>

WindowsFormsHost在System.Windows.Forms.Integration 命名空间中,添加引用组件名称WindowsFormsIntegration。

版权声明:本文为ajian11原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。