1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
namespace Kosmo
{
public class FormStateSaver : Component
{
private Form form;
private bool saveSize = true;
private bool saveLocation = true;
private bool saveWindowState = true;
public Form FormToSave
{
get { return form; }
set
{
RemoveEventsFromForm();
form = value;
AddEventsToForm();
}
}
[DefaultValue(true)]
public bool SaveSize
{
get { return saveSize; }
set { saveSize = value; }
}
[DefaultValue(true)]
public bool SaveLocation
{
get { return saveLocation; }
set { saveLocation = value; }
}
[DefaultValue(true)]
public bool SaveWindowState
{
get { return saveWindowState; }
set { saveWindowState = value; }
}
private void AddEventsToForm()
{
if (!DesignMode && form != null)
{
form.Load += Form_Load;
form.FormClosing += Form_FormClosing;
}
}
private void RemoveEventsFromForm()
{
if (!DesignMode && form != null)
{
form.Load -= Form_Load;
form.FormClosing -= Form_FormClosing;
}
}
private void Form_Load(object sender, EventArgs e)
{
LoadFormState();
}
private void Form_FormClosing(object sender, EventArgs e)
{
SaveFormState();
}
protected string GetXmlPath()
{
return Path.Combine(Application.LocalUserAppDataPath, form.Name + ".xml");
}
public void LoadFormState()
{
if (form == null || (!saveLocation && !saveSize && !saveWindowState))
return;
string path = GetXmlPath();
if (File.Exists(path))
{
XmlDocument xml = new XmlDocument();
xml.Load(path);
if (xml["Form"] != null)
{
XmlNode root = xml["Form"];
if (saveWindowState && root["WindowState"] != null)
{
form.WindowState = (FormWindowState)Enum.Parse(typeof(FormWindowState), root["WindowState"].InnerText);
}
if (saveLocation && root["Location"] != null)
{
if (root["Location"]["X"] != null)
form.Left = Convert.ToInt32(root["Location"]["X"].InnerText);
if (root["Location"]["Y"] != null)
form.Top = Convert.ToInt32(root["Location"]["Y"].InnerText);
}
if (saveSize && root["Size"] != null)
{
if (root["Size"]["Width"] != null)
form.Width = Convert.ToInt32(root["Size"]["Width"].InnerText);
if (root["Size"]["Height"] != null)
form.Height = Convert.ToInt32(root["Size"]["Height"].InnerText);
}
}
}
}
public void SaveFormState()
{
if (form == null || (!saveLocation && !saveSize && !saveWindowState))
return;
XmlDocument xml = new XmlDocument();
XmlNode root = xml.AppendChild(xml.CreateElement("Form"));
Rectangle bounds = form.Bounds;
if (form.WindowState != FormWindowState.Normal)
{
bounds = form.RestoreBounds;
}
if (saveLocation)
{
XmlNode loc = root.AppendChild(xml.CreateElement("Location"));
loc.AppendChild(xml.CreateElement("X")).InnerText = bounds.X.ToString();
loc.AppendChild(xml.CreateElement("Y")).InnerText = bounds.Y.ToString();
}
if (saveSize)
{
XmlNode size = root.AppendChild(xml.CreateElement("Size"));
size.AppendChild(xml.CreateElement("Width")).InnerText = bounds.Width.ToString();
size.AppendChild(xml.CreateElement("Height")).InnerText = bounds.Height.ToString();
}
if (saveWindowState)
{
root.AppendChild(xml.CreateElement("WindowState")).InnerText = form.WindowState.ToString();
}
xml.Save(GetXmlPath());
}
}
}
|