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; // declare the Registry object
22 myReg1 = Registry. CurrentUser; // obtain the current user Registry key
23 try
24 {
25 myReg2 = myReg1.CreateSubKey ("Software \ MySoft"); // create a subitem in the Registry key
26 this. Location = new Point (Convert. ToInt16 (myReg2.GetValue ("1"), Convert. ToInt16 (myReg2.GetValue ("2"); // set the display position of the form
27}
28 catch {}
29}
30
31 private void Frm_Main_FormClosed (object sender, FormClosedEventArgs e)
32 {
33 RegistryKey myReg1, myReg2; // declare the Registry object
34 myReg1 = Registry. CurrentUser; // obtain the current user Registry key
35 myReg2 = myReg1.CreateSubKey ("Software \ MySoft"); // create a subitem in the Registry key
36 try
37 {
38 myReg2.SetValue ("1", this. Location. X. ToString (); // write the x coordinates at the close position of the form to the Registry.
39 myReg2.SetValue ("2", this. Location. Y. ToString (); // write the y coordinate at the close position of the form to the Registry.
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 // required designer variables.
7 /// </summary>
8 private System. ComponentModel. IContainer components = null;
9
10 /// <summary>
11 /// clear all resources in use.
12 /// </summary>
13 /// <param name = "disposing"> true if managed resources should be released; otherwise, 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 code generated by Windows Form Designer
24
25 /// <summary>
26 // The designer supports the required methods-do not
27 // use the code editor to modify the content of this method.
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 = "starting the form from the last closed position ";
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}
From Mo Mingqi Miao