麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Delphi > 正文

Delphi中的窗體移動

2019-11-18 18:54:39
字體:
來源:轉載
供稿:網友
如果你在開發圖形或多媒體應用程序,你可能正在為如何不使用窗體的標題欄而移動窗體發愁。其實只需用鼠標拖動窗體的客戶區就可以了。

方法一
  以下是完成上述功能最普通的方法:在窗體的PRivate聲明部分加入以下過程聲明:

  procedure WMNCHitTest(var Msg:TWMNCHitTest);message WM_NCHITTEST;

  然后在implementation部分加入以下代碼:

procedure TForm1{或你定義的Form名}.WMNCHitTest(var Msg:TWMNCHitTest);
begin
DefaultHandler(Msg);
if Msg.Result = HTCLIENT then
Msg.Result:= HTCAPTION;
end;

  此方法中使當鼠標點擊窗體客戶區時,令Windows認為被點擊的是標題欄。

方法二
  以下是另一個實現用鼠標移動普通窗體的方法。

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if (ssLeft in Shift) then begin
ReleaseCapture;
SendMessage(Form1.Handle,WM_SYSCOMMAND,SC_MOVE+1,0);
end;
end;

以上方法不完善之處
  當把“拖動窗口的過程中顯示其內容”的選項取消時,讓我們看看會發生什么。這是Windows窗口的一項設置,你可以在“開始菜單-->設置-->文件夾選項-->查看-->高級設置”中找到該屬性。在Windows95中,你需要修改注冊表。當此屬性被設為無效,拖動窗體時窗體會變為一個正方形輪廓線。也許你使用一個不規則窗體,但它仍然會顯示輪廓線。
  當你想要讓你的窗體停靠在屏幕的邊緣(如:WinAmp,當你拖動窗體進入屏幕頂部某一特定位置時,窗體便緊靠在屏幕頂部),如果你使用以上第二種方法,在鼠標按鈕放開前,你將無法處理窗體位置,并無法處理??繂栴}。
  下面我會用簡單的方法解決兩個問題:
  第一,無論設置為何,在拖動窗體時都不顯示輪廓線;
  第二,移動窗體時進行位置檢測,并在位置適當時??吭谀骋惶囟ㄎ恢谩?BR>  很多人也許已經解決了這些問題,但是也許下面的代碼對你會有幫助。

方法三
  以下代碼可以直接復制到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.


  我希望以上內容對那些正面臨類似內容困擾的人有所幫助。


上一篇:在Delphi中編寫控件的基本方法(1)

下一篇:Delphi菜單如何做成word2000的可??繕幼?/a>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 色a综合| 亚洲精品自在在线观看 | 久草在线最新 | 中文字幕网在线 | 一夜新娘第三季免费观看 | 成人超碰97 | 国产午夜亚洲精品午夜鲁丝片 | 日本黄色免费片 | 亚洲精品久久久久www | 欧美特一级片 | 亚洲精品xxx | 午夜a狂野欧美一区二区 | 中文字幕观看 | 毛片免费观看视频 | 一区二区三区视频播放 | 欧美成人精品一区 | 亚洲小视频网站 | av一道本 | 欧美极品欧美精品欧美视频 | 在线成人免费视频 | 亚洲午夜精品视频 | 日本中文视频 | 久久国产精品影视 | 久久亚洲成人网 | 色999久久久精品人人澡69 | 成人免费一区二区三区视频网站 | 久久99精品久久久久久秒播蜜臀 | 精品国产一区二区亚洲人成毛片 | 中文字幕在线观看日韩 | 国产日韩中文字幕 | 国产黄色一级大片 | 91女上位 在线播放 性欧美日本 | 国产精品久久久久久久久久iiiii | 蜜桃视频在线免费播放 | 久草在线播放视频 | 国产午夜网 | 成人一区二区三区在线 | 欧美18一19sex性护士农村 | 一级毛片在线免费观看 | 久久综合精品视频 | 午夜视频在线免费观看 |