C# 窗体程序拉伸控件自适应
一、控件拉伸自适应
1.新建一个C#窗体程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_Form
{
public partial class Form1 : Form
{
//定义X,y用于记录窗体初始大小
private float X;
private float Y;
public Form1()
{
InitializeComponent();
}
FormSetSelfAuto fa = new FormSetSelfAuto();
private void Form1_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(Form1_Resize);
fa._x = this.Width;
fa._y = this.Height;
fa.setTag(this);
}
//重绘窗体
private void Form1_Resize(object sender, EventArgs e)
{
fa.form_Resize(this);
}
}
}
2.添加一个类
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_Form
{
class FormSetSelfAuto
{
private float X;
private float Y;
public float _x
{
set { X = value; }
}
public float _y
{
set { Y = value; }
}
//获取控件的width,height,left,top,字体的大小值,存放在控件的tag属性中
public 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);
}
}
}
//根据窗体大小调整控件大小
public void setControls(float newx, float newy, Control cons)
{
//遍历窗体中的控件,重新设置控件的值
foreach (Control con in cons.Controls)
{
//获取控件tag属性值,并分割后存储字符串数组
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]) * newy;
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
public void form_Resize(Form fr)
{
float newx = (fr.Width) / X;
float newy = (fr.Height) / Y;
setControls(newx, newy, fr);
fr.Text = fr.Width.ToString() + " " + fr.Height.ToString();
}
}
}
此时,程序运行后拖动边框,内部控件随之拉伸。
二.动态加载控件
1.测试类
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace DynamicControl
{
public partial class Form1 : Form
{
//缓存已添加的控件
Dictionary<String, Control> dictionary = new Dictionary<String, Control>();
Random random = new Random();
public Form1()
{
InitializeComponent();
}
private void AddTest_Click(object sender, EventArgs e)
{
String id = "C_" + random.Next().ToString();
CheckBox box = new CheckBox();
box.Text = id;
box.Size = new Size(168, 35);
box.Font = new Font("Microsoft YaHei UI", 8F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
box.ForeColor = Color.Black;
box.Click += new EventHandler(box_Click);
addNewControl(this.panel2, id, 2, 1, box);
}
void box_Click(object sender, EventArgs e)
{
MessageBox.Show("123");
}
public void addNewControl(Panel panel, String id, int stepX, int stepY, Control obj)
{
int count = dictionary.Count;
//取父空间宽高
int pw = panel.Width;
int ph = panel.Height;
//计算下一个控件位置
int sw = obj.Width;
int sh = obj.Height;
int countX = pw / (sw + stepX);
int countY = ph / (sh + stepY);
//判断是否能容纳更多
if (count >= countX * countY)
{
return;
}
//计算位置
int py = (count / countX) * sh + stepY;
int px = (count % countX) * sw + stepX;
obj.Location = new Point(px, py);
//添加控件
panel.Controls.Add(obj);
//添加缓存
if (dictionary.ContainsKey(id))
{
dictionary.Remove(id);
}
dictionary.Add(id, obj);
}
//[DllImport("user32", EntryPoint = "HideCaret")]
//private static extern bool HideCaret(IntPtr hWnd);
//void txt_MouseDown(object sender, MouseEventArgs e)
//{
// HideCaret(((CheckBox)sender).Handle);
//}
//void txt_MouseEnter(object sender, EventArgs e)
//{
// HideCaret(((CheckBox)sender).Handle);
//}
}
}
控件布局
测试效果
事件测试