1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Windows.Forms;
|
---|
4 | using System.Runtime.InteropServices;
|
---|
5 | using Microsoft.Win32;
|
---|
6 |
|
---|
7 | namespace Common
|
---|
8 | {
|
---|
9 | class FormDimensions
|
---|
10 | {
|
---|
11 | public Form Form;
|
---|
12 | public string RegSubKey;
|
---|
13 | public FormWindowState DefaultFormWindowState = FormWindowState.Normal;
|
---|
14 |
|
---|
15 | [DllImport("user32.dll")]
|
---|
16 | [return: MarshalAs(UnmanagedType.Bool)]
|
---|
17 | static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
|
---|
18 |
|
---|
19 | public FormDimensions()
|
---|
20 | {
|
---|
21 | RegSubKey = "Forms";
|
---|
22 | }
|
---|
23 |
|
---|
24 | private struct WINDOWPLACEMENT
|
---|
25 | {
|
---|
26 | public int length;
|
---|
27 | public int flags;
|
---|
28 | public int showCmd;
|
---|
29 | public System.Drawing.Point ptMinPosition;
|
---|
30 | public System.Drawing.Point ptMaxPosition;
|
---|
31 | public System.Drawing.Rectangle rcNormalPosition;
|
---|
32 | }
|
---|
33 |
|
---|
34 | const UInt32 SW_HIDE = 0;
|
---|
35 | const UInt32 SW_SHOWNORMAL = 1;
|
---|
36 | const UInt32 SW_NORMAL = 1;
|
---|
37 | const UInt32 SW_SHOWMINIMIZED = 2;
|
---|
38 | const UInt32 SW_SHOWMAXIMIZED = 3;
|
---|
39 | const UInt32 SW_MAXIMIZE = 3;
|
---|
40 | const UInt32 SW_SHOWNOACTIVATE = 4;
|
---|
41 | const UInt32 SW_SHOW = 5;
|
---|
42 | const UInt32 SW_MINIMIZE = 6;
|
---|
43 | const UInt32 SW_SHOWMINNOACTIVE = 7;
|
---|
44 | const UInt32 SW_SHOWNA = 8;
|
---|
45 | const UInt32 SW_RESTORE = 9;
|
---|
46 |
|
---|
47 | private WINDOWPLACEMENT GetPlacement(Form form)
|
---|
48 | {
|
---|
49 | WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
|
---|
50 | placement.length = Marshal.SizeOf(placement);
|
---|
51 | GetWindowPlacement(form.Handle, ref placement);
|
---|
52 | return placement;
|
---|
53 | }
|
---|
54 |
|
---|
55 | private bool IsVisibleOnAnyScreen(Rectangle rect)
|
---|
56 | {
|
---|
57 | int minVisible = 50;
|
---|
58 | foreach (Screen screen in Screen.AllScreens)
|
---|
59 | {
|
---|
60 | Rectangle visibleArea = screen.WorkingArea;
|
---|
61 | visibleArea.Intersect(rect);
|
---|
62 | if ((visibleArea.Width > minVisible) && (visibleArea.Height > minVisible) && (rect.Top >= 0))
|
---|
63 | {
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void Load(Form form, Form parentForm = null)
|
---|
71 | {
|
---|
72 | this.Form = form;
|
---|
73 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name);
|
---|
74 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + form.Name);
|
---|
75 |
|
---|
76 | string name = "";
|
---|
77 | if (parentForm != null) name = parentForm.Name;
|
---|
78 |
|
---|
79 | bool prevVisibleState = form.Visible;
|
---|
80 | form.Visible = false;
|
---|
81 | Rectangle windowPosition = form.DesktopBounds;
|
---|
82 | Rectangle restoreBounds = form.RestoreBounds;
|
---|
83 | if (parentForm != null)
|
---|
84 | {
|
---|
85 | // Set default form position centered to parent form
|
---|
86 | Point currentParentFormCenter = parentForm.DesktopBounds.Center();
|
---|
87 | windowPosition.X = currentParentFormCenter.X - windowPosition.Width / 2;
|
---|
88 | windowPosition.Y = currentParentFormCenter.Y - windowPosition.Height / 2;
|
---|
89 | restoreBounds.X = currentParentFormCenter.X - restoreBounds.Width / 2;
|
---|
90 | restoreBounds.Y = currentParentFormCenter.Y - restoreBounds.Height / 2;
|
---|
91 | }
|
---|
92 | windowPosition = new Rectangle(
|
---|
93 | (int)regKey.GetValue("DesktopBoundsLeft" + name, windowPosition.Left),
|
---|
94 | (int)regKey.GetValue("DesktopBoundsTop" + name, windowPosition.Top),
|
---|
95 | (int)regKey.GetValue("DesktopBoundsWidth" + name, windowPosition.Width),
|
---|
96 | (int)regKey.GetValue("DesktopBoundsHeight" + name, windowPosition.Height));
|
---|
97 | restoreBounds = new Rectangle(
|
---|
98 | (int)regKey.GetValue("RestoreBoundsLeft" + name, restoreBounds.Left),
|
---|
99 | (int)regKey.GetValue("RestoreBoundsTop" + name, restoreBounds.Top),
|
---|
100 | (int)regKey.GetValue("RestoreBoundsWidth" + name, restoreBounds.Width),
|
---|
101 | (int)regKey.GetValue("RestoreBoundsHeight" + name, restoreBounds.Height));
|
---|
102 | FormWindowState windowState = (FormWindowState)regKey.GetValue("State" + name, (int)DefaultFormWindowState);
|
---|
103 | if (parentForm != null)
|
---|
104 | {
|
---|
105 | Rectangle parentBounds = new Rectangle(
|
---|
106 | (int)regKey.GetValue("ParentBoundsLeft" + name, parentForm.DesktopBounds.Left),
|
---|
107 | (int)regKey.GetValue("ParentBoundsTop" + name, parentForm.DesktopBounds.Top),
|
---|
108 | (int)regKey.GetValue("ParentBoundsWidth" + name, parentForm.DesktopBounds.Width),
|
---|
109 | (int)regKey.GetValue("ParentBoundsHeight" + name, parentForm.DesktopBounds.Height));
|
---|
110 | Point currentParentFormCenter = parentForm.DesktopBounds.Center();
|
---|
111 | Point previousParentFormCenter = parentBounds.Center();
|
---|
112 | Point parentShift = Point.Subtract(currentParentFormCenter, new Size(previousParentFormCenter));
|
---|
113 | windowPosition.Offset(parentShift);
|
---|
114 | }
|
---|
115 | switch (windowState)
|
---|
116 | {
|
---|
117 | case FormWindowState.Normal:
|
---|
118 | form.WindowState = FormWindowState.Normal;
|
---|
119 | if ((windowPosition != Rectangle.Empty) && IsVisibleOnAnyScreen(windowPosition))
|
---|
120 | {
|
---|
121 | form.DesktopBounds = windowPosition;
|
---|
122 | }
|
---|
123 | form.WindowState = windowState;
|
---|
124 | break;
|
---|
125 | case FormWindowState.Minimized:
|
---|
126 | form.WindowState = FormWindowState.Normal;
|
---|
127 | if ((windowPosition != Rectangle.Empty) && IsVisibleOnAnyScreen(restoreBounds))
|
---|
128 | {
|
---|
129 | form.DesktopBounds = restoreBounds;
|
---|
130 | }
|
---|
131 | break;
|
---|
132 | case FormWindowState.Maximized:
|
---|
133 | form.WindowState = FormWindowState.Normal;
|
---|
134 | if ((windowPosition != Rectangle.Empty) && IsVisibleOnAnyScreen(restoreBounds))
|
---|
135 | {
|
---|
136 | form.DesktopBounds = restoreBounds;
|
---|
137 | }
|
---|
138 | form.WindowState = FormWindowState.Maximized;
|
---|
139 | break;
|
---|
140 | default:
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | form.Visible = prevVisibleState;
|
---|
144 | LoadControl(form);
|
---|
145 | }
|
---|
146 |
|
---|
147 | public void Save(Form form, Form parentForm = null)
|
---|
148 | {
|
---|
149 | this.Form = form;
|
---|
150 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name, true);
|
---|
151 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + form.Name);
|
---|
152 |
|
---|
153 | string name = "";
|
---|
154 | if (parentForm != null) name = parentForm.Name;
|
---|
155 |
|
---|
156 | WINDOWPLACEMENT placement = GetPlacement(form);
|
---|
157 | const int WPF_RESTORETOMAXIMIZED = 0x2;
|
---|
158 |
|
---|
159 | if ((form.WindowState == FormWindowState.Minimized) &&
|
---|
160 | ((placement.flags | WPF_RESTORETOMAXIMIZED) != WPF_RESTORETOMAXIMIZED))
|
---|
161 | {
|
---|
162 | regKey.SetValue("State" + name, (int)FormWindowState.Minimized);
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | regKey.SetValue("State" + name, (int)form.WindowState);
|
---|
167 | }
|
---|
168 | regKey.SetValue("DesktopBoundsLeft" + name, form.DesktopBounds.Left);
|
---|
169 | regKey.SetValue("DesktopBoundsTop" + name, form.DesktopBounds.Top);
|
---|
170 | regKey.SetValue("DesktopBoundsWidth" + name, form.DesktopBounds.Width);
|
---|
171 | regKey.SetValue("DesktopBoundsHeight" + name, form.DesktopBounds.Height);
|
---|
172 | regKey.SetValue("RestoreBoundsLeft" + name, form.RestoreBounds.Left);
|
---|
173 | regKey.SetValue("RestoreBoundsTop" + name, form.RestoreBounds.Top);
|
---|
174 | regKey.SetValue("RestoreBoundsWidth" + name, form.RestoreBounds.Width);
|
---|
175 | regKey.SetValue("RestoreBoundsHeight" + name, form.RestoreBounds.Height);
|
---|
176 | if (parentForm != null)
|
---|
177 | {
|
---|
178 | regKey.SetValue("ParentBoundsLeft" + name, parentForm.DesktopBounds.Left);
|
---|
179 | regKey.SetValue("ParentBoundsTop" + name, parentForm.DesktopBounds.Top);
|
---|
180 | regKey.SetValue("ParentBoundsWidth" + name, parentForm.DesktopBounds.Width);
|
---|
181 | regKey.SetValue("ParentBoundsHeight" + name, parentForm.DesktopBounds.Height);
|
---|
182 | }
|
---|
183 | SaveControl(form);
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void LoadControl(Control control)
|
---|
187 | {
|
---|
188 | if (control is ListView)
|
---|
189 | {
|
---|
190 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true);
|
---|
191 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name);
|
---|
192 |
|
---|
193 | for (int I = 0; I < (control as ListView).Columns.Count; I++)
|
---|
194 | {
|
---|
195 | if (regKey.GetValue("ColWidth" + I.ToString()) != null)
|
---|
196 | (control as ListView).Columns[I].Width = (int) regKey.GetValue("ColWidth" + I.ToString());
|
---|
197 | }
|
---|
198 | } else
|
---|
199 | if (control is DataGridView)
|
---|
200 | {
|
---|
201 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true);
|
---|
202 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name);
|
---|
203 |
|
---|
204 | for (int I = 0; I < (control as DataGridView).Columns.Count; I++)
|
---|
205 | {
|
---|
206 | if (regKey.GetValue("ColWidth" + I.ToString()) != null)
|
---|
207 | (control as DataGridView).Columns[I].Width = (int)regKey.GetValue("ColWidth" + I.ToString());
|
---|
208 | }
|
---|
209 | }
|
---|
210 | if (control is SplitContainer)
|
---|
211 | {
|
---|
212 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true);
|
---|
213 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name);
|
---|
214 |
|
---|
215 | if (regKey.GetValue("SplitterDistance") != null)
|
---|
216 | (control as SplitContainer).SplitterDistance = (int)regKey.GetValue("SplitterDistance");
|
---|
217 | }
|
---|
218 |
|
---|
219 | foreach (Control child in control.Controls)
|
---|
220 | {
|
---|
221 | LoadControl(child);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | public void SaveControl(Control control)
|
---|
226 | {
|
---|
227 | if (control is ListView)
|
---|
228 | {
|
---|
229 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true);
|
---|
230 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name);
|
---|
231 |
|
---|
232 | for (int I = 0; I < (control as ListView).Columns.Count; I++)
|
---|
233 | {
|
---|
234 | regKey.SetValue("ColWidth" + I.ToString(), (control as ListView).Columns[I].Width);
|
---|
235 | }
|
---|
236 | } else
|
---|
237 | if (control is DataGridView)
|
---|
238 | {
|
---|
239 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true);
|
---|
240 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name);
|
---|
241 |
|
---|
242 | for (int I = 0; I < (control as DataGridView).Columns.Count; I++)
|
---|
243 | {
|
---|
244 | regKey.SetValue("ColWidth" + I.ToString(), (control as DataGridView).Columns[I].Width);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | if (control is SplitContainer)
|
---|
248 | {
|
---|
249 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true);
|
---|
250 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name);
|
---|
251 |
|
---|
252 | regKey.SetValue("SplitterDistance", (control as SplitContainer).SplitterDistance);
|
---|
253 | }
|
---|
254 |
|
---|
255 | foreach (Control child in control.Controls)
|
---|
256 | {
|
---|
257 | SaveControl(child);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public static class RectangleExt
|
---|
263 | {
|
---|
264 | public static Point Center(this Rectangle rect)
|
---|
265 | {
|
---|
266 | return new Point(rect.Left + rect.Width / 2,
|
---|
267 | rect.Top + rect.Height / 2);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | } |
---|