WPF 窗口句柄handle

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。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值