You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Delphi中INTACustomDockableForm启动时加载保存的窗口位置

问题

我已实现INTACustomDockableForm接口中保存和加载窗口状态的两个方法,代码如下:

procedure TMyDockableForm.SaveWindowState(Desktop : TCustomIniFile; const Section : string; IsProject : Boolean);
var
   wp : TWindowPlacement;
begin
   wp.length := Sizeof(wp);
   GetWindowPlacement(form.handle, @wp);
   var
   pos := wp.rcNormalPosition;
   Desktop.WriteInteger(Section , 'Left', pos.Left);
   Desktop.WriteInteger(Section , 'Top', pos.Top);
   Desktop.WriteInteger(Section , 'Width', pos.Right - pos.Left);
   Desktop.WriteInteger(Section , 'Height', pos.Bottom - pos.Top);
   Desktop.WriteString(Section , 'State', GetEnumName(TypeInfo(TWindowState), Ord(form.WindowState)));
end;

procedure TMyDockableForm.LoadWindowState(Desktop : TCustomIniFile; const Section : string);
var
   L, T, W, H : Integer;
begin
   L := Desktop.ReadInteger(Section , 'Left', form.Left);
   T := Desktop.ReadInteger(Section , 'Top', form.Top);
   W := Desktop.ReadInteger(Section , 'Width', form.Width);
   H := Desktop.ReadInteger(Section , 'Height', form.Height);
   form.SetBounds(L, T, W, H);
   form.Windowstate := TWindowState(GetEnumValue(TypeInfo(TWindowState), Desktop.ReadString(section, 'State', 'wsNormal')));
end;

当前保存标准布局、切换调试模式后重新加载标准布局时,这两个方法能正常触发;关闭Delphi时,新布局条目也会写入$AppData\Roaming\Embarcadero\BDS\22.0\Default Layout.dst文件,但Delphi再次启动时不会执行加载操作。我需要让可停靠窗体在Delphi启动时自动加载到保存的位置,该如何实现?

解决方法

要实现Delphi启动时自动加载可停靠窗体的保存状态,需完成以下操作:

  1. 正确注册可停靠窗体
    在包的Register过程中,将你的窗体类注册为IDE的自定义模块或工具窗口,确保IDE能识别并持久化其状态:

    procedure Register;
    begin
      // 注册为自定义模块
      RegisterCustomModule(TMyDockableForm, TCustomModule);
      // 若为工具窗口,需额外注册
      RegisterToolWindow(nil, TMyDockableForm, 'My Dockable Tool Window', '', 0, nil);
    end;
    
  2. 确保接口实现完整
    确认窗体类完全实现INTACustomDockableForm的所有方法,除了已实现的两个状态方法,还需实现GetName等标识类方法——这些方法用于IDE关联窗体与配置节,确保加载时能找到对应的数据。

  3. 避免手动覆盖布局
    不要在窗体的OnCreate、OnShow等事件中手动设置位置、大小或窗口状态,否则会覆盖LoadWindowState读取的配置值,让IDE的布局加载流程优先执行。

  4. 验证布局文件配置
    打开$AppData\Roaming\Embarcadero\BDS\22.0\Default Layout.dst,查找对应窗体的配置节(通常以类名或注册名称命名),确认其中存在Left、Top、Width、Height和State的有效数值。若节名与窗体标识不匹配,IDE启动时无法定位配置,也就不会触发加载。

内容的提问来源于stack exchange,提问作者Mattia72

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 06:21:09