Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 覆盖派生控件中的CreateHandle方法_C#_Winforms_Controls - Fatal编程技术网

C# 覆盖派生控件中的CreateHandle方法

C# 覆盖派生控件中的CreateHandle方法,c#,winforms,controls,C#,Winforms,Controls,让我们有一个第三方DLL来创建本机窗口并返回它的句柄 我想通过基于System.Windows.Forms.Control的MyControl类在C#WinForms应用程序中使用此功能 如何在从控件派生的类中正确覆盖受虚拟保护的方法Control.CreateHandle using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WinFormsApp { public

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

我想通过基于System.Windows.Forms.Control的MyControl类在C#WinForms应用程序中使用此功能

如何在从控件派生的类中正确覆盖受虚拟保护的方法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);
    }
  }
}

继承者注意:当在派生类中重写CreateHandle时,请确保调用基类的CreateHandle方法以确保已创建句柄。我知道上述要求。请参阅我的问题中提供的代码模板中的注释。我认为您需要继承
NativeWindow
。请看。但我可能错了。