欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#保存窗口位置大小操作類(序列化和文件讀寫功能)

 更新時(shí)間:2013年11月29日 10:07:09   作者:  
這篇文章主要介紹了c#保存窗口位置大小操作類,其實(shí)就是把序列化和文件讀寫合到一塊,大家參考使用

記錄窗口上次關(guān)閉的位置和大小

復(fù)制代碼 代碼如下:

namespace PDSafe.Base
{
    public class Setting
    {
        ///<summary>
        /// 把對(duì)象序列化為字節(jié)數(shù)組
        ///</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>
        /// 把字節(jié)數(shù)組反序列化成對(duì)象
        ///</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)
        {
            //如果不存在創(chuàng)建文件
            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();
            //分析內(nèi)容
            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);

            }
        }
    }
}

基本功能就是保存一個(gè)結(jié)構(gòu)體類型的數(shù)據(jù)
bool Save(filePath,value,true);
還有讀取被保存數(shù)據(jù)的文件,從中讀取,這個(gè)結(jié)構(gòu)體被裝箱,要做的只是拆箱
object result = Save(filePath,將要保存的數(shù)據(jù)實(shí)例,true)
if(result != null)//確認(rèn)文件存在且讀取成功

將這兩個(gè)功能結(jié)合,能不能把窗口的位置和大小記錄下來呢,當(dāng)然可以,首先要做的事聲明一個(gè)結(jié)構(gòu)體,用來保存大小和位置還有狀態(tài)

復(fù)制代碼 代碼如下:

 [Serializable]
        public struct FormSizeandLocation
        {
            public int SizeW;
            public int SizeH;
            public int LocationX;
            public int LocationY;
            public int Style;
        }

然后進(jìn)行保存和設(shè)置,代碼108-172行都是對(duì)于它的處理,How does it work?
讓用戶給出一個(gè)窗口實(shí)例
訂閱實(shí)例的 Load和Closing事件
在load事件中把保存的文件讀取,并更改實(shí)例的位置和大小
在closing事件中把大小和位置保存
AddRenewFormSizeControl(this);
 //只需一句代碼,一定要寫在InitializeComponent函數(shù)后。不能寫在load事件里

注意,保存的文件是 工作路徑+Location.set 你也可以自己改寫此類。

相關(guān)文章

最新評(píng)論