关于注册表问题

赢光小虾米 2007-02-03 11:13:33
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的方法.注释部分如何修改.
...全文
201 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赢光小虾米 2007-02-05
  • 打赏
  • 举报
回复
没有人回答我吗?
赢光小虾米 2007-02-03
  • 打赏
  • 举报
回复
我不是说了吗?在dispose()方法里面写过SaveSettings() 了.这个意思就是说当程序正常中断时,就调用SaveSettings()方法.这个贮存颜色和大小都行.可以保存Form位置不行啊!为什么 .
王集鹄 2007-02-03
  • 打赏
  • 举报
回复
我这里用按钮调用ReadSettings\SaveSettings没有问题
估计是调用的位置出现问题
SaveSettings放到Form1_FormClosed

你可以通过多种测试缩小错误的范围,以便找到错误的关键触发原因。
王集鹄 2007-02-03
  • 打赏
  • 举报
回复
ReadSettings放到Form1_Load中看看
别放在构造方法里

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧