Self Placing Window (save form window related information to Registry)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
public class Form1 : Form
{
private System.Windows.Forms.ListBox listBoxMessages;
private System.Windows.Forms.Button buttonChooseColor;
private ColorDialog chooseColorDialog = new ColorDialog();
public Form1()
{
this.listBoxMessages = new System.Windows.Forms.ListBox();
this.buttonChooseColor = new System.Windows.Forms.Button();
this.SuspendLayout();
this.listBoxMessages.Size = new System.Drawing.Size(288, 199);
this.buttonChooseColor.Location = new System.Drawing.Point(0, 208);
this.buttonChooseColor.Size = new System.Drawing.Size(104, 23);
this.buttonChooseColor.Text = "Choose Color";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 232);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonChooseColor,
this.listBoxMessages});
this.ResumeLayout(false);
buttonChooseColor.Click += new EventHandler(OnClickChooseColor);
try
{
if (ReadSettings() == false)
listBoxMessages.Items.Add("No information in registry");
else
listBoxMessages.Items.Add("Information read in from registry");
StartPosition = FormStartPosition.Manual;
}
catch (Exception e)
{
listBoxMessages.Items.Add("A problem occurred reading in data from registry:");
listBoxMessages.Items.Add(e.Message);
}
}
void OnClickChooseColor(object Sender, EventArgs e)
{
if (chooseColorDialog.ShowDialog() == DialogResult.OK)
BackColor = chooseColorDialog.Color;
}
void SaveSettings()
{
RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software", true);
RegistryKey wroxKey = softwareKey.CreateSubKey("JJJ");
RegistryKey selfPlacingWindowKey = wroxKey.CreateSubKey("SelfPlacingWindow");
selfPlacingWindowKey.SetValue("BackColor", (object)BackColor.ToKnownColor());
selfPlacingWindowKey.SetValue("Red", (object)(int)BackColor.R);
selfPlacingWindowKey.SetValue("Green", (object)(int)BackColor.G);
selfPlacingWindowKey.SetValue("Blue", (object)(int)BackColor.B);
selfPlacingWindowKey.SetValue("Width", (object)Width);
selfPlacingWindowKey.SetValue("Height", (object)Height);
selfPlacingWindowKey.SetValue("X", (object)DesktopLocation.X);
selfPlacingWindowKey.SetValue("Y", (object)DesktopLocation.Y);
selfPlacingWindowKey.SetValue("WindowState", (object)WindowState.ToString());
}
bool ReadSettings()
{
RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software");
RegistryKey wroxKey = softwareKey.OpenSubKey("JJJ");
if (wroxKey == null)
return false;
RegistryKey selfPlacingWindowKey =
wroxKey.OpenSubKey("SelfPlacingWindow");
if (selfPlacingWindowKey == null)
return false;
else
listBoxMessages.Items.Add("Successfully opened key " + selfPlacingWindowKey.ToString());
int redComponent = (int)selfPlacingWindowKey.GetValue("Red");
int greenComponent = (int)selfPlacingWindowKey.GetValue("Green");
int blueComponent = (int)selfPlacingWindowKey.GetValue("Blue");
this.BackColor = Color.FromArgb(redComponent, greenComponent, blueComponent);
listBoxMessages.Items.Add("Background color: " + BackColor.Name);
int X = (int)selfPlacingWindowKey.GetValue("X");
int Y = (int)selfPlacingWindowKey.GetValue("Y");
this.DesktopLocation = new Point(X, Y);
listBoxMessages.Items.Add("Desktop location: " + DesktopLocation.ToString());
this.Height = (int)selfPlacingWindowKey.GetValue("Height");
this.Width = (int)selfPlacingWindowKey.GetValue("Width");
listBoxMessages.Items.Add("Size: " + new Size(Width, Height).ToString());
string initialWindowState = (string)selfPlacingWindowKey.GetValue("WindowState");
listBoxMessages.Items.Add("Window State: " + initialWindowState);
this.WindowState = (FormWindowState)FormWindowState.Parse
(WindowState.GetType(), initialWindowState);
return true;
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
Related examples in the same category