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

C# WPF应用程序内WinForms控件上的自定义绘制处理程序,c#,.net,wpf,winforms,paint,C#,.net,Wpf,Winforms,Paint,我有一个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 =

我有一个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);
但是,我在尝试向其添加自定义绘制事件处理程序时遇到问题。似乎在WPF中添加它(此处未显示)会导致绘图在WinForm控件下完成,因此顶部不会显示任何内容。将其添加到WinForm控件不会产生任何效果;绘制事件(gMapZoom_绘制)甚至从未调用过


非常感谢您的帮助。

您可以将PaintEventHandler事件添加到Windows窗体控件(gMapZoom)中

在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();

您可以将PaintEventHandler事件添加到Windows窗体控件(gMapZoom)

在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();

那真的没用。。这就是我已经在做的。问题是从未调用绘制事件。比如,在你的例子中,GMap_-Paint永远不会被执行。下面是一个例子,我的意思是:在这个例子中,pictureBox_-Paint方法在应用程序第一次加载时被调用一次。之后,即使调用Invalidate(),它也不会重新绘制.hmmm。。。我已经把答案说得更清楚了。它在我的沙箱中工作。我发现了我的问题:Invalidate()从一开始就没有被调用过。事实证明,您放入的任何事件处理程序都必须是WinForms控件的处理程序,而不是包含它的WPF控件的处理程序。因此,在上面的示例中,我必须将事件处理程序添加到pictureBox,而不是grid1。谢谢你的帮助!:)那真的没用。。这就是我已经在做的。问题是从未调用绘制事件。比如,在你的例子中,GMap_-Paint永远不会被执行。下面是一个例子,我的意思是:在这个例子中,pictureBox_-Paint方法在应用程序第一次加载时被调用一次。之后,即使调用Invalidate(),它也不会重新绘制.hmmm。。。我已经把答案说得更清楚了。它在我的沙箱中工作。我发现了我的问题:Invalidate()从一开始就没有被调用过。事实证明,您放入的任何事件处理程序都必须是WinForms控件的处理程序,而不是包含它的WPF控件的处理程序。因此,在上面的示例中,我必须将事件处理程序添加到pictureBox,而不是grid1。谢谢你的帮助!:)