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()
andDestroyWnd()
in most situations. OverrideDestroyWnd()
to do window unregistrations, temporarily save any window-dependent data if thecsRecreating
flag is present in theControlState
property, and call the inheritedDestroyWnd()
(which callsDestroyWindowHandle()
) to destroy the HWND. OverrideCreateWnd()
to call the inheritedCreateWnd()
(which callsCreateWindowHandle()
) to create the HWND, load and discard temporary window-dependent data if any was previously saved, and do window registrations.Override
CreateWindowHandle()
andDestroyWindowHandle()
when you need to obtain/free an HWND through other means than normal Win32CreateWindow/Ex()
andDestroyWindow()
calls. For example,TForm
overrides these to create/free MDI child windows by sendingWM_MDICREATE
andWM_MDIDESTROY
messages to itsClientHandle
window.I have never seen
CreateHandle()
andDestroyHandle()
overridden for anything useful.See this discussion for more details:
What's the difference between CreateWnd and CreateWindowHandle?