2009-07-28 77 views
7

这个问题看起来很简单,但由于某种原因,我无法找到答案。在Delphi TForm上保存最大化和表单大小

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

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

是否有一种方法可以保存最大化事件之前的表单大小,然后保存表单最大化的事实。从INI文件读取的内容是以最大化状态创建表单并将其“还原”大小设置为最大化事件之前的大小?

谢谢!

回答

12

使用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; 
+0

谢谢。我如何调用这个函数?什么是IPersistentSettingsWriter? – wonderer 2009-07-28 16:02:35

3

尝试Form.WindowState财产。通过阅读它,你可以将它写入ini文件,然后从ini中读回以重新设置form.show方法中的状态。由于WindowState是一个枚举类型(TWindowState),您可能希望将其重新转换为整数。

0

DelphiDabbler有一些不错的window state components。你只需在表单上放一个,然后将状态保存到一个ini文件或表单上的注册表中,然后将其装载到表单create上。

2

汤姆的答案应该很好地工作。这里有一些伪代码稍作澄清:

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)));