After applying a new style at runtime the MainForm of my application creates a new window handle – is there any way to stop this or reassign the Handle as I a getting a tonne of the following error:
'System Error. Code: 1400. Invalid window handle'
Is there any way to manipulate the process that forces the new handle to be assigned?
I solved this doing the following:
My main form created an unseen 'helper' form that is never displayed, but that does have visual components - it was throwing the handle error when it was trying to redraw these visual components, so I replaced the relevant components with objects instead (note I did not write this code originally).
There is no way to avoid re-creating the window handles. Instead, override your window's CreateWnd
and DestroyWnd
methods so that you're notified when a window is re-created.
Also, avoid keeping persistent references to handles of windows that might get destroyed. Instead, read the Handle
property each time you need it. Then you won't have to watch for notifications.
Beware of reading Handle
from a different thread, though, since it may cause the window to become associated with the wrong thread. Wrap any code that needs to interact with a VCL window into a method that you call via Synchronize
or Queue
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With