700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C#如何实现窗体控件大小随窗体大小变化(包括字体)

C#如何实现窗体控件大小随窗体大小变化(包括字体)

时间:2022-11-18 10:10:11

相关推荐

C#如何实现窗体控件大小随窗体大小变化(包括字体)

如图,拖动窗体即可改变控件大小(包括字体)

窗体尺寸:345*315

窗体尺寸:603*509

项目资源如下

源码如下

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 控件随窗体自动调整{public partial class Form1 : Form{public Form1(){InitializeComponent();}//在Form_Load中添加代码private void Form1_Load(object sender, EventArgs e){this.Resize += new EventHandler(Form1_Resize);X = this.Width;Y = this.Height;setTag(this);Form1_Resize(new object(), new EventArgs());}//***控制控件大小及文字大小开始***//private float X;private float Y;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);}}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);}}}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();}//***控制控件大小及文字大小结束***//}}

原文地址:/index.php/post/32.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。