C# 覆盖派生控件中的CreateHandle方法
让我们有一个第三方DLL来创建本机窗口并返回它的句柄 我想通过基于System.Windows.Forms.Control的MyControl类在C#WinForms应用程序中使用此功能 如何在从控件派生的类中正确覆盖受虚拟保护的方法Control.CreateHandleC# 覆盖派生控件中的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
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
。请看。但我可能错了。