|
L'intento è di fare in modo che la nostra finestra si agganci automaticamente ai bordi dello schermo quando si trova a meno di una certa distanza (ad esempio 20 pixel) da essi.
Innanzitutto va creato il nostro record:
type
TWindowPos = packed record
hwnd: HWND;
hwndInsertAfter: HWND;
x: Integer;
y: Integer;
cx: Integer;
cy: Integer;
flags: UINT;
end;
Dopodiché si crea la funzione che si aggancia ai messaggi di Windows che controllano la posizione della finestra.
-------------------------------------
... //interface
private
procedure WMWINDOWPOSCHANGING(var Msg: TWMWINDOWPOSCHANGING) ; message WM_WINDOWPOSCHANGING;
...//implementation
procedure TForm1.WMWINDOWPOSCHANGING(var Msg: TWMWINDOWPOSCHANGING);
var
Docked: Boolean;
rWorkArea: TRect;
StickAt : Word;
begin
StickAt := 20; //20 PIXELS
Docked := FALSE;
SystemParametersInfo(SPI_GETWORKAREA, 0, @rWorkArea, 0) ;
with Msg.WindowPos^ do
begin
if x <= rWorkArea.Left + StickAt then
begin
x := rWorkArea.Left;
Docked := TRUE;
end;
if x + cx >= rWorkArea.Right - StickAt then
begin
x := rWorkArea.Right - cx;
Docked := TRUE;
end;
if y <= rWorkArea.Top + StickAt then
begin
y := rWorkArea.Top;
Docked := TRUE;
end;
if y + cy >= rWorkArea.Bottom - StickAt then
begin
y := rWorkArea.Bottom - cy;
Docked := TRUE;
end;
if Docked then
begin
with rWorkArea do
begin
// no moving out of the screen
if x < Left then x := Left;
if x + cx > Right then x := Right - cx;
if y < Top then y := Top;
if y + cy > Bottom then y := Bottom - cy;
end; {with rWorkArea}
end; {if Docked}
end; {with Msg.WindowPos^}
inherited;
end;
-------------------------------------
Ora è sufficiente eseguire il progetto ed avvicinare la finestra ai bordi dello schermo per ottenere il risultato desiderato.
|