考完試了,階段性的休息一下,最近在寫一個數據加密軟件叫Privacy Data Safe ,這不是一個簡單的小軟件,而是一個功能十分強大的軟件,目前還處於初步的設計階段,但是一個成功的軟件總是在細節考慮得很周到,例如 :記錄窗口上次關閉的位置和大小,等等細節,但是技術鍍金還是要有個度的,太追求完美勢必適得其反,倒不如把這些東西封裝在一起,拿來就用
不說費話了,先看看這段代碼,其實就是把 序列化 和 文件讀寫 合到一塊了
namespace PDSafe.Base
{
public class Setting
{
///<summary>
/// 把對象序列化為字節數組
///</summary>
public static byte[] SerializeObject(object obj)
{
if (obj == null)
return null;
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, bytes.Length);
ms.Close();
return bytes;
}
///<summary>
/// 把字節數組反序列化成對象
///</summary>
public static object DeserializeObject(byte[] bytes)
{
object obj = null;
if (bytes == null)
return obj;
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
try
{
obj = formatter.Deserialize(ms);
}
catch { obj = null; }
ms.Close();
return obj;
}
public static bool Save(string path, object value, bool isCeranew)
{
//如果不存在創建文件
FileStream fs;
if ((!File.Exists(path)) && isCeranew)
{
try
{
fs = File.Create(path);
}
catch
{
return false;
}
}
//如果存在則打開
else
{
try
{
fs = File.Open(path, FileMode.Open, FileAccess.Write);
}
catch
{
return false;
}
}
//寫文件
byte[] buffer = SerializeObject(value);
try
{
for (long i = 0; i < buffer.LongLength; i++)
fs.WriteByte(buffer[i]);
}
catch
{
return false;
}
fs.Close();
return true;
}
public static object Read(string path)
{
FileStream fs;
try
{
fs = File.OpenRead(path);
}
catch
{
return null;
}
//讀入緩存
StreamReader sreader = new StreamReader(fs);
string str = sreader.ReadToEnd();
fs.Close();
sreader.Close();
//分析內容
byte[] buffer = Encoding.Default.GetBytes(str);
return DeserializeObject(buffer);
}
[Serializable]
public struct FormSizeandLocation
{
public int SizeW;
public int SizeH;
public int LocationX;
public int LocationY;
public int Style;
}
private static Setting.FormSizeandLocation fsp = new Setting.FormSizeandLocation();
public static void AddRenewFormSizeControl(Form form)
{
form.FormClosing += new FormClosingEventHandler(FormcloseEvent);
form.Load += new EventHandler(FormloadEvent);
}
private static void FormcloseEvent(object sender, EventArgs e)
{
Form form = (Form)sender;
switch (form.WindowState)
{
case FormWindowState.Maximized:
fsp.Style = 2;
fsp.SizeW = form.Width;
fsp.SizeH = form.Height;
fsp.LocationX = form.Location.X;
fsp.LocationY = form.Location.Y;
break;
case FormWindowState.Minimized:
fsp.Style = 1;
break;
case FormWindowState.Normal:
fsp.Style = 0;
fsp.SizeW = form.Width;
fsp.SizeH = form.Height;
fsp.LocationX = form.Location.X;
fsp.LocationY = form.Location.Y;
break;
}
Setting.Save(Directory.GetCurrentDirectory() + @"\" + "Location.set", fsp, true);
}
private static void FormloadEvent(object sender, EventArgs e)
{
Form form = (Form)sender;
object result = Setting.Read(Directory.GetCurrentDirectory() + @"\" + "Location.set");
if (result != null)
{
fsp = (Setting.FormSizeandLocation)result;
switch (fsp.Style)
{
case 2:
form.WindowState = FormWindowState.Maximized;
break;
default:
form.WindowState = FormWindowState.Normal;
break;
}
form.Left = fsp.LocationX;
form.Top = fsp.LocationY;
form.Size = new Size(fsp.SizeW, fsp.SizeH);
}
}
}
}
基本功能就是保存一個結構體類型的數據
bool Save(filePath,value,true);
還有讀取被保存數據的文件,從中讀取,這個結構體被裝箱,要做的只是拆箱
object result = Save(filePath,將要保存的數據實例,true)
if(result != null)//確認文件存在且讀取成功
...........
將這兩個功能結合,能不能把窗口的位置和大小記錄下來呢,當然可以,首先要做的事聲明一個結構體,用來保存大小和位置還有狀態
• [Serializable]
• public struct FormSizeandLocation
• {
• public int SizeW;
• public int SizeH;
• public int LocationX;
• public int LocationY;
• public int Style;
• }
然後進行保存和設置,代碼108-172行都是對於它的處理,How does it work?
• 讓用戶給出一個窗口實例
• 訂閱實例的 Load和Closing事件
• 在load事件中把保存的文件讀取,並更改實例的位置和大小
• 在closing事件中把大小和位置保存
AddRenewFormSizeControl(this);
//只需一句代碼,一定要寫在InitializeComponent函數後。不能寫在load事件裡
注意,保存的文件是 工作路徑+Location.set 你也可以自己改寫此類。
想了解的話看看怎麼實現的,想用的拿去就用沒必要了解工作原理。
沒什麼技術含量....
摘自 A Dream