| 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 | public 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 static 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 static bool IsVisibleOnAnyScreen(Rectangle rect)
|
|---|
| 56 | {
|
|---|
| 57 | const 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 | Form = form;
|
|---|
| 73 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name) ??
|
|---|
| 74 | 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 | }
|
|---|
| 141 | form.Visible = prevVisibleState;
|
|---|
| 142 | LoadControl(form);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | public void Save(Form form, Form parentForm = null)
|
|---|
| 146 | {
|
|---|
| 147 | Form = form;
|
|---|
| 148 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name, true) ??
|
|---|
| 149 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + form.Name);
|
|---|
| 150 |
|
|---|
| 151 | string name = "";
|
|---|
| 152 | if (parentForm != null) name = parentForm.Name;
|
|---|
| 153 |
|
|---|
| 154 | WINDOWPLACEMENT placement = GetPlacement(form);
|
|---|
| 155 | const int WPF_RESTORETOMAXIMIZED = 0x2;
|
|---|
| 156 |
|
|---|
| 157 | if ((form.WindowState == FormWindowState.Minimized) &&
|
|---|
| 158 | ((placement.flags | WPF_RESTORETOMAXIMIZED) != WPF_RESTORETOMAXIMIZED))
|
|---|
| 159 | {
|
|---|
| 160 | regKey.SetValue("State" + name, (int)FormWindowState.Minimized);
|
|---|
| 161 | }
|
|---|
| 162 | else
|
|---|
| 163 | {
|
|---|
| 164 | regKey.SetValue("State" + name, (int)form.WindowState);
|
|---|
| 165 | }
|
|---|
| 166 | regKey.SetValue("DesktopBoundsLeft" + name, form.DesktopBounds.Left);
|
|---|
| 167 | regKey.SetValue("DesktopBoundsTop" + name, form.DesktopBounds.Top);
|
|---|
| 168 | regKey.SetValue("DesktopBoundsWidth" + name, form.DesktopBounds.Width);
|
|---|
| 169 | regKey.SetValue("DesktopBoundsHeight" + name, form.DesktopBounds.Height);
|
|---|
| 170 | regKey.SetValue("RestoreBoundsLeft" + name, form.RestoreBounds.Left);
|
|---|
| 171 | regKey.SetValue("RestoreBoundsTop" + name, form.RestoreBounds.Top);
|
|---|
| 172 | regKey.SetValue("RestoreBoundsWidth" + name, form.RestoreBounds.Width);
|
|---|
| 173 | regKey.SetValue("RestoreBoundsHeight" + name, form.RestoreBounds.Height);
|
|---|
| 174 | if (parentForm != null)
|
|---|
| 175 | {
|
|---|
| 176 | regKey.SetValue("ParentBoundsLeft" + name, parentForm.DesktopBounds.Left);
|
|---|
| 177 | regKey.SetValue("ParentBoundsTop" + name, parentForm.DesktopBounds.Top);
|
|---|
| 178 | regKey.SetValue("ParentBoundsWidth" + name, parentForm.DesktopBounds.Width);
|
|---|
| 179 | regKey.SetValue("ParentBoundsHeight" + name, parentForm.DesktopBounds.Height);
|
|---|
| 180 | }
|
|---|
| 181 | SaveControl(form);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | public void LoadControl(Control control)
|
|---|
| 185 | {
|
|---|
| 186 | switch (control)
|
|---|
| 187 | {
|
|---|
| 188 | case ListView listView:
|
|---|
| 189 | {
|
|---|
| 190 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name, true) ??
|
|---|
| 191 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name);
|
|---|
| 192 |
|
|---|
| 193 | for (int I = 0; I < listView.Columns.Count; I++)
|
|---|
| 194 | {
|
|---|
| 195 | object value = regKey.GetValue(listView.Columns[I].Text);
|
|---|
| 196 | if (value != null) listView.Columns[I].Width = (int)value;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | break;
|
|---|
| 200 | }
|
|---|
| 201 | case DataGridView dataGridView:
|
|---|
| 202 | {
|
|---|
| 203 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name, true) ??
|
|---|
| 204 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name);
|
|---|
| 205 |
|
|---|
| 206 | for (int I = 0; I < dataGridView.Columns.Count; I++)
|
|---|
| 207 | {
|
|---|
| 208 | object value = regKey.GetValue("ColWidth" + I);
|
|---|
| 209 | if (value != null) dataGridView.Columns[I].Width = (int)value;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | break;
|
|---|
| 213 | }
|
|---|
| 214 | case SplitContainer splitContainer:
|
|---|
| 215 | {
|
|---|
| 216 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name, true) ??
|
|---|
| 217 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name);
|
|---|
| 218 |
|
|---|
| 219 | object value = regKey.GetValue("SplitterDistance");
|
|---|
| 220 | if (value != null)
|
|---|
| 221 | splitContainer.SplitterDistance = (int)value;
|
|---|
| 222 | break;
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | foreach (Control child in control.Controls)
|
|---|
| 227 | {
|
|---|
| 228 | LoadControl(child);
|
|---|
| 229 | }
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | public void SaveControl(Control control)
|
|---|
| 233 | {
|
|---|
| 234 | switch (control)
|
|---|
| 235 | {
|
|---|
| 236 | case ListView listView:
|
|---|
| 237 | {
|
|---|
| 238 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name, true) ??
|
|---|
| 239 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name);
|
|---|
| 240 |
|
|---|
| 241 | for (int I = 0; I < listView.Columns.Count; I++)
|
|---|
| 242 | {
|
|---|
| 243 | regKey.SetValue(listView.Columns[I].Text, listView.Columns[I].Width);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | break;
|
|---|
| 247 | }
|
|---|
| 248 | case DataGridView dataGridView:
|
|---|
| 249 | {
|
|---|
| 250 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name, true) ??
|
|---|
| 251 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name);
|
|---|
| 252 |
|
|---|
| 253 | for (int I = 0; I < dataGridView.Columns.Count; I++)
|
|---|
| 254 | {
|
|---|
| 255 | regKey.SetValue("ColWidth" + I, dataGridView.Columns[I].Width);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | break;
|
|---|
| 259 | }
|
|---|
| 260 | case SplitContainer splitContainer:
|
|---|
| 261 | {
|
|---|
| 262 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name, true) ??
|
|---|
| 263 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name);
|
|---|
| 264 |
|
|---|
| 265 | regKey.SetValue("SplitterDistance", splitContainer.SplitterDistance);
|
|---|
| 266 | break;
|
|---|
| 267 | }
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | foreach (Control child in control.Controls)
|
|---|
| 271 | {
|
|---|
| 272 | SaveControl(child);
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | public static class RectangleExt
|
|---|
| 278 | {
|
|---|
| 279 | public static Point Center(this Rectangle rect)
|
|---|
| 280 | {
|
|---|
| 281 | return new Point(rect.Left + rect.Width / 2,
|
|---|
| 282 | rect.Top + rect.Height / 2);
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|