开发QQ桌球瞄准器(5):使用注册表保存配置

 本节介绍如何使用注册表保存程序的配置信息,方便我们再次运行程序的时候加载上次的设置数据。
 
注册表读写操作
.NET提供了Microsoft.Win32.Registry对注册表进行了封装。
要获取 RegistryKey 的实例,请使用 Registry 类的静态成员之一。
 
注册表充当计算机上操作系统和应用程序的中央信息储存库。注册表根据存储在其中的元素的逻辑顺序,以分层形式组织(有关该层次结构中的基级项,请参见 Registry)。在注册表中存储信息时,请根据存储的信息类型选择适当的位置。一定要避免损坏由其他应用程序创建的信息,原因是这样会导致那些应用程序出现意外的行为,并且还会对您自己的应用程序带来不利影响。
 
注册表项是注册表中的基本组织单位,好比是 Windows 资源管理器中的文件夹。每个具体的注册表项都可以有子项,就像文件夹下可以有子文件夹一样。只要用户具有相应的权限,且注册表项不是基项或基项的下一级项,就可以删除该注册表项。每个注册表项也可带有与其相关联的多个值(一个值就好比是一个文件),它们用于存储信息,例如,有关计算机上安装的应用程序的信息。每个值存储特定的信息,可按需要对其进行检索或更新。例如,可以为您的公司创建一个 RegistryKey(在项 HKEY_LOCAL_MACHINE\Software 下),然后为您的公司创建的每个应用程序创建一个子项。每个子项保存特定于该应用程序的信息,如颜色设置、屏幕位置和大小或者可识别的文件扩展名。
 
下面的代码示例演示如何在 HKEY_CURRENT_USER 下创建一个子项,处理相应的内容,然后删除该子项。
 

using System;
using System.Security.Permissions;
using Microsoft.Win32;
 
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
    ViewAndModify = "HKEY_CURRENT_USER")]
class RegKey
{
    static void Main()
    {
        // Create a subkey named Test9999 under HKEY_CURRENT_USER.
        RegistryKey test9999 =
            Registry.CurrentUser.CreateSubKey("Test9999");
        // Create two subkeys under HKEY_CURRENT_USER\Test9999. The
        // keys are disposed when execution exits the using statement.
        using(RegistryKey
            testName = test9999.CreateSubKey("TestName"),
            testSettings = test9999.CreateSubKey("TestSettings"))
        {
            // Create data for the TestSettings subkey.
            testSettings.SetValue("Language", "French");
            testSettings.SetValue("Level", "Intermediate");
            testSettings.SetValue("ID", 123);
        }
 
        // Print the information from the Test9999 subkey.
        Console.WriteLine("There are {0} subkeys under {1}.",
            test9999.SubKeyCount.ToString(), test9999.Name);
        foreach(string subKeyName in test9999.GetSubKeyNames())
        {
            using(RegistryKey
                tempKey = test9999.OpenSubKey(subKeyName))
            {
                Console.WriteLine("\nThere are {0} values for {1}.",
                    tempKey.ValueCount.ToString(), tempKey.Name);
                foreach(string valueName in tempKey.GetValueNames())
                {
                    Console.WriteLine("{0,-8}: {1}", valueName,
                        tempKey.GetValue(valueName).ToString());
                }
            }
        }
 
        using(RegistryKey
            testSettings = test9999.OpenSubKey("TestSettings", true))
        {
            // Delete the ID value.
            testSettings.DeleteValue("id");
 
            // Verify the deletion.
            Console.WriteLine((string)testSettings.GetValue(
                "id", "ID not found."));
        }
 
        // Delete or close the new subkey.
        Console.Write("\nDelete newly created registry key? (Y/N) ");
        if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
        {
            Registry.CurrentUser.DeleteSubKeyTree("Test9999");
            Console.WriteLine("\nRegistry key {0} deleted.",
                test9999.Name);
        }
        else
        {
            Console.WriteLine("\nRegistry key {0} closed.",
                test9999.ToString());
            test9999.Close();
        }
    }
}
 

保存数据到注册表
 
1.新建类ConfigInfo类及方法来写入和读取注册表的操作。代码如下:

    public class ConfigInfo
    {
        public const string subkey = "tq";
        Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(subkey);
 
        //写入透明度
        public void WriteOpacity(int value)
        {
            if (key != null)
                key.SetValue("opacity", value);
        }
        //获取透明度
        public int GetOpacity()
        {
            if (key != null)
                return (int)key.GetValue("opacity", 80);
            return 80;
        }
        //写入线宽
        public void WriteLineWidth(int value)
        {
            if (key != null)
                key.SetValue("linewidth", value);
        }
        //获取线宽
        public int GetLineWidth()
        {
            if (key != null)
                return (int)key.GetValue("linewidth", 5);
            return 5;
        }
        //写入球体大小
        public void WriteBallSize(int value)
        {
            if (key != null)
                key.SetValue("ballsize", value);
        }
        //获取球体大小
        public int GetBallSize()
        {
            if (key != null)
                return (int)key.GetValue("ballsize", 30);
            return 30;
        }
        ~ConfigInfo()
        {
            Close();
        }
 
        public void Close()
        {
            if (key != null)
                key.Close();
        }
    }
 

2.为Form类添加如下方法用于加载和写入注册表信息。

        //从注册表加载配置
        private void LoadConfigInfo()
        {
            ConfigInfo info = new ConfigInfo();
            frmOpacityTrackBar.Value = info.GetOpacity();
            penWeigthTrackBar.Value = info.GetLineWidth();
            ballSizeTrackBar.Value = info.GetBallSize();
        }
        //写配置到注册表
        private void WriteConfigInfo()
        {
            ConfigInfo info = new ConfigInfo();
            info.WriteOpacity(frmOpacityTrackBar.Value);
            info.WriteLineWidth(penWeigthTrackBar.Value);
            info.WriteBallSize(ballSizeTrackBar.Value);
        }
 

3.为Form添加Load事件
在Load事件里调用LoadConfigInfo方法加载注册表中保存的数据。

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadConfigInfo();
            this.Opacity = frmOpacityTrackBar.Value;
            penWidth = penWeigthTrackBar.Value / 10;
            radius = radius_base + ballSizeTrackBar.Value / 10;
        }
 

4.为Form添加FormClosing事件
在FormClosing事件里调用WriteConfigInfo方法写入数据到注册表中。

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            WriteConfigInfo();
        }

问题描述

至此,我们的桌球瞄准器功能可以说已经基本完成了。你可以试着运行,体验一下自己开发的瞄准器。还存在的问题是当我们瞄准的时候,需要用瞄准线将目标球准确的一分为二才算是精确的击球线路,如下图所示:


如果稍偏一点平分目标球不准确的话,对于一些角度比较难进的球来说也不是很好瞄准。为了解决这个问题我们将在下一节介绍如何绘制一个目标球来更精确的辅助瞄准!

代码下载
http://dev.mjxy.cn/uploadfiles/files/taiqiu_step5.zip

参考资料
http://msdn.microsoft.com/zh-cn/library/ad51f2dx(VS.80).aspx

 

转载请注明: 敏捷学院-技术资源库  http://mjxy.cn  作者:邢泉  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值