編程的時候,有時為了需要,會將窗體的BorderStyle設置為bsNone,即無標題窗體。但是這樣一來,因為沒有了標題欄,就無法拖動窗體了。其實,我們只需要用以下的方法,就可以實現平滑拖動窗體了。
OnMouseDown事件中加入
OldX:=x;
OldY:=u;
OnMouseMove事件中加入
Form1.Left:=Form1.Left+x-Oldx;
Form1.Top:=Form1.Top+y-Oldy;
##1創碼如下:
unit Unit1;
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;Button:TMouseButton;
Shift:TShiftState;X,Y,Integer);
private
{Private declarations}
public
Private declarations}
end;
var
Form1:TForm1;
OldX,OldY:integer; //定義全局變量
implementation
$R *.DFM}
procedure TForm1.FormMouseDown(Sender:TObject;Button:TMouseButton;
Shift:TShiftState;X,Y:Integer);
begin
OldX:=x;
OldY:=y;
end;
procedure TForm1.FormMouseMove(Sender:TObject;Button:TMouseButton
Shift:TShiftState;X,
Y:Integer);
begin
if ssleft in shift then //按下鼠標左鍵
begin
Form1.Left:=Form1.Left+x-Oldx;
Form1.Top:=Form1.Top+y-Oldy;
end;
end;
end.
閡隕洗碼在Delphi5.0、Win98 SE中測試通過。