一个 结构体保存和窗口位置大小记录的类
2011-11-04 22:39
357 查看
考完试了,阶段性的休息一下,最近在写一个数据加密软件叫 Privacy Data Safe ,这不是一个简单的小软件,而是一个功能十分强大的软件,目前还处于初步的设计阶段,但是一个成功的软件总是在细节考虑得很周到,例如 :记录窗口上次关闭的位置和大小,等等细节,但是技术镀金还是要有个度的,太追求完美势必适得其反,倒不如把这些东西封装在一起,拿来就用
不说费话了,先看看这段代码,其实就是把 序列化 和 文件读写 合到一块了
基本功能就是保存一个结构体类型的数据
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 你也可以自己改写此类。
想了解的话看看怎么实现的,想用的拿去就用没必要了解工作原理。
没什么技术含量....
转载需注明出处,并保持一致!!
不说费话了,先看看这段代码,其实就是把 序列化 和 文件读写 合到一块了
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 你也可以自己改写此类。
想了解的话看看怎么实现的,想用的拿去就用没必要了解工作原理。
没什么技术含量....
转载需注明出处,并保持一致!!
相关文章推荐
- 记录窗口的位置和大小
- Camshift原理 camshift利用目标的颜色直方图模型将图像转换为颜色概率分布图,初始化一个搜索窗的大小和位置,并根据上一帧得到的结果自适应调整搜索窗口的位置和大小,从而定位出当前图像中目标的
- c#保存窗口位置大小操作类(序列化和文件读写功能)
- 如何设置一个窗口的大小、位置和其他参数
- 记录窗口位置与大小
- QT中QSettings 的用法 保存窗口的位置 大小
- Delphi中创建一个可以改变大小的无边框窗口
- 牛牛和15朋友来玩打土豪分田地的游戏,牛牛决定让你来分田地,地主的田地可以看成是一个矩形,每个位置有一个价值。分割田地的方法是横竖各切三刀,分成26份,作为领导干部,牛牛总是会选择其中总价值最小的一份田地,作为牛牛最好的朋友,你希望牛牛取得的田地价值和尽可能大,你知道这个值最大可以是多少吗? 输入两个整数n和m(1≤n,m≤75)表示田地的大小,接下来n行,每行包括m个0-9之间的数字,表示每块
- 根据窗口大小自动调整位置,保持在窗口中央
- 如何定制弹出窗口的大小及其位置
- 设置Gvim的启动位置与窗口大小,代码折叠及配色等
- js调试记录,将客户的调试信息保存到服务器端的一个小方法。
- 想做一个预览窗口任意大小的camera,有什么好的办法
- 指定窗口显示位置及大小(引发潜在问题) window.open()
- 基于AOP的一个操作记录保存日志
- MFC中设置窗口大小位置
- 如何将当前界面的可视组件以同样的相对位置和大小保存在png图像文件中?要求写出实践的代码。
- 页面大小、窗口大小和滚动条位置
- 产生一个固定大小、禁止最大化、最小化的qquickview窗口
- QQ聊天记录的保存位置