QuickImage – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #region Using directives |
2 | |||
3 | using System.Windows.Forms; |
||
4 | using Microsoft.Win32; |
||
5 | using System.Drawing; |
||
6 | |||
7 | #endregion |
||
8 | |||
9 | namespace DocToolkit |
||
10 | { |
||
11 | /// <summary> |
||
12 | /// Class allows to keep last window state in Registry |
||
13 | /// and restore it when form is loaded. |
||
14 | /// |
||
15 | /// Source: Saving and Restoring the Location, Size and |
||
16 | /// Windows State of a .NET Form |
||
17 | /// By Joel Matthias |
||
18 | /// |
||
19 | /// Downloaded from http://www.codeproject.com |
||
20 | /// |
||
21 | /// Using: |
||
22 | /// 1. Add class member to the owner form: |
||
23 | /// |
||
24 | /// private PersistWindowState persistState; |
||
25 | /// |
||
26 | /// 2. Create it in the form constructor: |
||
27 | /// |
||
28 | /// persistState = new PersistWindowState("Software\\MyCompany\\MyProgram", this); |
||
29 | /// |
||
30 | /// </summary> |
||
31 | public class PersistWindowState |
||
32 | { |
||
33 | #region Members |
||
34 | |||
35 | private Form ownerForm; // reference to owner form |
||
36 | private string registryPath; // path in Registry where state information is kept |
||
37 | |||
38 | // Form state parameters: |
||
39 | private int normalLeft; |
||
40 | private int normalTop; |
||
41 | private int normalWidth; |
||
42 | private int normalHeight; |
||
43 | |||
44 | // FormWindowState is enumeration from System.Windows.Forms Namespace |
||
45 | // Contains 3 members: Maximized, Minimized and Normal. |
||
46 | private FormWindowState windowState = FormWindowState.Normal; |
||
47 | |||
48 | // if allowSaveMinimized is true, form closed in minimal state |
||
49 | // is loaded next time in minimal state. |
||
50 | private bool allowSaveMinimized = false; |
||
51 | |||
52 | #endregion |
||
53 | |||
54 | #region Constructor |
||
55 | |||
56 | /// <summary> |
||
57 | /// Initialization |
||
58 | /// </summary> |
||
59 | /// <param name="sRegPath"></param> |
||
60 | /// <param name="owner"></param> |
||
61 | public PersistWindowState(string path, Form owner) |
||
62 | { |
||
63 | if (path == null || |
||
64 | path.Length == 0) |
||
65 | { |
||
66 | registryPath = "Software\\Unknown"; |
||
67 | } |
||
68 | else |
||
69 | { |
||
70 | registryPath = path; |
||
71 | } |
||
72 | |||
73 | if (!registryPath.EndsWith("\\")) |
||
74 | registryPath += "\\"; |
||
75 | |||
76 | registryPath += "MainForm"; |
||
77 | |||
78 | ownerForm = owner; |
||
79 | |||
80 | // subscribe to parent form's events |
||
81 | |||
82 | ownerForm.Closing += OnClosing; |
||
83 | ownerForm.Resize += OnResize; |
||
84 | ownerForm.Move += OnMove; |
||
85 | ownerForm.Load += OnLoad; |
||
86 | |||
87 | // get initial width and height in case form is never resized |
||
88 | normalWidth = ownerForm.Width; |
||
89 | normalHeight = ownerForm.Height; |
||
90 | } |
||
91 | |||
92 | #endregion |
||
93 | |||
94 | #region Properties |
||
95 | |||
96 | /// <summary> |
||
97 | /// AllowSaveMinimized property (default value false) |
||
98 | /// </summary> |
||
99 | public bool AllowSaveMinimized |
||
100 | { |
||
101 | get |
||
102 | { |
||
103 | return allowSaveMinimized; |
||
104 | } |
||
105 | set |
||
106 | { |
||
107 | allowSaveMinimized = value; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | #endregion |
||
112 | |||
113 | #region Event Handlers |
||
114 | |||
115 | |||
116 | /// <summary> |
||
117 | /// Parent form is resized. |
||
118 | /// Keep current size. |
||
119 | /// </summary> |
||
120 | /// <param name="sender"></param> |
||
121 | /// <param name="e"></param> |
||
122 | private void OnResize(object sender, System.EventArgs e) |
||
123 | { |
||
124 | // save width and height |
||
125 | if (ownerForm.WindowState == FormWindowState.Normal) |
||
126 | { |
||
127 | normalWidth = ownerForm.Width; |
||
128 | normalHeight = ownerForm.Height; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /// <summary> |
||
133 | /// Parent form is moved. |
||
134 | /// Keep current window position. |
||
135 | /// </summary> |
||
136 | /// <param name="sender"></param> |
||
137 | /// <param name="e"></param> |
||
138 | private void OnMove(object sender, System.EventArgs e) |
||
139 | { |
||
140 | // save position |
||
141 | if (ownerForm.WindowState == FormWindowState.Normal) |
||
142 | { |
||
143 | normalLeft = ownerForm.Left; |
||
144 | normalTop = ownerForm.Top; |
||
145 | } |
||
146 | |||
147 | // save state |
||
148 | windowState = ownerForm.WindowState; |
||
149 | } |
||
150 | |||
151 | /// <summary> |
||
152 | /// Parent form is closing. |
||
153 | /// Keep last state in Registry. |
||
154 | /// </summary> |
||
155 | /// <param name="sender"></param> |
||
156 | /// <param name="e"></param> |
||
157 | private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) |
||
158 | { |
||
159 | // save position, size and state |
||
160 | RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath); |
||
161 | key.SetValue("Left", normalLeft); |
||
162 | key.SetValue("Top", normalTop); |
||
163 | key.SetValue("Width", normalWidth); |
||
164 | key.SetValue("Height", normalHeight); |
||
165 | |||
166 | // check if we are allowed to save the state as minimized (not normally) |
||
167 | if (!allowSaveMinimized) |
||
168 | { |
||
169 | if (windowState == FormWindowState.Minimized) |
||
170 | windowState = FormWindowState.Normal; |
||
171 | } |
||
172 | |||
173 | key.SetValue("WindowState", (int)windowState); |
||
174 | } |
||
175 | |||
176 | /// <summary> |
||
177 | /// Parent form is loaded. |
||
178 | /// Read last state from Registry and set it to form. |
||
179 | /// </summary> |
||
180 | /// <param name="sender"></param> |
||
181 | /// <param name="e"></param> |
||
182 | private void OnLoad(object sender, System.EventArgs e) |
||
183 | { |
||
184 | // attempt to read state from registry |
||
185 | RegistryKey key = Registry.CurrentUser.OpenSubKey(registryPath); |
||
186 | if (key != null) |
||
187 | { |
||
188 | int left = (int)key.GetValue("Left", ownerForm.Left); |
||
189 | int top = (int)key.GetValue("Top", ownerForm.Top); |
||
190 | int width = (int)key.GetValue("Width", ownerForm.Width); |
||
191 | int height = (int)key.GetValue("Height", ownerForm.Height); |
||
192 | FormWindowState windowState = (FormWindowState)key.GetValue("WindowState", (int)ownerForm.WindowState); |
||
193 | |||
194 | ownerForm.Location = new Point(left, top); |
||
195 | ownerForm.Size = new Size(width, height); |
||
196 | ownerForm.WindowState = windowState; |
||
197 | } |
||
198 | } |
||
199 | |||
200 | #endregion |
||
201 | |||
202 | } |
||
203 | } |