簡體   English   中英

WPF應用程序中WinForms控件上的自定義畫圖處理程序

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM