Sign in Join
Wiki copytemporary

macOS Form Position Save and Restore

(macOS only)

Overview

Here is a quick, almost code-free method to save the form's position and size using the Mac preferences system.

The setFrameAutosaveName method sets the preferences key name used and loads any previously-saved state. As a result, it is best to call it in a Form's OnShow event handler. There is no need to do anything else and the state of the form's position and size will be saved automatically when the form closes.

A short example may be found below. Note that you need to give the form a unique name - here I have used Form_123

Code example

pascal
unit Unit1;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}

interface

uses
  Forms,      // needed for main form
  CocoaAll;   // needed for NSView

type

  { TForm_123 }

  TForm_123 = class(TForm)
    procedure FormShow(Sender: TObject);
  private

  public

  end;

var
  Form_123: TForm_123;

implementation

{$R *.lfm}

{ TForm_123 }

procedure TForm_123.FormShow(Sender: TObject);
begin
  NSView(Form_123.Handle).window.setFrameAutosaveName(NSSTR(ClassName));
end;

end.

See also

External links


From the Free Pascal wiki, page "macOS Form Position Save and Restore", as it stood on 2021-12-28 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.