以上方法不完善之處
當(dāng)把“拖動(dòng)窗口的過(guò)程中顯示其內(nèi)容”的選項(xiàng)取消時(shí),讓我們看看會(huì)發(fā)生什么。這是Windows窗口的一項(xiàng)設(shè)置,你可以在“開(kāi)始菜單-->設(shè)置-->文件夾選項(xiàng)-->查看-->高級(jí)設(shè)置”中找到該屬性。在Windows95中,你需要修改注冊(cè)表。當(dāng)此屬性被設(shè)為無(wú)效,拖動(dòng)窗體時(shí)窗體會(huì)變?yōu)橐粋€(gè)正方形輪廓線。也許你使用一個(gè)不規(guī)則窗體,但它仍然會(huì)顯示輪廓線。
當(dāng)你想要讓你的窗體停靠在屏幕的邊緣(如:WinAmp,當(dāng)你拖動(dòng)窗體進(jìn)入屏幕頂部某一特定位置時(shí),窗體便緊靠在屏幕頂部),如果你使用以上第二種方法,在鼠標(biāo)按鈕放開(kāi)前,你將無(wú)法處理窗體位置,并無(wú)法處理停靠問(wèn)題。
下面我會(huì)用簡(jiǎn)單的方法解決兩個(gè)問(wèn)題:
第一,無(wú)論設(shè)置為何,在拖動(dòng)窗體時(shí)都不顯示輪廓線;
第二,移動(dòng)窗體時(shí)進(jìn)行位置檢測(cè),并在位置適當(dāng)時(shí)停靠在某一特定位置。
很多人也許已經(jīng)解決了這些問(wèn)題,但是也許下面的代碼對(duì)你會(huì)有幫助。
方法三
以下代碼可以直接復(fù)制到Delphi中,前提是你將Form1存為uMain.pas,F(xiàn)orm2存為uDock.pas。用到的事件是:OnMouseDown,OnMouseMove,OnMouseUp,OnShow(Form1)。
這是一個(gè)根據(jù)鼠標(biāo)的移動(dòng)移動(dòng)窗體的方法,包含兩個(gè)窗體,uMain和uDock(Form1和Form2)。Form2通過(guò)Form1打開(kāi),并可以停靠在Form1的底部。停靠后,F(xiàn)orm2將隨Form1一起移動(dòng),直到你將Form2移開(kāi)。
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.
我希望以上內(nèi)容對(duì)那些正面臨類似內(nèi)容困擾的人有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注