c#保存窗口位置大小操作類(序列化和文件讀寫功能)
記錄窗口上次關(guān)閉的位置和大小
namespace PDSafe.Base
{
public class Setting
{
///<summary>
/// 把對(duì)象序列化為字節(jié)數(shù)組
///</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>
/// 把字節(jié)數(shù)組反序列化成對(duì)象
///</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)
{
//如果不存在創(chuàng)建文件
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();
//分析內(nèi)容
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);
}
}
}
}
基本功能就是保存一個(gè)結(jié)構(gòu)體類型的數(shù)據(jù)
bool Save(filePath,value,true);
還有讀取被保存數(shù)據(jù)的文件,從中讀取,這個(gè)結(jié)構(gòu)體被裝箱,要做的只是拆箱
object result = Save(filePath,將要保存的數(shù)據(jù)實(shí)例,true)
if(result != null)//確認(rèn)文件存在且讀取成功
將這兩個(gè)功能結(jié)合,能不能把窗口的位置和大小記錄下來(lái)呢,當(dāng)然可以,首先要做的事聲明一個(gè)結(jié)構(gòu)體,用來(lái)保存大小和位置還有狀態(tài)
[Serializable]
public struct FormSizeandLocation
{
public int SizeW;
public int SizeH;
public int LocationX;
public int LocationY;
public int Style;
}
然后進(jìn)行保存和設(shè)置,代碼108-172行都是對(duì)于它的處理,How does it work?
讓用戶給出一個(gè)窗口實(shí)例
訂閱實(shí)例的 Load和Closing事件
在load事件中把保存的文件讀取,并更改實(shí)例的位置和大小
在closing事件中把大小和位置保存
AddRenewFormSizeControl(this);
//只需一句代碼,一定要寫在InitializeComponent函數(shù)后。不能寫在load事件里
注意,保存的文件是 工作路徑+Location.set 你也可以自己改寫此類。
相關(guān)文章
C#實(shí)現(xiàn)用于操作wav聲音文件的類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)用于操作wav聲音文件的類,實(shí)例分析了C#操作wav音頻文件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中
這篇文章主要介紹了C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中,是遞歸算法的一個(gè)簡(jiǎn)單應(yīng)用,需要的朋友可以參考下2014-10-10
C# 使用 OleDbConnection 連接讀取Excel的方法
這篇文章主要介紹了C# 使用 OleDbConnection 連接讀取Excel的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
C#?WPF后臺(tái)動(dòng)態(tài)添加控件實(shí)戰(zhàn)教程
最近嘗試用wpf在后臺(tái)動(dòng)態(tài)添加控件,所以下面這篇文章主要給大家介紹了關(guān)于C#?WPF后臺(tái)動(dòng)態(tài)添加控件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

