Simple class to save your Form's Size To Registry
I just whipped this up EditPlus so there may be some syntax errors.using System;using System.Drawing;using System.Windows.Forms;using Microsoft.Win32; namespace Chico.Registry{ public struct SizeRegistry : IDisposable { private const string kWidthName =...
I just whipped this up EditPlus so there may be some syntax errors.
using System; using System.Drawing; using System.Windows.Forms; using Microsoft.Win32; namespace Chico.Registry { public struct SizeRegistry : IDisposable { private const string kWidthName = "FormWidth", kHeightName = "FormHeight"; private RegistryKey mKey; public SizeRegistry(string subkey) { using (var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default)) { mKey = key.CreateSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryOptions.Volatile); mKey.OpenSubKey(subkey); } } public Size GetSize() { int w, h; w = (int)mKey.GetValue(kWidthName, 320); h = (int)mKey.GetValue(kHeightName, 240); return new Size(w, h); } public void SetSize(Size size) { mKey.SetValue(kWidthName, size.Width, RegistryValueKind.DWord); mKey.SetValue(kHeightName, size.Height, RegistryValueKind.DWord); } public void Dipose() { mKey.Close(); mKey.Dispose(); } public static void StreamFormState(Form f, string subkey, bool is_opening) { using (var sr = new SizeRegistry(subkey)) { if (is_opening) f.Size = sr.GetSize(); else sr.SetSize(f.Size); } } }; } namespace WindowsFormsApplication1 { public partial class Form1 : Form { static readonly string kSizeSubkeyName = Application.ProductName; public Form1() { InitializeComponent(); SizeRegistry.StreamFormState(this, kSizeSubkeyName, true); } void Form1_FormClosing(object sender, FormClosingEventArgs e) { SizeRegistry.StreamFormState(this, kSizeSubkeyName, false); } }; }