using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CvMatch.Game2D
{
public partial class frmAutoSize : Form
{
public frmAutoSize()
{
InitializeComponent();
}
Dictionary<string, Rectangle> dic = new Dictionary<string, Rectangle>();//原始控件大小
float x = 0;//原始大小
float y = 0;
private void frmAutoSize_Load(object sender, EventArgs e)
{
x = this.Size.Width;
y = this.Size.Height;
AddDic(this);
}
/// <summary>
/// /添加数据
/// </summary>
/// <param name="con"></param>
void AddDic(Control con)
{
foreach (Control item in con.Controls )
{
if (item.Controls.Count !=0)
{
AddDic(item );
}
if (dic.Keys .Contains (item.Name ))
{
continue;
}
dic.Add(item.Name ,new Rectangle (item.Location .X ,item.Location.Y,item.Width,item.Height ));
}
}
/// <summary>
/// 窗体变化大小
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmAutoSize_SizeChanged(object sender, EventArgs e)
{
float x1 = this.Size.Width / x;
float y1 = this.Size.Height / y;
SetSize(this, x1, y1);
}
/// <summary>
/// /设置全部的控件大小位置字体大小
/// </summary>
/// <param name="con"></param>
/// <param name="x1"></param>
/// <param name="y1"></param>
void SetSize(Control con, float x1, float y1)
{
foreach (Control item in con.Controls)
{
if (item.Controls.Count != 0)
{
SetSize(item, x1, y1);
}
if (dic.Keys.Contains(item.Name) == false)
{
continue;
}
Rectangle rec = dic[item.Name];
item.Location = new Point((int)(rec.X * x1), (int)(rec.Y * y1));
item.Size = new Size((int)(rec.Width * x1), (int)(rec.Height * y1));
//item.Font.Size = (int)(9 * x1);
float xy = 9 * x1;//使用最小的字体变化,默认字体9号
if (x>y)
{
xy = 9 * y1;
}
item.Font = new System.Drawing.Font("宋体", xy, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
//item.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}
}
}
}