Difference between revisions of "macOS Form Position Save and Restore"

From Free Pascal wiki
Jump to navigationJump to search
(Mew macOS content :-)
 
(→‎Code example: Add comments)
 
(3 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
Here is a quick, almost code-free method to save the form's position and size using the Mac preferences system.
 
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 an OnShow 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.
+
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
 
A short example may be found below. Note that you need to give the form a unique name - here I have used Form_123
Line 21: Line 21:
  
 
uses
 
uses
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
+
   Forms,     // needed for main form
   CocoaAll;
+
   CocoaAll;   // needed for NSView
  
 
type
 
type

Latest revision as of 06:32, 28 December 2021

English (en)

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

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

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