How to implement the System.Windows.Forms.Control derived class correctly, which uses its own mechanism for creating a window handle?
Let's say there is a third-party library that creates a Windows window and returns a handle to it.
And then bind the resulting Windows window handle to the control (i.e., save it with NativeWindow/Handle )?
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() { // Здесь нужно обязательно вызвать base.CreateHandle() // для изменения private field state, // но он сам создает handle? var handle = CreateWindow(); // Почему-то происходит повторный вход в CreateHandle // в момент вызова CreateWindow, // вероятно из Control/NativeWindow WndProc. // Как теперь ассоциировать полученный handle с MyControl, // т. е. что-то вроде: (WindowTarget as NativeWindow)?.AssignHandle(handle); } } }
Controlif you are trying to wipe its descriptor. - LLENN