Self Placing Window (save form window related information to Registry) : Form Style « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » GUI Windows Form » Form StyleScreenshots 
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(288199);

            this.buttonChooseColor.Location = new System.Drawing.Point(0208);
            this.buttonChooseColor.Size = new System.Drawing.Size(10423);
            this.buttonChooseColor.Text = "Choose Color";

            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(292232);
            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
1.Non-resizable formNon-resizable form
2.BorderLess WindowBorderLess Window
3.AutoScroll WindowAutoScroll Window
4.Not in TaskBarNot in TaskBar
5.MinimumWindow Size
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.