以上方法不完善之處
當把“拖動窗口的過程中顯示其內容”的選項取消時,讓我們看看會發生什麼。這是Windows窗口的一項設置,你可以在“開始菜單-->設置-->文件夾選項-->查看-->高級設置”中找到該屬性。在Windows95中,你需要修改注冊表。當此屬性被設為無效,拖動窗體時窗體會變為一個正方形輪廓線。也許你使用一個不規則窗體,但它仍然會顯示輪廓線。
當你想要讓你的窗體停靠在屏幕的邊緣(如:WinAmp,當你拖動窗體進入屏幕頂部某一特定位置時,窗體便緊靠在屏幕頂部),如果你使用以上第二種方法,在鼠標按鈕放開前,你將無法處理窗體位置,並無法處理停靠問題。
下面我會用簡單的方法解決兩個問題:
第一,無論設置為何,在拖動窗體時都不顯示輪廓線;
第二,移動窗體時進行位置檢測,並在位置適當時停靠在某一特定位置。
很多人也許已經解決了這些問題,但是也許下面的代碼對你會有幫助。
方法三
以下代碼可以直接復制到Delphi中,前提是你將Form1存為uMain.pas,Form2存為uDock.pas。用到的事件是:OnMouseDown,OnMouseMove,OnMouseUp,OnShow(Form1)。
這是一個根據鼠標的移動移動窗體的方法,包含兩個窗體,uMain和uDock(Form1和Form2)。Form2通過Form1打開,並可以停靠在Form1的底部。停靠後,Form2將隨Form1一起移動,直到你將Form2移開。
Form1
unit uMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormMouseDown(Sender:TObject; Button:TMouseButton;Shift:TShiftState;X,Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState;X,Y: Integer);
procedure FormMouseUp(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y: Integer);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
DocktoForm: Boolean;
{ Public declarations }
end;
var
Form1: TForm1;
CanMove, CanMoveX, CanMoveY: Boolean;
OldX, OldY: Integer;
F1X,F1Y,F2X,F2Y: integer;
WorkArea : TRect;
implementation
uses uDock;
{$R *.DFM}
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
begin
CanMoveX := true;
CanMoveY := true;
CanMove := true;
OldX := X;
OldY := Y;
if DocktoForm then
begin
F1X := Form1.Left;
F1Y := Form1.Top;
F2X := Form2.Left;
F2Y := Form2.Top;
end;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if (CanMove) then
begin
if CanMoveX then
Form1.Left := Form1.Left + (X - OldX);
if CanMoveY then
Form1.Top := Form1.Top + (Y - OldY);
//This section latches to the top
if (Form1.Top < WorkArea.Top + 10) and (Form1.Top > WorkArea.Top-10) then
begin
Form1.Top := WorkArea.Top;
if (Y-OldY > 10) or (Y-OldY < -10) then
CanMoveY := true
else
CanMoveY := False;
end;
//This section latches to the left side
if (Form1.Left < WorkArea.Left+10) and (Form1.Left > WoskArea.Left-10) then
begin
Form1.Left := WorkArea.Left;
if (X-OldX > 10) or (X-OldX < -10) then
CanMoveX := true
else
CanMoveX := False;
end;
//This section latches to the right side
if (Form1.Left > WorkArea.Right-Form1.Width-10) and (Form1.Left < WorkArea.Right-Form1.Width+10) then
begin
Form1.Left := WorkArea.Right-Form1.Width;
if (X-OldX > 10) or (X-OldX < -10) then
CanMoveX := true
else
CanMoveX := False;
end;
//This section latches to the TaskBar
if DocktoForm then
begin
if (Form1.Top > WorkArea.Bottom-Form1.Height-Form2.Height-10) and (Form1.Top < WorkArea.Bottom-Form1.Height-Form2.Height+10) then
begin
Form1.Top := WorkArea.Bottom-Form1.Height-Form2.Height;
if (Y-OldY > 10) or (Y-OldY < -10) then
CanMoveY := true
else
CanMoveY := False;
end;
end
else begin
if (Form1.Top > WorkArea.Bottom-Form1.Height-10) and (Form1.Top < WorkArea.Bottom-Form1.Height+10) then
begin
Form1.Top := WorkArea.Bottom-Form1.Height;
if (Y-OldY > 10) or (Y-OldY < -10) then
CanMoveY := true
else
CanMoveY := False;
end;
end;
if DocktoForm then
begin
Form2.Left := Form1.Left - (F1X-F2X);// + (X-OldX);
Form2.Top := Form1.Top+Form1.Height;
exit;
end;
//This section latches playlist in center of Form1
if (Form2.Left > Form1.Left + ((Form1.Width div 2)-(Form2.Width div 2))-10) and (Form2.Left < Form1.Left + ((Form1.Width div 2)-(Form2.Width div 2))+10) and
(Form2.Top > Form1.Top+Form1.Height-10) and (Form2.Top < Form1.Top+Form1.Height+10) then
begin
Form2.Left := Form1.Left + ((Form1.Width div 2)-(Form2.Width div 2));
DocktoForm := True;
F1X := Form1.Left;
F1Y := Form1.Top;
F2X := Form2.Left;
F2Y := Form2.Top;
end;
end;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
CanMove := false;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
//Get Work Area Parameters
SystemParametersInfo(SPI_GETWORKAREA, 0, @WorkArea, 0 );
Form2.Show;
end;
end.
Form2
unit uDock;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm2 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
CanMove, CanMoveX, CanMoveY, DocktoForm: Boolean;
OldX, OldY: Integer;
implementation
uses uMain;
{$R *.DFM}
procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
begin
CanMoveX := true;
CanMoveY := true;
CanMove := true;
OldX := X;
OldY := Y;
end;
procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if (CanMove) then
begin
if CanMoveX then
Form2.Left := Form2.Left + (X - OldX);
if CanMoveY then
Form2.Top := Form2.Top + (Y - OldY);
//This section latches to the top
if (Form2.Top < WorkArea.Top + 10) and (Form2.Top > WorkArea.Top-10) then
begin
Form2.Top := WorkArea.Top;
if (Y-OldY > 10) or (Y-OldY < -10) then
CanMoveY := true
else
CanMoveY := False;
end;
//This section latches to the left side
if (Form2.Left < WorkArea.Left+10) and (Form2.Left > WorkArea.Left-10) then
begin
Form2.Left := WorkArea.Left;
if (X-OldX > 10) or (X-OldX < -10) then
CanMoveX := true
else
CanMoveX := False;
end;
//This section latches to the right side
if (Form2.Left > WorkArea.Right-Form2.Width-10) and (Form2.Left < WorkArea.Right-Form2.Width+10) then
begin
Form2.Left := WorkArea.Right-Form2.Width;
if (X-OldX > 10) or (X-OldX < -10) then
CanMoveX := true
else
CanMoveX := False;
end;
//This section latches to the TaskBar
if (Form2.Top > WorkArea.Bottom-Form2.Height-10) and (Form2.Top < WorkArea.Bottom-Form2.Height+10) then
begin
Form2.Top := WorkArea.Bottom-Form2.Height;
if (Y-OldY > 10) or (Y-OldY < -10) then
CanMoveY := true
else
CanMoveY := False;
exit;
end;
//This section latches to the Player Bottom
if (Form2.Top > Form1.Top+Form1.Height-10) and (Form2.Top < Form1.Top+Form1.Height+10) and (Form2.Left > Form1.Left-Form2.Width) and (Form2.Left < Form1.Left + Form1.Width) then
begin
Form2.Top := Form1.Top+Form1.Height;
if (Y-OldY > 10) or (Y-OldY < -10) then begin
CanMoveY := true;
Form1.DockToForm := False;
end
else begin
CanMoveY := False;
Form1.DockToForm := True;
end;
end;
//This section latches playlist in center of Form1
if (Form2.Left > Form1.Left + ((Form1.Width div 2)-(Form2.Width div 2))-10) and (Form2.Left < Form1.Left + ((Form1.Width div 2)-(Form2.Width div 2))+10) and
(Form2.Top > Form1.Top+Form1.Height-10) and (Form2.Top < Form1.Top+Form1.Height+10) then
begin
Form2.Left := Form1.Left + ((Form1.Width div 2)-(Form2.Width div 2));
if (X-OldX > 10) or (X-OldX < -10) or (Y-OldY > 10) or (Y-OldY < -10) then
CanMoveX := true
else
CanMoveX := False;
end;
end;
end;
procedure TForm2.FormMouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
begin
CanMove := false;
end;
end.
我希望以上內容對那些正面臨類似內容困擾的人有所幫助。