在一个WinForms控制定制油漆处理程序中的WPF应用程序中 [英] Custom Paint handler on a WinForms Control inside a WPF application

查看:165
本文介绍了在一个WinForms控制定制油漆处理程序中的WPF应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序里面承载的Windows窗体元素,使用这种方法:

I have a WPF application with a Windows Form element hosted inside it, using this method:

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)是从来没有所谓。

However, I'm having problems trying to add a custom Paint event handler to it. 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. Adding it to the WinForm control does nothing whatsoever; the paint event (gMapZoom_Paint) is never even called.

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

推荐答案

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

You can add a PaintEventHandler event to your Windows Form Control (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的背后code:

In WPF code behind:

{
  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事件是:

Then you can trigger the OnPaint event by:

gmap.Invalidate();

这篇关于在一个WinForms控制定制油漆处理程序中的WPF应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆