你的其它几个Form可以都从TFollowForm继承这样就可以了.
不过这个类写的不好,,没有太多时间思考了,你自己改一下吧.
因为它引用了几个在Unit1中的全局变量.
在下面还有测试代码
unit FollowForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFollowForm = class(TForm)
private
FFormList : TList;
{ Private declarations }
procedure WMMOVE(var Message: TMessage); message WM_MOVE;
procedure WMACTIVE(var Message: TWMActivate); message WM_NCACTIVATE;
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
end;
implementation
//这里一定要包含Unit1
uses Unit1;
procedure TFollowForm.WMACTIVE(var Message: TWMActivate);
var
i : Integer;
begin
inherited;
if not Assigned(FFormList) or not go then exit;
if Message.Active <> 0 then
begin
for i := 0 to Screen.FormCount - 1 do
FFormList.Add(Screen.Forms);
SetLength(offsetX,FFormList.Count);
SetLength(offsetY,FFormList.Count);
for i := 0 to FFormList.Count - 1 do
if TForm(FFormList.Items)<>Self then
begin
offsetX := TForm(FFormList.Items).Left - Left;
offsetY := TForm(FFormList.Items).Top - Top;
end;
end;
end;
procedure TFollowForm.WMMOVE(var Message: TMessage);
var
i : Integer;
begin
if not go then Exit;
if Assigned(FFormList)and(Self.Active) then
for i := 0 to FFormList.Count - 1 do
begin
if TForm(FFormList.Items)<>Self then
begin
TForm(FFormList.Items).Left := Left+offsetX;
TForm(FFormList.Items).Top := Top+offsetY;
end;
end;
end;
constructor TFollowForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFormList := TList.Create ;
end;
destructor TFollowForm.Destroy;
begin
FFormList.Free ;
inherited;
end;
end.
下面是测试代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,FollowForm, StdCtrls;
type
TForm1 = class(TFollowForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
go : boolean;
offsetX : array of Integer;
offsetY : array of Integer;
implementation
uses Unit2, Unit3;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show ;
Form3.Show;
if Button1.Caption = '开始' then
begin
go := True;
Button1.Caption := '停止';
end else
begin
go := False;
Button1.Caption := '开始';
end;
end;
end.