using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class FormResize
{
public List<controlRect> oldCtrls = new List<controlRect>();
public float OldFormWidth;
public float OldFormHight;
public float wScale;//宽比
public float hScale; //高比
public struct controlRect
{
public int Left;
public int Top;
public int Width;
public int Height;
public float FontSize;
public string ConName;
}
public void SetContorlRect(Control form)
{
if (form is Form)
{
OldFormWidth = form.Width;
OldFormHight = form.Height;
}
controlRect OldSet = new controlRect();
foreach (Control cR in form.Controls)
{
OldSet.Left = cR.Left;
OldSet.Top = cR.Top;
OldSet.Width = cR.Width;
OldSet.Height = cR.Height;
OldSet.FontSize = cR.Font.Size;
OldSet.ConName = cR.Name;
if (cR.Controls.Count > 0)
{
SetContorlRect(cR);
}
oldCtrls.Add(OldSet);
}
// MessageBox.Show("fafa");
}
public void SetResizeFloat(Form mForm)
{
wScale = (float)mForm.Width / OldFormWidth;//新旧窗体之间的比例,与最早的旧窗体
hScale = (float)mForm.Height / OldFormHight;//.Height;
}
public void SetResize(Control form)
{
foreach (Control oneControl in form.Controls)
{
controlRect OneRect = new controlRect();
OneRect = oldCtrls.Where(x => x.ConName == oneControl.Name).FirstOrDefault();
if (OneRect.Height != 0)
{
if (oneControl.Controls.Count > 0)
{
SetResize(oneControl);//窗体内其余控件还可能嵌套控件(比如panel),要单独抽出,因为要递归调用
oneControl.Left = (int)(OneRect.Left * wScale);//新旧控件之间的线性比例。控件位置只相对于窗体,所以不能加 + wLeft1
oneControl.Top = (int)(OneRect.Top * hScale);//
oneControl.Width = (int)(OneRect.Width * wScale);//只与最初的大小相关,所以不能与现在的宽度相乘 (int)(c.Width * w);
oneControl.Height = (int)(OneRect.Height * hScale);//
oldCtrls.Remove(OneRect);
}
else
{
Single currentSize = Convert.ToSingle(OneRect.FontSize * wScale);
oneControl.Left = (int)(OneRect.Left * wScale);//新旧控件之间的线性比例。控件位置只相对于窗体,所以不能加 + wLeft1
oneControl.Top = (int)(OneRect.Top * hScale);//
oneControl.Width = (int)(OneRect.Width * wScale);//只与最初的大小相关,所以不能与现在的宽度相乘 (int)(c.Width * w);
oneControl.Height = (int)(OneRect.Height * hScale);//
oneControl.Font = new Font(oneControl.Font.Name, currentSize, oneControl.Font.Style, oneControl.Font.Unit);
oldCtrls.Remove(OneRect);
}
}
}
}
/// <summary>
/// 窗口改变size后的变动
/// </summary>
public void SetResizeInput(Form Form)
{
SetResizeFloat(Form);
SetResize(Form);
SetContorlRect(Form);
}
}
}
//使用方法
public partial class Form1 : Form
{
//AutoSizeFormClass asc = new AutoSizeFormClass();
FormResize asc = new FormResize();
public Form1()
{
InitializeComponent();
asc.SetContorlRect(this);//加入最初的大小
this.Resize += new EventHandler(MyForm_Resize);
}
private void MyForm_Resize(object sender, EventArgs e)
{
if (this.IsHandleCreated)
{
this.Invoke(new EventHandler(delegate
{
asc.SetResizeInput(this);//运行主程序
}));
}
//MessageBox.Show(label1.Font.Size.ToString());
//this.Text = this.Width.ToString() + " " + this.Height.ToString(); //窗体标题显示长度和宽度
}
}
版权声明:本文为weixin_40029679原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。