Introduction
I find it really annoying having to constantly reposition and size certain windows the way I like them in some of the application I use regularly. This code allows easy saving and loading of forms and controls positioning and size, making your WinForm application more user friendly.
Using the code
To load the data, place code after InitializeComponent()
in the Form
's constructor.
public Form1(){
InitializeComponent();
McGiv.Win32.Forms.Positioning.Load(this,
RegistryHive.CurrentUser, "regSubKey");
}
To save the data, place code in OnClosing
method of the Form
.
protected override void OnClosing( System.ComponentModel.CancelEventArgs e){
McGiv.Win32.Forms.Positioning.Save(this,
RegistryHive.CurrentUser, "regSubKey");
}
The whole code listing:
using System;
using Microsoft.Win32;
using System.Windows.Forms;
namespace McGiv.Win32.Forms {
internal class Positioning {
public static void Save(Control control,
RegistryHive topLevel, string subKey){
if( control == null )
{
throw new ArgumentException("The control cannot be null");
}
Microsoft.Win32.RegistryKey key = Open(topLevel, subKey);
Form form = control as Form;
if( form != null ){
key.SetValue(form.Name + "_windowState", (int)form.WindowState);
if( form.WindowState != FormWindowState.Normal ){
form.WindowState = FormWindowState.Normal;
}
}
key.SetValue(control.Name + "_screenHeight",
Screen.PrimaryScreen.Bounds.Height);
key.SetValue(control.Name + "_screenWidth",
Screen.PrimaryScreen.Bounds.Width);
key.SetValue(control.Name + "_top", control.Top);
key.SetValue(control.Name + "_left", control.Left);
key.SetValue(control.Name + "_width", control.Width);
key.SetValue(control.Name + "_height", control.Height);
key.Close();
}
public static void Load(Control control,
RegistryHive topLevel, string subKey){
if( control == null ){
throw new ArgumentException("The control cannot be null");
}
Microsoft.Win32.RegistryKey key = Open(topLevel, subKey);
try{
if(Screen.PrimaryScreen.Bounds.Height !=
(int)key.GetValue(control.Name + "_screenHeight") ||
Screen.PrimaryScreen.Bounds.Width !=
(int)key.GetValue(control.Name + "_screenWidth") ){
return;
}
}
catch(NullReferenceException){return;}
control.SuspendLayout();
Form form = control as Form;
if( form != null ){
try{
form.WindowState =
(FormWindowState)key.GetValue(form.Name +
"_windowState");
form.StartPosition = FormStartPosition.Manual;
}
catch(NullReferenceException){}
}
try{
control.Top = (int)key.GetValue(control.Name + "_top");
}
catch(NullReferenceException){}
try{
control.Left = (int)key.GetValue(control.Name + "_left");
}
catch(NullReferenceException){}
try{
control.Width = (int)key.GetValue(control.Name + "_width");
}
catch(NullReferenceException){}
try{
control.Height = (int)key.GetValue(control.Name + "_height");
}
catch(NullReferenceException){}
control.ResumeLayout();
key.Close();
}
private static Microsoft.Win32.RegistryKey
Open(RegistryHive topLevel, string subKey){
switch( topLevel){
case RegistryHive.ClassesRoot:{
return Registry.CurrentUser.CreateSubKey(subKey);
}
case RegistryHive.CurrentConfig:{
return Registry.CurrentConfig.CreateSubKey(subKey);
}
case RegistryHive.CurrentUser:{
return Registry.CurrentUser.CreateSubKey(subKey);
}
case RegistryHive.DynData:{
return Registry.DynData.CreateSubKey(subKey);
}
case RegistryHive.LocalMachine:{
return Registry.LocalMachine.CreateSubKey(subKey);
}
case RegistryHive.PerformanceData:{
return Registry.PerformanceData.CreateSubKey(subKey);
}
case RegistryHive.Users:{
return Registry.Users.CreateSubKey(subKey);
}
default:{
return null;
}
}
}
}
}