首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >德尔菲7-2表格可以跟随主体的位置。

德尔菲7-2表格可以跟随主体的位置。
EN

Stack Overflow用户
提问于 2015-05-29 19:37:28
回答 2查看 1.3K关注 0票数 0

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

我试着使用这个delphiDabbler提示,即阻止表单移动,但是没有成功地得到一些有用的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-29 19:46:25

同时处理WM_WINDOWPOSCHANGING以移动其他窗体。

代码语言:javascript
运行
复制
  ...

   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

(原代码根据评论中的建议更新)

或者像这样的两种形式相互碰撞

票数 2
EN

Stack Overflow用户

发布于 2015-05-29 19:52:55

在主要形式中,您需要这样做:

代码语言:javascript
运行
复制
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语句。

另一种形式是这样做:

代码语言:javascript
运行
复制
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;

这将确保任何移动另一个窗体的尝试都被拒绝。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30537760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档