欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【WPF】获取控件句柄

程序员文章站 2022-07-13 22:39:23
...

一、说明

WPF与WinForm的本质区别在于 WPF只有窗口句柄,没有控件句柄
以下方法都是获取WPF的窗体句柄

((HwndSource)PresentationSource.FromDependencyObject(pb)).Handle
((HwndSource)PresentationSource.FromVisual(pb)).Handle
IntPtr hwnd = new WindowInteropHelper(this).Handle;

二、解决方案

引入Window Form 控件进行绘图,如 wf:PictureBox 引入 winFrom的PictureBox控件

<UserControl
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
    xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls">
    <Grid HorizontalAlignment="Center" Width="200">
        <Grid.RowDefinitions>
            <RowDefinition Height="200" />
        </Grid.RowDefinitions>
        <Border Grid.Row="0" >
            <WindowsFormsHost x:Name="windowsFormsHost" Background="Transparent">
                <wf:PictureBox  x:Name="pictureBoxHost1" Paint="pictureBoxHost_Paint" RegionChanged="pictureBoxHost_RegionChanged" />
            </WindowsFormsHost>
        </Border>
    </Grid>
</UserControl>
相关标签: WPF