【问题标题】:saving the Maximized and form size on a Delphi TForm在 Delphi TForm 上保存最大化和表单大小
【发布时间】:2010-11-14 18:16:01
【问题描述】:

这个问题看起来很简单,但由于某种原因我很难找到答案。

我有一个将表单的大小和位置保存在 INI 文件中的应用程序。这一切都很好,但是当您在最大化时关闭应用程序时,它将保存最大化的表单的大小和位置,但不会保存其状态。

我的意思是,在下一次运行时,表单会显示为最大化,而实际上它已“恢复”但覆盖了整个桌面。

有没有办法在最大化事件之前保存表单大小,然后保存表单被最大化的事实。在从 INI 文件读取时创建处于最大化状态的表单并将其“恢复”大小设置为最大化事件之前的大小?

谢谢!

【问题讨论】:

    标签: delphi forms size


    【解决方案1】:

    使用Windows API函数GetWindowPlacement(),像这样:

    procedure TForm1.WriteSettings(AUserSettings: TIniFile);
    var
      Wp: TWindowPlacement;
    begin
      Assert(AUserSettings <> nil);
    
      if HandleAllocated then begin
        // The address of Wp should be used when function is called
        Wp.length := SizeOf(TWindowPlacement);
        GetWindowPlacement(Handle, @Wp);
    
        AUserSettings.WriteInteger(SektionMainForm, KeyFormLeft,
          Wp.rcNormalPosition.Left);
        AUserSettings.WriteInteger(SektionMainForm, KeyFormTop,
          Wp.rcNormalPosition.Top);
        AUserSettings.WriteInteger(SektionMainForm, KeyFormWidth,
          Wp.rcNormalPosition.Right - Wp.rcNormalPosition.Left);
        AUserSettings.WriteInteger(SektionMainForm, KeyFormHeight,
          Wp.rcNormalPosition.Bottom - Wp.rcNormalPosition.Top);
        AUserSettings.WriteBool(SektionMainForm, KeyFormMaximized,
          WindowState = wsMaximized);
      end;
    end;
    

    【讨论】:

    • 谢谢。我如何调用这个函数?什么是 IPersistentSettingsWriter?
    • @wonderer -- 此示例显示 GetWindowPlacement API 函数的使用示例。您必须为自己的项目修改此示例...并将 AUserSettings 调用更改为 INI 文件或用户注册表设置之类的内容。
    • @skamradt:确实,+1。我只是从我当前的项目中复制它。 @wonderer:IPersistentSettingsWriter 最简单的形式是 TIniFile 的包装器,您可以看到 WriteXXX() 方法具有相同的名称和参数。只需按照 skamradt 的建议替换即可。
    • 已编辑,希望现在更清楚了。在我的辩护中,我认为针对接口而不是类进行编程是一件非常好的事情。我的设置接口可以很容易地实现在INI文件、注册表甚至数据库表存储方面,并且使用它的代码不必稍作更改。此外,我有不同的 IPersistentSettingsReaderIPersistentSettingsWriter 接口,因此如果代码仅提供给阅读器,则不可能意外写入只读目录中的 INI 文件界面。
    • 我明白了,谢谢。我收到以下错误:[Error] main.pas(1150): Incompatible types: 'tagWINDOWPLACEMENT' and 'PWindowPlacement' on GetWindowPlacement(Handle, Wp);
    【解决方案2】:

    试试 Form.WindowState 属性。通过阅读,您可以将其写入 ini 文件,然后从 ini 中读取以在 form.show 方法中重新设置状态。您可能希望将其重新转换为整数,因为 WindowState 是枚举类型 (TWindowState)。

    【讨论】:

      【解决方案3】:

      汤姆的回答应该很好用。这里有一些伪代码来澄清一下:

      procedure TfrmDatenMonitor.FormClose(Sender: TObject;
        var Action: TCloseAction);
      begin
        inherited;
        //*** Save the WindowState in every case
        aIniFile.WriteInteger(Name, 'State', Integer(WindowState));
      
        if WindowState = wsNormal then begin
          //*** Save Position and Size, too...
          aIniFile.WriteInteger(Name, 'Top',    Top);
          aIniFile.WriteInteger(Name, 'Left',   Left);
          aIniFile.WriteInteger(Name, 'Height', Height);
          aIniFile.WriteInteger(Name, 'Width',  Width);
        end;
      end;
      

      在读取设置时首先设置大小和位置。 然后读取 WindowState 并为其分配类型转换:

      WindowState := TWindowState(aIniFile.ReadInteger(Name, 'State', Integer(wsNormal)));
      

      【讨论】:

        【解决方案4】:

        DelphiDabbler 有一些不错的 window state components。您只需在表单上放置一个,它就会将状态保存到 ini 文件或表单销毁时的注册表中,并在表单创建时加载它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-27
          相关资源
          最近更新 更多