2007. május 14., hétfő

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


Problem/Question/Abstract:

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.

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 < 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;
  end;
end;

The full source code of this example is available for download:

http://www.latiumsoftware.com/download/p0020.zip


Copyright (c) 2001 Ernesto De Spirito
Visit: http://www.latiumsoftware.com/delphi-newsletter.php

Nincsenek megjegyzések:

Megjegyzés küldése