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 }
摘自 墨明棋妙