WPF应用程序内部WinForms控件上的自定义绘制处理程序
本文关键字:自定义 绘制 处理 程序 应用程序 内部 WinForms 控件 WPF | 更新日期: 2023-09-27 18:23:52
我有一个WPF应用程序,里面托管着一个Windows Form元素,使用以下方法:
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
gMapZoom = new GMap();
gMapZoom.Paint += new PaintEventHandler(gMapZoom_Paint);
host.Child = gMapZoom; // gMapZoom is the Windows Form control
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
然而,我在尝试向其添加自定义Paint事件处理程序时遇到了问题。似乎在WPF中添加它(此处未显示)会导致在WinForm控件下绘制,因此顶部不会显示任何内容。将它添加到WinForm控件中不会有任何作用;绘制事件(gMapZoom_paint)甚至从未被调用。
任何帮助都将不胜感激。
您可以将PaintEventHandler事件添加到Windows窗体控件(gMapZoom)
public event PaintEventHandler OnPaint;
public GMap()
{
InitializeComponent();
this.Paint += new PaintEventHandler(GMap_Paint);
}
void Gmap_Paint(object sender, PaintEventArgs e)
{
OnPaint(this, e);
}
在WPF代码背后:
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
gmap = new GMap();
gmap.OnPaint += new System.Windows.Forms.PaintEventHandler(gmap_Paint);
host.Child = gmap;
this.grid1.Children.Add(host);
}
void gmap_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//Custom paint
}
然后,您可以通过以下方式触发OnPaint事件:
gmap.Invalidate();