How can I properly manage the form or window of RecreateWnd?

As you know, window handles of window controls (including forms) are considered unstable. That is, setting some properties can lead to the fact that the control will destroy its window handle and then recreate it (there are a bunch of examples of this technique, searching for StdCtrls for RecreateWnd ).

So, when I need to register my HWND with the OS after creation and unregister before destruction, I have to redefine the corresponding pair of methods. A brief overview of Controls and Forms gave me 3 pairs, all of them were virtual:

  • CreateHandle / DestroyHandle
  • CreateWindowHandle / DestroyWindowHandle
  • CreateWnd / DestroyWnd

Are there any more couples? Which pair should I override for the form? And what is the pair for general window control?

+3
delphi vcl
source share
1 answer

Override CreateWnd() and DestroyWnd() in most situations. Override DestroyWnd() to execute registration windows, temporarily save any window-dependent data if the csRecreating flag csRecreating present in the ControlState property and calls inherited DestroyWnd() (which calls DestroyWindowHandle() ) to destroy the HWND. Override CreateWnd() to call the inherited CreateWnd() (which calls CreateWindowHandle() ) to create the HWND, load and discard temporary window-specific data if it was previously saved, and register the windows.

Override CreateWindowHandle() and DestroyWindowHandle() when you need to get / release HWND using other tools besides the usual Win32 calls to CreateWindow/Ex() and DestroyWindow() . For example, TForm overrides them to create / free MDI child windows by sending WM_MDICREATE and WM_MDIDESTROY messages to the WM_MDICREATE window.

I have never seen an override of CreateHandle() and DestroyHandle() for anything useful.

See discussion for more details:

What is the difference between CreateWnd and CreateWindowHandle?

+5
source share

All Articles