我怎样才能使第二种形式能够跟随主形式的位置,无论主形式在哪里移动。例如,在这个GIF图像中可以看到:

我试着使用这个delphiDabbler提示,即阻止表单移动,但是没有成功地得到一些有用的东西。
发布于 2015-05-29 19:46:25
同时处理WM_WINDOWPOSCHANGING以移动其他窗体。
...
public
OldTop, OldLeft: Integer;
procedure WindowPosChanging(var Msg: TWMWindowPosChanging);
message WM_WINDOWPOSCHANGING;
end;
...
implementation
...
procedure TForm1.WindowPosChanging(var Msg: TWMWindowPosChanging);
var
DTop, DLeft: Integer;
begin
// well and here inside of you put the relationship of like you
// want him to move.
// an example of this moving them in the same sense can be...
if (Form2 = nil) or (not Form2.Visible) then Exit;
// this line is to avoid the error of calling them when the forms
// are creating or when they are not visible...
DTop := Top - OldTop;
DLeft := Left - OldLeft;
Form2.Top := Form2.Top + DTop;
Form2.Left := Form2.Left + DLeft;
OldTop := Top;
OldLeft := Left;
inherited;
end;来源:http://delphi.cjcsoft.net/viewthread.php?tid=43047
(原代码根据评论中的建议更新)
或者像这样的两种形式相互碰撞
发布于 2015-05-29 19:52:55
在主要形式中,您需要这样做:
type
TMainForm = class(TForm)
protected
procedure WMWindowPosChanged(var Msg: TWMWindowPosChanged);
message WM_WINDOWPOSCHANGED;
end;
....
procedure TMainForm.WMWindowPosChanged(var Msg: TWMWindowPosChanged);
begin
inherited;
if Assigned(OtherForm) and not Application.Terminated then
begin
OtherForm.Left := Left + Width;
OtherForm.Top := Top;
end;
end;这确保了每当主窗体的位置发生变化时,另一个表单就会夹紧在它上。请注意,此消息可以在创建其他表单之前发送,也可以在它不再有效之后发送。因此产生了if语句。
另一种形式是这样做:
type
TOtherForm = class(TForm)
protected
procedure WMWindowPosChanging(var Msg: TWMWindowPosChanging);
message WM_WINDOWPOSCHANGING;
end;
....
procedure TOtherForm.WMWindowPosChanging(var Msg: TWMWindowPosChanging);
begin
inherited;
if not Application.Terminated then
begin
Msg.WindowPos.x := MainForm.Left + MainForm.Width;
Msg.WindowPos.y := MainForm.Top;
end;
end;这将确保任何移动另一个窗体的尝试都被拒绝。
https://stackoverflow.com/questions/30537760
复制相似问题