简体   繁体   English

WPF应用程序中WinForms控件上的自定义画图处理程序

[英]Custom Paint handler on a WinForms Control inside a WPF application

I have a WPF application with a Windows Form element hosted inside it, using this method: 我有一个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);

However, I'm having problems trying to add a custom Paint event handler to it. 但是,在尝试向其添加自定义Paint事件处理程序时遇到问题。 It seems that adding it in WPF (not shown here) causes the drawing to be done beneath the WinForm control, so nothing appears on top. 似乎将其添加到WPF中(此处未显示)会导致在WinForm控件下完成绘制,因此顶部没有任何显示。 Adding it to the WinForm control does nothing whatsoever; 将其添加到WinForm控件不会执行任何操作。 the paint event (gMapZoom_Paint) is never even called. 绘画事件(gMapZoom_Paint)从未被调用。

Any help would be much appreciated. 任何帮助将非常感激。

You can add a PaintEventHandler event to your Windows Form Control (gMapZoom) 您可以将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);
 }

In WPF code behind: 在后面的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         
}

Then you can trigger the OnPaint event by: 然后,您可以通过以下方式触发OnPaint事件:

gmap.Invalidate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM