如下,测试窗口布局比较简单,不多说,仅供参考:
![](https://i-blog.csdnimg.cn/blog_migrate/249254f4b524e84b58a437aa389a1ab7.png)
后台代码如下:
例子实现当窗口最大化、最小化或者拖动边缘时候,窗口中的控件随着窗体的改变而相应改变比例大小!共勉!!
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 UIChange
{
public partial class Form1 : Form
{
private float X = 0.0f,Y = 0.0f;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(Form1_Resize);
X = this.Width;
Y = this.Height;
this.setTag(this);
}
private void setTag(Control cons) {
foreach (Control con in cons.Controls) {
con.Tag = con.Width + ":" + con.Height + ":" + con.Top + ":" + con.Left + ":" + con.Font.Size;
if (con.Controls.Count > 0) {
setTag(con);
}
}
}
private void setControls(float newx, float newy, Control cons) {
foreach (Control con in cons.Controls) {
String[] mytags = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytags[0]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytags[1]) * newy;
con.Height = (int)a;
a = Convert.ToSingle(mytags[2])*newy;
con.Top = (int)a;
a = Convert.ToSingle(mytags[3])*newx;
con.Left = (int)a;
Single currectsize = Convert.ToSingle(mytags[4]) * newy;
con.Font = new Font(con.Font.Name, currectsize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0) {
setControls(newx,newy,con);
}
}
}
private void Form1_Resize(object sender, EventArgs e)
{
float newx = (this.Width) / X;
float newy = (this.Height) / Y;
setControls(newx,newy,this);
this.Text = this.Width.ToString() + "*" + this.Height.ToString();
}
}
}
例子实现当窗口最大化、最小化或者拖动边缘时候,窗口中的控件随着窗体的改变而相应改变比例大小!共勉!!