WPF应用程序内WinForms控件上的自定义绘制处理程序

我有一个WPF应用程序,其中包含一个Windows窗体元素,使用此方法:

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控件不会做任何事情; 甚至从未调用paint事件(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();