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 你也可以自己改写此类。
猜你喜欢
- MyBatis 通过包含的jdbcType类型BIT FLOAT CHAR &nbs
- mybatis Integer类型参数动态sql注意条件例如以下拼接的动态sql<if test="work_status
- 前言:Guarded Suspension意为保护暂停,其核心思想是仅当服务进程准备好时,才提供服务。设想一种场景,服务器可能会在很短时间内
- 本文实例为大家分享了java使用Cookie判断用户登录情况的方法,供大家参考,具体内容如下1.判断是否登录public boolean i
- 在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来
- java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static voi
- 本文实例讲述了java编程调用存储过程中得到新增记录id号的实现方法。分享给大家供大家参考,具体如下:关于ms sql server2000
- 亲爱的读者,在这篇文章中,我提供了一些c#编程的最佳实践。你是否在用户输入验证中使用异常处理机制?如果是,那么你就是那个把你的项目执行速度降
- java连接zookeeper实现zookeeperJava服务端连接Zookeeper,进行节点信息的获取,管理…整理成一个基本工具添加依
- 1 前言在 Springboot 中,异步任务和定时任务是经常遇到的处理问题方式,为了能够用好这两项配置,不干扰正常的业务,需要对其进行异步
- 业务场景近年来B2C、O2O等商业概念的提出和移动端的发展,使得分布式系统流行了起来。分布式系统相对于单系统,解决了流量大、系统高可用和高容
- 定义一个对象应该对其他对象保持最少的了解。问题由来类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大。解决方案尽
- 本章,会对synchronized关键字进行介绍。涉及到的内容包括:1. synchronized原理2. synchronized基本规则
- Service是什么 Service是一个android 系统中的应用程序组件,它跟Activity的级别差不多,但是他没有图形化界面,不能
- Singleton是众多设计模式中最容易理解的一种,也是众多设计模式中较为重要的一种设计模式。接下来我们看看具体介绍。Singleton模式
- 意图:想将项目用到的两个dll库文件(CryptEnDe.dll和ICSharpCode.SharpZipLib.dll)一同编译进exe中
- 汽车前后轮倒车轨迹计算附C#源码(Unity),供大家参考,具体内容如下原理很简单, 都是高中的几何数学部分需要的参数有:车前后轴距;车宽(
- 如下所示:import java.util.ArrayList;import com.heima.bean.Person;public cl
- Java 1.0 IO系统介绍1 Java IO版本Java库的IO分为输入/输出两部分。早期的Java 1.0版本的输入系统是InputS
- 支付宝今年推出了新的转账接口alipay.fund.trans.uni.transfer(升级后安全性更高,功能更加强大) ,老转账接口al