在派生控件中覆盖CreateHandle方法

时间:2018-04-28 13:23:06

标签: c# winforms controls

让我们有一个第三方DLL,它创建一个本机窗口并返回它的句柄。

我希望通过MyControl类在C#WinForms应用程序中使用此功能,该类基于System.Windows.Forms.Control。

如何正确覆盖从Control派生的类中的虚拟保护方法Control.CreateHandle?

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WinFormsApp {
  public class MyControl : Control {

    [DllImport("MyLib.dll")]
    static extern IntPtr CreateWindow();

    protected override void CreateHandle() {

      // Did to need do invoke a base.CreateHandle()?
      // However, it creates Control.Handle it self.

      var handle = CreateWindow();

      // How to associate returned handle with MyControl instance?

      (WindowTarget as NativeWindow)?.AssignHandle(handle);
    }
  }
}

0 个答案:

没有答案