C#自动保存控件大小和位置,C#自动保存窗体大小和位置

本文介绍了一种基于C#的控件自动配置与保存机制,通过读取和写入配置文件来实现控件的位置、大小等属性的自动保存与加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


namespace Share
{
    class AutoConfig
    {
        static string CONFIG_FILEPATH = "AutoConfig.ini";
        Control controler = null;
        string savedFilePath = null ;
        string ID = null ;
        public Rectangle DefaultRect = new Rectangle(100, 100, 100, 100);
        public AutoConfig(Control controler, string ID = null, string savedFilePath = null)
        {
            if (savedFilePath == null || savedFilePath.Length <= 0)
            {
                savedFilePath = CONFIG_FILEPATH;
            }
            this.savedFilePath = savedFilePath;


            if (ID == null || ID.Length <= 0)
            {
                ID = controler.Name;
            }
            this.ID = ID;


            this.controler = controler;
            controler.HandleCreated += new EventHandler(controler_HandleCreated);
            controler.HandleDestroyed += new EventHandler(controler_HandleDestroyed);
        }
        
        void controler_HandleDestroyed(object sender, EventArgs e)
        {
            ConfigFile file = new ConfigFile(CONFIG_FILEPATH);
            file.Write("控件[" + this.ID + "].X",this.controler.Location.X.ToString());
            file.Write("控件[" + this.ID + "].Y", this.controler.Location.Y.ToString());
            file.Write("控件[" + this.ID + "].W", this.controler.Size.Width.ToString());
            file.Write("控件[" + this.ID + "].H", this.controler.Size.Height.ToString());
            file.Close(true);
        }


        void controler_HandleCreated(object sender, EventArgs e)
        {
            //读取配置文件
            ConfigFile file = new ConfigFile(CONFIG_FILEPATH);
            //窗口位置,大小
            
            this.controler.Location =
                new Point(
                    Convert.ToInt32(file.Read("控件[" + this.ID + "].X", this.controler.Location.X.ToString())),
                    Convert.ToInt32(file.Read("控件[" + this.ID + "].Y", this.controler.Location.Y.ToString())));
            this.controler.Size =
                new System.Drawing.Size(
                    Convert.ToInt32(file.Read("控件[" + this.ID + "].W", this.controler.Size.Width.ToString())),
                    Convert.ToInt32(file.Read("控件[" + this.ID + "].H", this.controler.Size.Height.ToString())));


            if (0 - this.controler.Location.X > this.controler.Width || 0 - this.controler.Location.Y > this.controler.Height || this.controler.Size.Height * this.controler.Size.Width <= 0)
            {
                this.controler.Location = new Point(DefaultRect.X, DefaultRect.Y);
                this.controler.Size = new Size(DefaultRect.Width, DefaultRect.Height);
            }
            file.Close(false);
        }
    }
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值