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的方法.注释部分如何修改.
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的方法.注释部分如何修改.
解决方案 »
- C# winform 输入完了后 textbox控件 触发什么事件比较好???
- socket 用smtp发送邮件问题
- 一个有很有意思的API问题,PostMessage/SendMessage/keybd_event
- 请教一个二叉树递归查询的问题(在线急等C#高人帮忙,非常感谢
- 关于DropDownList控件的问题.
- 求验证文件路径的通用正则表达式
- 30岁的人来看一看--我实在没办法,就是无法保存数据vs2005---C#
- 请问如何在编码中显示oledb联接输入框
- 如何做一个windows服务程序(提供http channel服务)
- 按钮点击事件,调用js写的函数,出错
- winform问题,怎样把多行输入的textbox的每一行,作为一个list的item添加进去?
- winform问题:关于随机取值的问题,没分了,就这么点了啊
别放在构造方法里
估计是调用的位置出现问题
SaveSettings放到Form1_FormClosed你可以通过多种测试缩小错误的范围,以便找到错误的关键触发原因。