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();
- GC.Collect循环?
- 为什么我的应用程序设置没有持久化?
- 方法重载 – 好的还是坏的设计?
- 将javascript呈现的svg图像保存为本地磁盘为.png文件
- 无法转换’NHibernate.Collection.Generic.PersistentGenericBag’类型的对象
- 使用命令行构建VS安装项目时为什么没有输出?
- Image.Save()抛出exception“Value不能为null ./r/nParameter name:encoder”
- 计算Windows文件夹大小的最快方法是什么?
- 当使用.NET Newtonsoft.Json组件反序列化一些有效的json时,为什么我的POCO中的所有集合都为null