方法一:只能实现控件位置自动适应。
方法二:能实现控件位置与控件字体的大小自动适应 。
方法一代码:
添加一个适应类:
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowForm
{
public class AutoSizeFormClass
{
//(1).声明结构,只记录窗体和其控件的初始位置和大小。
public struct ControlRect
{
public int Left;
public int Top;
public int Width;
public int Height;
}
//(2).声明 1个对象
//注意这里不能使用控件列表记录 List<Control> nCtrl;,因为控件的关联性,记录的始终是当前的大小。
public List<ControlRect> oldCtrl;
//int ctrl_first = 0;
//(3). 创建两个函数
//(3.1)记录窗体和其控件的初始位置和大小,
/// <summary>
/// 记录窗体和其控件的初始位置和大小
/// </summary>
/// <param name="mForm"></param>
public void ControllInitializeSize(Form mForm)
{
// if (ctrl_first == 0)
{
// ctrl_first = 1;
oldCtrl = new List<ControlRect>();
ControlRect cR;
cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height;
oldCtrl.Add(cR);
foreach (Control c in mForm.Controls)
{
ControlRect objCtrl;
objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height;
oldCtrl.Add(objCtrl);
}
}
// this.WindowState =