[英]Custom Paint handler on a WinForms Control inside a WPF application
我有一个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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.