多个窗口同时移动,拖动任意一个窗口其他窗口都保持同步移动 ( 积分: 100 )

  • 主题发起人 主题发起人 emitsong
  • 开始时间 开始时间
E

emitsong

Unregistered / Unconfirmed
GUEST, unregistred user!
象winamp,网上给的列子都不能同步移动,只能实现磁性窗口功能,不能移动,而且移动起来很闪烁
 
象winamp,网上给的列子都不能同步移动,只能实现磁性窗口功能,不能移动,而且移动起来很闪烁
 
MoveWindow()
 
这是win32函数吗,有没有相关代码
 
我觉得还的用时间空间来监视
 
时间控制肯定不好,还是事件触发,如sendmessage等,求相关代码
 
盒子里好像见过了。 有例子
 
合子的东西不是我要的
 
给你一段代码,Form1移动,Form2跟着移动,
但是你自己可能还要写一些位置控制的代码
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
procedure FormShow(Sender: TObject);
private
{ Private declarations }
procedure WMMOVE(var Message: TMessage); message WM_MOVE;
procedure WMACTIVE(var Message: TMessage); message WM_NCACTIVATE;
public
{ Public declarations }

end;

var
Form1: TForm1;
offsetX : Integer;
offsetY : Integer;
implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
Form2.Show ;
end;

procedure TForm1.WMACTIVE(var Message: TMessage);
begin
inherited;
offsetX := Form2.Left-Left ;
offsetY := Form2.Top-Top ;
end;

procedure TForm1.WMMOVE(var Message: TMessage);
begin
if not Assigned(FOrm2) then exit;
MoveWindow(Form2.Handle,Left+offsetX,Top+offsetY,form2.Width,Form2.Height,True);
end;

end.
 
happy_boy,你的方法只能from1移动,from2跟随,无法form1,form2,互相跟随
 
你改一改呀.
通过Screen.FormCount得到Form数量
再将位置差值存到一个数组中
offsetX :array of Integer;
offsetY :array of Integer;
然后对Screen.Forms进行MoveWindow操作就可以了.
我没时间,你自己改一下
 
拖动任意一个窗口,你的方法是拖动指定窗口,其他一起动,如果其他窗口也拦截这两个消息,就会进死循环,程序会报错
 
你的其它几个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.
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Left1,Left2,Left3:integer;
Top1,Top2,Top3:integer;
procedure WMMove(var Message: TWMMove); message WM_MOVE;
end;

var
Form1: TForm1;

implementation
uses Unit2, Unit3;
{$R *.DFM}

procedure TForm1.WMMove(var Message: TWMMove);
begin
inherited;
if Assigned(Form2) then
begin
Form2.Top:=Form1.Top-Top1+Top2+30; // 30是偏差位置
Form2.Left:=Form1.Left-Left1+Left2+30; // 30是偏差位置
end;

if Assigned(Form3) then
begin
Form3.Top:=Form1.Top-Top1+Top3+60; // 60是偏差位置
Form3.Left:=Form1.Left-Left1+Left3+60; // 60是偏差位置
end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
Left1:=Form1.Left;
Top1:=Form1.Top;

Form2:=TForm2.Create(Self);
Left2:=Form2.Left;

Form3:=TForm3.Create(Self);
Top2:=Form2.Top;
Left3:=Form3.Left;
Top3:=Form3.Top;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
// 要顯示多個窗體,然後移動窗體就有效果了。
Form2.Show;
Form3.Show;
end;

end.
 
后退
顶部