Prenventing the user from positioning a form outside the screen work area (Views: 100)
How can I prevent the user from moving the form outside screen boundaries to guarantee the form is always visible inside the screen work area?
We can know if a form has resized with the Resize event (OnResize property), but how do we know if a form has moved? Simply by capturing the WM_MOVE Windows message.
In the message event we call "inherited" to let the ancestors of TForm process the message. This will update the Left and Top properties that we can use along with Width and Height to see if the form is placed within the limits of the screen's work area (the portion of the screen not used by the system taskbar or by application desktop toolbars) and move it if not.
procedure TfrmMain.OnMove(var Msg: TWMMove);
if SystemParametersInfo(SPI_GETWORKAREA, 0, @WorkArea, 0) then
if Left < WorkArea.Left then
Left := WorkArea.Left
else if Left + Width > WorkArea.Right then
Left := WorkArea.Right - Width;
if Top < WorkArea.Top then
Top := WorkArea.Top
else if Top + Height > WorkArea.Bottom then
Top := WorkArea.Bottom - Height;
The full source code of this example is available for download:
Copyright (c) 2001 Ernesto De Spirito
<< Back to main page