Mega Code Archive

 
Categories / Delphi / Forms
 

Prenventing the user from positioning a form outside the screen work area

Title: Prenventing the user from positioning a form outside the screen work area Question: 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? Answer: 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 as shown here: http://www.delphi3000.com/articles/article_2076.asp 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); var WorkArea: TRect; begin inherited; if SystemParametersInfo(SPI_GETWORKAREA, 0, @WorkArea, 0) then begin if Left Left := WorkArea.Left else if Left + Width WorkArea.Right then Left := WorkArea.Right - Width; if Top Top := WorkArea.Top else if Top + Height WorkArea.Bottom then Top := WorkArea.Bottom - Height; end; end; The full source code of this example is available for download: http://www.latiumsoftware.com/download/p0020.zip