Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to save your window size and position Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Peter Johnson

It's often useful to remember the size and state of your program's window (or 
sometimes of dialogue boxes) between executions. This article discusses how. 


The method we're going to use is to save the position in the registry. 

First of all, decide where you're going to keep the information. It's customary for 
apps to place information that varies between users in 


(X.X is the version number of the program). We'll use such a key in this article. 

You can save the window's current position and size when the program is closing 
down - the OnDestroy form event handler is a good place for this. The program then 
restores it's position from the registry (if it's been written yet) when opening - 
we use the form's OnCreate handler for that code. 

There are complications when saving and restoring the window state because of when 
the window is minimised, Delphi doesn't minimise the form - it hides it and 
displays the Application window in the taskbar. The method I've used causes a 
previously minimised window to flash on-screen briefly. I'd welcome ideas on any 
alternative approaches. (This has now been fixed -- see the component available for 

Another complication is that when a window is maximised Delphi updates the Width, 
Height, Left and Top properties of the form to the window's maximised size and 
position. This means that closing a maximised window stores the maximised size in 
the registry. When the program is run again it appears maximised, but when the user 
restores it they expect it to go to the previous normal size and position, but if 
we reloaded the Left, Top, Height and Width properties, the form won't shrink when 
restored. We get round this by using the Windows API to get the non-maximised size. 

Here's the code - the comments should explain what's happening. 

1   const
2     CRegKey = 'Software\Demos\WdwStateDemo\1.0';
4     // Helper function to read registry values, and deal with
5     // cases where no values exist
7   function ReadIntFromReg(Reg: TRegistry; Name: string;
8     Def: Integer): Integer;
9   {Reads integer with given name from registry and returns it
10  If no such value exists, returns Def default value}
11  begin
12    if Reg.ValueExists(Name) then
13      Result := Reg.ReadInteger(Name)
14    else
15      Result := Def;
16  end;
18  procedure TForm1.FormDestroy(Sender: TObject);
19  var
20    Reg: TRegistry; // the registry
21    State: Integer; // state of wdw
22    Pl: TWindowPlacement; // used for API call
23    R: TRect; // used for wdw pos
24  begin
25    {Calculate window's normal size and position using
26    Windows API call - the form's Width, Height, Top and
27    Left properties will give maximized window size if
28    form is maximised, which is not what we want here}
29    Pl.Length := SizeOf(TWindowPlacement);
30    GetWindowPlacement(Self.Handle, @Pl);
31    R := Pl.rcNormalPosition;
32    Reg := TRegistry.Create;
33    try
34      // Open required key - and create it if it doesn't exist
35      Reg.RootKey := HKEY_CURRENT_USER;
36      Reg.OpenKey(CRegKey, True);
37      // Write window size and position
38      Reg.WriteInteger('Width', R.Right - R.Left);
39      Reg.WriteInteger('Height', R.Bottom - R.Top);
40      Reg.WriteInteger('Left', R.Left);
41      Reg.WriteInteger('Top', R.Top);
42      // Write out state of window
43      {Record window state (maximised, minimised or normal)
44      - special case when minimized since form window is simply
45      hidden when minimised, and application window is actually
46      the one minimised - so we check to see if application
47      window *is* minimized and act accordingly}
48      if IsIconic(Application.Handle) then
49        {minimized - write that state}
50        State := Ord(wsMinimized)
51      else
52        {not mimimized - we can rely on window state of form}
53        State := Ord(Self.WindowState);
54      Reg.WriteInteger('State', State);
55    finally
56      Reg.Free;
57    end;
58  end;
60  procedure TForm1.FormCreate(Sender: TObject);
61  var
62    Reg: TRegistry; // the registry
63    State: Integer; // state of wdw
64  begin
65    Reg := TRegistry.Create;
66    try
67      // Open required key - and exit if it doesn't exist
68      Reg.RootKey := HKEY_CURRENT_USER;
69      if not Reg.OpenKey(CRegKey, False) then
70        Exit;
71      // Read the window size and position
72      // - designed form sizes are defaults
73      Self.Width := ReadIntFromReg(Reg, 'Width', Self.Width);
74      Self.Height := ReadIntFromReg(Reg, 'Height', Self.Height);
75      Self.Left := ReadIntFromReg(Reg, 'Left', Self.Left);
76      Self.Top := ReadIntFromReg(Reg, 'Top', Self.Top);
77      // Now get window state and restore
78      State := ReadIntFromReg(Reg, 'State', Ord(wsNormal));
79      {check if window was minimised - we have special
80      processing for minimized state since Delphi doesn't
81      minimize windows - it uses application window
82      instead}
83      if State = Ord(wsMinimized) then
84      begin
85        {we need to set visible true else form won't restore
86        properly - but this causes a brief display of form
87        any ideas on how to stop this?}
88        Self.Visible := True;
89        Application.Minimize;
90      end
91      else
92        Self.WindowState := TWindowState(State);
93    finally
94      Reg.Free;
95    end;
96  end;

A component that wraps up all this functionality on behalf of the form it lives on 
is available for download. There's also a sister component included that works with 
ini files rather than the registry. 

Component Download:

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC