c#保存窗口位置大小操作类(序列化和文件读写功能)
时间:2023-07-15 18:51:06
记录窗口上次关闭的位置和大小
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 你也可以自己改写此类。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java正则验证正整数的方法分析【测试可用】
![](https://img.aspxhome.com/file/2023/9/66229_0s.jpg)
C# 获取当前总毫秒数的实例讲解
Quartz与Spring集成的两种方法示例
Android中使用Handler及Countdowntimer实现包含倒计时的闪屏页面
![](https://img.aspxhome.com/file/2023/8/125398_0s.gif)
springboot结合websocket聊天室实现私聊+群聊
![](https://img.aspxhome.com/file/2023/9/67609_0s.jpg)
详解如何让Spring MVC显示自定义的404 Not Found页面
![](https://img.aspxhome.com/file/2023/1/72571_0s.jpg)
Java中获取泛型类型信息的方法
springboot项目如何设置session的过期时间
![](https://img.aspxhome.com/file/2023/9/102649_0s.png)
Linux中Java开发常用软件安装方法总结
![](https://img.aspxhome.com/file/2023/2/85912_0s.png)
java实现登录验证码功能
![](https://img.aspxhome.com/file/2023/3/70953_0s.jpg)
C#中==(双等于号)与equals()区别详解
![](https://img.aspxhome.com/file/2023/0/70440_0s.png)
Android之网络通信案例分析
![](https://img.aspxhome.com/file/2023/2/108232_0s.jpg)
java基础入门之IO流
![](https://img.aspxhome.com/file/2023/7/128927_0s.jpg)
Java中String类的常用方法总结
java实现PPT转化为PDF
Java设计模式中代理模式应用详解
详解SpringBoot中Session超时原理说明
在springboot中对kafka进行读写的示例代码
![](https://img.aspxhome.com/file/2023/7/60457_0s.png)
SpringBoot项目中的多数据源支持的方法
Mybatis分解式查询使用方法
![](https://img.aspxhome.com/file/2023/5/89645_0s.png)