一、说明
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>