c#保存窗口位置大小操作类(序列化和文件读写功能)
记录窗口上次关闭的位置和大小
namespacePDSafe.Base
{
publicclassSetting
{
///<summary>
///把对象序列化为字节数组
///</summary>
publicstaticbyte[]SerializeObject(objectobj)
{
if(obj==null)
returnnull;
MemoryStreamms=newMemoryStream();
BinaryFormatterformatter=newBinaryFormatter();
formatter.Serialize(ms,obj);
ms.Position=0;
byte[]bytes=newbyte[ms.Length];
ms.Read(bytes,0,bytes.Length);
ms.Close();
returnbytes;
}
///<summary>
///把字节数组反序列化成对象
///</summary>
publicstaticobjectDeserializeObject(byte[]bytes)
{
objectobj=null;
if(bytes==null)
returnobj;
MemoryStreamms=newMemoryStream(bytes);
ms.Position=0;
BinaryFormatterformatter=newBinaryFormatter();
try
{
obj=formatter.Deserialize(ms);
}
catch{obj=null;}
ms.Close();
returnobj;
}
publicstaticboolSave(stringpath,objectvalue,boolisCeranew)
{
//如果不存在创建文件
FileStreamfs;
if((!File.Exists(path))&&isCeranew)
{
try
{
fs=File.Create(path);
}
catch
{
returnfalse;
}
}
//如果存在则打开
else
{
try
{
fs=File.Open(path,FileMode.Open,FileAccess.Write);
}
catch
{
returnfalse;
}
}
//写文件
byte[]buffer=SerializeObject(value);
try
{
for(longi=0;i<buffer.LongLength;i++)
fs.WriteByte(buffer[i]);
}
catch
{
returnfalse;
}
fs.Close();
returntrue;
}
publicstaticobjectRead(stringpath)
{
FileStreamfs;
try
{
fs=File.OpenRead(path);
}
catch
{
returnnull;
}
//读入缓存
StreamReadersreader=newStreamReader(fs);
stringstr=sreader.ReadToEnd();
fs.Close();
sreader.Close();
//分析内容
byte[]buffer=Encoding.Default.GetBytes(str);
returnDeserializeObject(buffer);
}
[Serializable]
publicstructFormSizeandLocation
{
publicintSizeW;
publicintSizeH;
publicintLocationX;
publicintLocationY;
publicintStyle;
}
privatestatic Setting.FormSizeandLocationfsp=newSetting.FormSizeandLocation();
publicstaticvoidAddRenewFormSizeControl(Formform)
{
form.FormClosing+=newFormClosingEventHandler(FormcloseEvent);
form.Load+=newEventHandler(FormloadEvent);
}
privatestaticvoidFormcloseEvent(objectsender,EventArgse)
{
Formform=(Form)sender;
switch(form.WindowState)
{
caseFormWindowState.Maximized:
fsp.Style=2;
fsp.SizeW=form.Width;
fsp.SizeH=form.Height;
fsp.LocationX=form.Location.X;
fsp.LocationY=form.Location.Y;
break;
caseFormWindowState.Minimized:
fsp.Style=1;
break;
caseFormWindowState.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);
}
privatestaticvoidFormloadEvent(objectsender,EventArgse)
{
Formform=(Form)sender;
objectresult=Setting.Read(Directory.GetCurrentDirectory()+@"\"+"Location.set");
if(result!=null)
{
fsp=(Setting.FormSizeandLocation)result;
switch(fsp.Style)
{
case2:
form.WindowState=FormWindowState.Maximized;
break;
default:
form.WindowState=FormWindowState.Normal;
break;
}
form.Left=fsp.LocationX;
form.Top=fsp.LocationY;
form.Size=newSize(fsp.SizeW,fsp.SizeH);
}
}
}
}
基本功能就是保存一个结构体类型的数据
boolSave(filePath,value,true);
还有读取被保存数据的文件,从中读取,这个结构体被装箱,要做的只是拆箱
objectresult=Save(filePath,将要保存的数据实例,true)
if(result!=null)//确认文件存在且读取成功
将这两个功能结合,能不能把窗口的位置和大小记录下来呢,当然可以,首先要做的事声明一个结构体,用来保存大小和位置还有状态
[Serializable]
publicstructFormSizeandLocation
{
publicintSizeW;
publicintSizeH;
publicintLocationX;
publicintLocationY;
publicintStyle;
}
然后进行保存和设置,代码108-172行都是对于它的处理,Howdoesitwork?
让用户给出一个窗口实例
订阅实例的Load和Closing事件
在load事件中把保存的文件读取,并更改实例的位置和大小
在closing事件中把大小和位置保存
AddRenewFormSizeControl(this);
//只需一句代码,一定要写在InitializeComponent函数后。不能写在load事件里
注意,保存的文件是工作路径+Location.set你也可以自己改写此类。
相关文章
- C#操作INI文件的案例分享
- c#常用正则表达式,验证邮箱,Url,电话号码等详解编程语言
- 通过C#来加载X509格式证书文件并生成RSA对象详解编程语言
- C#读取文件为byte数组详解编程语言
- C#正则判断一个数字的格式是否有逗号的代码
- c#递归访问文件夹(删掉歌词文件)
- C#SetWindowPos窗口置顶使用说明
- ASP.NET(C#)读取Excel的文件内容
- C#中执行批处理文件(*.bat)的方法代码
- c#读取xml文件到datagridview实例
- C#中XmlTextWriter读写xml文件详细介绍
- C#对XML文件的各种操作实现方法
- asp.netC#实现下载文件的六种方法实例
- c#文件的复制,移动,创建(实例代码)
- C#基础延迟加载介绍与实例
- C#操作目录与文件的方法步骤
- C#操作config文件的具体方法
- 在C#中创建和读取XML文件的实现方法
- C#操作txt文件,进行清空添加操作的小例子
- C#读写txt文件多种方法实例代码
- C#中按指定质量保存图片的实例代码
- C#实现word文件下载的代码
- C#winfrom模拟ftp文件管理实现代码
- C#Ini文件操作实例
- c#读写excel文件使用示例
- c#使用file.copy实现文件备份示例
- C#中csv文件与DataTable互相导入处理实例解析
- C#GDI在控件上绘图的方法