How to capture a form's OnMove event
Delphi does not expose an OnMove event handler for forms to notify you when a form has been moved. But this event can be captured in code and acted upon, allowing you to respond to the movement of a form by the user.
To capture the OnMove event, add an event handler to respond to the WM_MOVE message, so add the following to the definition of your form:
procedure OnMove(var Msg: TWMMove); message WM_MOVE;
In your OnMove event handler be sure to allow the base class (TForm) to process the message by calling inherited, e.g.:
procedure TMyForm.OnMove(var Msg: TWMMove); begin inherited; end
The WM_MOVE message is sent after the form has been moved, so to get the new location of the form simply interrogate the form's Top and Left properties as normal. You will need to use the Messages unit for the definition of WM_MOVE.
If you wish to capture the event to prevent the form from being moved off the visible area of the screen then the message to capture is WM_MOVING.
These notes are believed to be correct for Delphi 6 and Delphi 7, and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.