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é)合,能不能把窗口的位置和大小記錄下來呢,當(dāng)然可以,首先要做的事聲明一個(gè)結(jié)構(gòu)體,用來保存大小和位置還有狀態(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)文章
Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(2)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲的第二部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Unity3D動(dòng)態(tài)對(duì)象優(yōu)化代碼分享
這篇文章主要介紹了Unity3D動(dòng)態(tài)對(duì)象優(yōu)化代碼分享的相關(guān)資料,需要的朋友可以參考下2015-03-03C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的示例詳解
AForge是一個(gè)專門為開發(fā)者和研究者基于C#框架設(shè)計(jì)的,這個(gè)框架提供了不同的類庫和關(guān)于類庫的資源,本文為大家介紹了C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下2023-11-11DevExpress實(shí)現(xiàn)TreeList節(jié)點(diǎn)互斥的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList節(jié)點(diǎn)互斥的方法,對(duì)于初學(xué)者更好的理解C#有一定的幫助,需要的朋友可以參考下2014-08-08C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
這篇文章主要介紹了C#導(dǎo)入導(dǎo)出EXCEL文件代碼實(shí)例,代碼的流程和方法都很詳細(xì),需要的朋友可以參考下2014-04-04C#中38個(gè)常用運(yùn)算符的優(yōu)先級(jí)的劃分和理解
這只我自己在學(xué)C#中的一些總結(jié),其中對(duì)于各級(jí)的劃分方式、各操作符的優(yōu)先級(jí)的理解并不見得正確,只是自己的看法,拿出來與大家分享2012-08-08