1、在设计文件里,Form1.Designer.cs如下代码。 2、在窗体文件里,Form1.cs如下。
using System.Drawing;
using System.Windows.Forms;
using System;
//
//
//
#region 窗口放大,对应控件放大
private float MainFormOldWidth;
private float MainFormOldHeight;
private void setTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
setTag(con);
}
}
/// <summary>
/// 设置对应控件的大小
/// </summary>
/// <param name="newx">窗体长</param>
/// <param name="newy">窗体宽</param>
/// <param name="cons">控件</param>
private void setControls(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
/// <summary>
/// 窗体大小改变,获取窗体的长宽
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Resize(object sender, EventArgs e)
{
float newx = (this.Width) / MainFormOldWidth;
float newy = (this.Height) / MainFormOldHeight;
setControls(newx, newy, this);
}
#endregion
//
//
//
Form1.cs 文件代码如下:
public Form1()
{
InitializeComponent();
this.Resize += new EventHandler(MainForm_Resize); //随着窗口大小变化,窗口里的控件自动缩放。
MainFormOldWidth = this.Width;
MainFormOldHeight = this.Height;
setTag(this);//存储控件的信息
}
508

被折叠的 条评论
为什么被折叠?



