欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#保存窗口位置大小操作类(序列化和文件读写功能)

程序员文章站 2024-02-16 22:05:46
记录窗口上次关闭的位置和大小复制代码 代码如下:namespace pdsafe.base{    public class setting&...

记录窗口上次关闭的位置和大小

复制代码 代码如下:

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 你也可以自己改写此类。