当前位置:编程学习 > C#/ASP.NET >>

从上次关闭位置启动窗体

Frm_Main.cs
  View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 using Microsoft.Win32;
 9
10 namespace StartFormByLClosePosition
11 {
12     public partial class Frm_Main : Form
13     {
14         public Frm_Main()
15         {
16             InitializeComponent();
17         }
18
19         private void Frm_Main_Load(object sender, EventArgs e)
20         {
21             RegistryKey myReg1, myReg2;//声明注册表对象
22             myReg1 = Registry.CurrentUser;//获取当前用户注册表项
23             try
24             {
25                 myReg2 = myReg1.CreateSubKey("Software\\MySoft");//在注册表项中创建子项
26                 this.Location = new Point(Convert.ToInt16(myReg2.GetValue("1")), Convert.ToInt16(myReg2.GetValue("2")));//设置窗体的显示位置
27             }
28             catch { }
29         }
30
31         private void Frm_Main_FormClosed(object sender, FormClosedEventArgs e)
32         {
33             RegistryKey myReg1, myReg2;//声明注册表对象
34             myReg1 = Registry.CurrentUser;//获取当前用户注册表项
35             myReg2 = myReg1.CreateSubKey("Software\\MySoft");//在注册表项中创建子项
36             try
37             {
38                 myReg2.SetValue("1", this.Location.X.ToString());//将窗体关闭位置的x坐标写入注册表
39                 myReg2.SetValue("2", this.Location.Y.ToString());//将窗体关闭位置的y坐标写入注册表
40             }
41             catch { }
42         }
43     }
44 }

Frm_Main.designer.cs
  View Code
 1 namespace StartFormByLClosePosition
 2 {
 3     partial class Frm_Main
 4     {
 5         /// <summary>
 6 /// 必需的设计器变量。
 7 /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9
10         /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22
23         #region Windows 窗体设计器生成的代码
24
25         /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29         private void InitializeComponent()
30         {
31             this.SuspendLayout();
32             //
33 // Frm_Main
34 //
35             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
36             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
37             this.ClientSize = new System.Drawing.Size(264, 104);
38             this.Name = "Frm_Main";
39             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
40             this.Text = "从上次关闭位置启动窗体";
41             this.Load += new System.EventHandler(this.Frm_Main_Load);
42             this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Frm_Main_FormClosed);
43             this.ResumeLayout(false);
44
45         }
46
47         #endregion
48     }
49 }

 

摘自 墨明棋妙
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,