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