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

这篇文章主要介绍了c#保存窗口位置大小操作类,其实就是把序列化和文件读写合到一块,大家参考使用

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

复制代码 代码如下:

namespace PDSafe.Base
{
    public class Setting
    {
        ///
        /// 把对象序列化为字节数组
        ///

        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;
        }

        ///


        /// 把字节数组反序列化成对象
        ///

        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                     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);

            }
        }
    }
}

以上就是c#保存窗口位置大小操作类(序列化和文件读写功能)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程