As you know, window handles of windowed controls (this includes forms) are considered volatile. That is, setting some properties may cause control to destroy its window handle and then recreate (there is a bunch of examples of that technique, search StdCtrls
for RecreateWnd
).
So, when I need to register my HWND with OS after creation and unregister it before destruction I should override corresponding method pair. Brief look to Controls
and Forms
gave me 3 pairs, all of them virtual:
- CreateHandle/DestroyHandle
- CreateWindowHandle/DestroyWindowHandle
- CreateWnd/DestroyWnd
Are there any more pairs? Which pair should I override for a form? And Which pair for general windowed control?
Override CreateWnd()
and DestroyWnd()
in most situations. Override DestroyWnd()
to do window unregistrations, temporarily save any window-dependent data if the csRecreating
flag is present in the ControlState
property, and call the 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-dependent data if any was previously saved, and do window registrations.
Override CreateWindowHandle()
and DestroyWindowHandle()
when you need to obtain/free an HWND through other means than normal Win32 CreateWindow/Ex()
and DestroyWindow()
calls. For example, TForm
overrides these to create/free MDI child windows by sending WM_MDICREATE
and WM_MDIDESTROY
messages to its ClientHandle
window.
I have never seen CreateHandle()
and DestroyHandle()
overridden for anything useful.
See this discussion for more details:
What's the difference between CreateWnd and CreateWindowHandle?