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;namespace SelfPlacingWindow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Click += new EventHandler(button1_Click);
            try
            {
                if (ReadSettings() == false)
                {
                    listBox1.Items.Add("No information in registry");
                }
                else
                {
                    listBox1.Items.Add("Information read in from registry");
                }            }
            catch(Exception e)
            {
                listBox1.Items.Add("A problem occurred reading in data form registry");
                listBox1.Items.Add(e.Message);
            }
        }
        ColorDialog Cdialog = new ColorDialog();        private void button1_Click(object sender, EventArgs e)
        {
            
            if (Cdialog.ShowDialog ()==DialogResult.OK)
            {
                BackColor = Cdialog.Color;
                return;
            }
        }
        private void SaveSettings()
        {
            RegistryKey SoftwareKey = Registry.LocalMachine.OpenSubKey("Software", true);
            RegistryKey WroxKey = SoftwareKey.CreateSubKey("WroxPress");
            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());
        }
        private bool ReadSettings()
        {
            RegistryKey SoftwareKey = Registry.LocalMachine.OpenSubKey("Software");
            RegistryKey WroxKey = SoftwareKey.OpenSubKey("WroxPress");
            if(WroxKey == null)
                return false;
            RegistryKey SelfPlacingWindowKey = WroxKey.OpenSubKey("SelfPlacingWindow");
            if (SelfPlacingWindowKey == null)
                return false;
            else
                listBox1.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");
            BackColor = Color.FromArgb(RedComponent ,GreenComponent ,BlueComponent);
            listBox1.Items.Add("Background color:"+ BackColor.Name);
            int X = (int)SelfPlacingWindowKey.GetValue("X");
            int Y = (int)SelfPlacingWindowKey.GetValue("Y");
            DesktopLocation = new Point(X,Y );
            listBox1.Items.Add("Desktop lacation:" + DesktopLocation.ToString ());
            Height = (int)SelfPlacingWindowKey.GetValue("Height");
            Width = (int)SelfPlacingWindowKey.GetValue("Width");
            listBox1.Items.Add("Size:"+ new Size (Height,Width).ToString ());
            string InitialWindowState = (string)SelfPlacingWindowKey.GetValue("WindowState");
            listBox1.Items.Add("Window State:"+InitialWindowState);
           // WindowState = (FormWindowState)FormWindowState.Parse(WindowState.GetType (),InitialWindowState);
            return true;
            
            
        }
    }
}
记住上面还有一句在写在dispose()方法里.就是SaveSettings()方法.
书上抄的原码:为什么再次启动Form的位置不能固定啊!颜色和大小都能固定.这是什么原因啊!
上面注释的部分在2005上不能用.formwindowstate没有parse的方法.注释部分如何修改.

解决方案 »

  1.   

    ReadSettings放到Form1_Load中看看
    别放在构造方法里
      

  2.   

    我这里用按钮调用ReadSettings\SaveSettings没有问题
    估计是调用的位置出现问题
    SaveSettings放到Form1_FormClosed你可以通过多种测试缩小错误的范围,以便找到错误的关键触发原因。
      

  3.   

    我不是说了吗?在dispose()方法里面写过SaveSettings() 了.这个意思就是说当程序正常中断时,就调用SaveSettings()方法.这个贮存颜色和大小都行.可以保存Form位置不行啊!为什么 .