记住应用程序的位置和大小

思路:当窗体关闭时,记录前的位置并存储到配置文件中(可以是文本文件,也可以是xml 文件),下次这个程序再启动时,如果配置存在,则读取中配置文件中窗体位的值 并赋给窗体,如果配置文件不存在,窗体则显示在居中。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RestoreFormPositionandSize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //GeometryFromString(Properties.Settings.Default.WindowGeometry, this);

            if (System.IO.File.Exists("config.config"))
            {
                string s = System.IO.File.ReadAllText(@"config.config");
                GeometryFromString(s, this);  
            }
            else
            {
                this.StartPosition = FormStartPosition.CenterScreen;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public static void GeometryFromString(string thisWindowGeometry, Form formIn)
        {
            if (string.IsNullOrEmpty(thisWindowGeometry) == true)
            {
                return;
            }

            string[] numbers = thisWindowGeometry.Replace("\r\n","").Split('|');
            string windowString = numbers[4];
            if (windowString == "Normal")
            {
                Point windowPoint = new Point(int.Parse(numbers[0]),
                    int.Parse(numbers[1]));
                Size windowSize = new Size(int.Parse(numbers[2]),
                    int.Parse(numbers[3]));

                bool locOkay = GeometryIsBizarreLocation(windowPoint, windowSize);
                bool sizeOkay = GeometryIsBizarreSize(windowSize);

                if (locOkay == true && sizeOkay == true)
                {
                    formIn.Location = windowPoint;
                    formIn.Size = windowSize;
                    formIn.StartPosition = FormStartPosition.Manual;
                    formIn.WindowState = FormWindowState.Normal;
                }
                else if (sizeOkay == true)
                {
                    formIn.Size = windowSize;
                }
            }
            else if (windowString == "Maximized")
            {
                formIn.Location = new Point(100, 100);
                formIn.StartPosition = FormStartPosition.Manual;
                formIn.WindowState = FormWindowState.Maximized;
            }
        }


        private static bool GeometryIsBizarreLocation(Point loc, Size size)
        {
            bool locOkay;
            if (loc.X < 0 || loc.Y < 0)
            {
                locOkay = false;
            }
            else if (loc.X + size.Width > Screen.PrimaryScreen.WorkingArea.Width)
            {
                locOkay = false;
            }
            else if (loc.Y + size.Height > Screen.PrimaryScreen.WorkingArea.Height)
            {
                locOkay = false;
            }
            else
            {
                locOkay = true;
            }
            return locOkay;
        }

        private static bool GeometryIsBizarreSize(Size size)
        {
            return (size.Height <= Screen.PrimaryScreen.WorkingArea.Height &&
                size.Width <= Screen.PrimaryScreen.WorkingArea.Width);
        }

        public static void  GeometryToString(Form mainForm)
        {
            string str = mainForm.Location.X.ToString() + "|" +
                            mainForm.Location.Y.ToString() + "|" +
                            mainForm.Size.Width.ToString() + "|" +
                            mainForm.Size.Height.ToString() + "|" +
                            mainForm.WindowState.ToString();

                using (StreamWriter sw = new StreamWriter("config.config", false, Encoding.Default))
                {
                    sw.WriteLine(str);
                }


        }
        //public static string GeometryToString(Form mainForm)
        //{
        //    return mainForm.Location.X.ToString() + "|" +
        //        mainForm.Location.Y.ToString() + "|" +
        //        mainForm.Size.Width.ToString() + "|" +
        //        mainForm.Size.Height.ToString() + "|" +
        //        mainForm.WindowState.ToString();


        //}
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // persist our geometry string.
            //Properties.Settings.Default.WindowGeometry = GeometryToString(this);
            //Properties.Settings.Default.Save();
            GeometryToString(this);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值