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); } } } 
  • Control.FromHandle Is this what you were looking for? - LLENN
  • The Control.FromHandle method returns an object of type Control, which created this window and saved a link to itself via the Windows API SetWindowLong) - this is off topic, read the original question carefully! - Mega WEB
  • Judging by your code, it’s not at all clear why you need then Control if you are trying to wipe its descriptor. - LLENN
  • static extern IntPtr CreateWindow () creates a Windows window and returns its handle. In a Windows Forms application, the easiest way to work with this window is through the functionality of the Control class using Handle. The original question: "How to implement a class derived from the System.Windows.Forms.Control that uses its own window descriptor creation mechanism?" - Mega WEB
  • In order to understand how to help you, explain why you want to overwrite the control descriptor that you want to control further? - LLENN 2:41 pm

0