• 记录窗体关闭位置(从窗体上次关闭的位置启动窗体)


    从上次关闭位置启动窗体

    基础类

    1. using Microsoft.Win32;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading.Tasks;
    7. using System.Xml.Linq;
    8. namespace WindowsFormsApp1
    9. {
    10. public class Reg
    11. {
    12. public static void Set(string name, object value,string item = "Software\\MySoft_ttd")
    13. {
    14. RegistryKey myReg1, myReg2;//声明注册表对象
    15. myReg1 = Registry.CurrentUser;//获取当前用户注册表项
    16. myReg2 = myReg1.CreateSubKey(item);//在注册表项中创建子项
    17. myReg2.SetValue(name, value);//将窗体关闭位置的x坐标写入注册表
    18. }
    19. public static object Get(string name, string item = "Software\\MySoft_ttd")
    20. {
    21. RegistryKey myReg1, myReg2;//声明注册表对象
    22. myReg1 = Registry.CurrentUser;//获取当前用户注册表项
    23. try
    24. {
    25. myReg2 = myReg1.CreateSubKey(item);//在注册表项中创建子项
    26. return myReg2.GetValue(name);
    27. }
    28. catch (Exception ex)
    29. {
    30. return "0";
    31. }
    32. }
    33. }
    34. }

    调用方法

    1. private void Form1_Load(object sender, EventArgs e)
    2. {
    3. //this.Location = new Point(Convert.ToInt32(Reg.Get("my_x")), Convert.ToInt32(Reg.Get("my_y")));
    4. //https://blog.csdn.net/u014453443/article/details/90040423
    5. //if (this.Location.X >= Screen.PrimaryScreen.Bounds.Width || this.Location.Y >= Screen.PrimaryScreen.Bounds.Height)
    6. //{
    7. // this.Location = new Point(0, 0);
    8. //}
    9. this.Left = Convert.ToInt32(Reg.Get("my_x"));
    10. this.Top = Convert.ToInt32(Reg.Get("my_y"));
    11. if (this.Left >= Screen.PrimaryScreen.Bounds.Width || this.Top >= Screen.PrimaryScreen.Bounds.Height)
    12. {
    13. this.Left =0;
    14. this.Top = 0;
    15. }
    16. }
    17. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    18. {
    19. //Reg.Set("my_x", this.Location.X.ToString());
    20. //Reg.Set("my_y", this.Location.Y.ToString());
    21. Reg.Set("my_x", this.Left.ToString());
    22. Reg.Set("my_y", this.Top.ToString());
    23. }

    详细可以参考

     C#开发实例大全  基础卷

    159例程

    注册表内记录的信息

    特此记录

    anlog

    2023年9月9日

  • 相关阅读:
    Apple官方优化Stable Diffusion绘画教程
    Java 注解与反射
    行为型模式-备忘录模式
    Intel OpenVINO 安装显卡驱动
    TIA博途V17中ProDiag功能的使用方法示例(二)可编辑的文本框
    自建Elasticsearch 集群的规划和常见问题
    GCC
    饿了么三面:让你怀疑人生的Spring Boot夺命连环40问
    【LeetCode】-- 236. 二叉树的最近公共祖先
    Leetcode Practice -- 数组
  • 原文地址:https://blog.csdn.net/anlog/article/details/132782546