C#WINFORM控件随窗口比例变化

public partial class Index : Form
    {
        //变量
        float WL, HL;
        bool bl = true;

        public Index()
        {
            InitializeComponent();
            //增加窗口变化事件
            this.Resize += new EventHandler(formChange);
            //初始界面大小
            this.Size = new System.Drawing.Size(1300, 700);
            WL = this.Width;
            HL = this.Height;
            //重绘控件
            HL = this.Height - 40;
            WL = this.Width - 18;

            #region 界面控件
            for (int i = 0; i < 4; i++)
            {
                string[] name = { "K", "S", "D", "X" };
                string[] TEXT = { "1", "2", "3", "4" };
                int W = (int)WL;
                int H = (int)HL;
                Button Pbut = new Button();
                Pbut.Name = name[i];
                Pbut.Text = TEXT[i];
                Pbut.Font = new Font("宋体", 15f, FontStyle.Regular);
                Pbut.BackColor = Color.Transparent;
                if (i < 2)
                {
                    Pbut.Location = new System.Drawing.Point(W * i / 2, 0);
                }
                else
                {
                    Pbut.Location = new System.Drawing.Point(W * (i - 2) / 2, H / 2);
                }
                Pbut.Size = new System.Drawing.Size(W / 2, H / 2);
                //鼠标移入
                Pbut.MouseEnter += new EventHandler(PS_IN);
                //鼠标移出
                Pbut.MouseLeave += new EventHandler(PS_OUT);
                //鼠标点击
                Pbut.MouseDown += new MouseEventHandler(PS_Click);
                //鼠标弹起

                this.Controls.Add(Pbut);
                #endregion

            }
            //调用窗口控件方法
            conChange(this);
        }
        #region 获取控件信息方法
        public void conChange(Control cons)
        {
            foreach (Control c in cons.Controls)
            {
                c.Tag = c.Width + ";" + c.Height + ";" + c.Left + ";" + c.Top + ";" + c.Font.Size;
                if (c.Controls.Count > 0)
                {
                    conChange(c);
                }
            }
        }
        #endregion

        #region 重构控件大小方法
        public void newCon(float newW, float newH, Control cons)
        {
            foreach (Control c in cons.Controls)
            {
                if (c.Tag != null)
                {
                    string[] newTag = c.Tag.ToString().Split(new char[] { ';' });
                    c.Width = Convert.ToInt32(System.Convert.ToSingle(newTag[0]) * newW);//宽度
                    c.Height = Convert.ToInt32(System.Convert.ToSingle(newTag[1]) * newH);//高
                    c.Left = Convert.ToInt32(System.Convert.ToSingle(newTag[2]) * newW);//左
                    c.Top = Convert.ToInt32(System.Convert.ToSingle(newTag[3]) * newH);//上
                    Single currentSize = System.Convert.ToSingle(newTag[4]) * newH;//字体大小
                    c.Font = new Font(c.Font.Name, currentSize, c.Font.Style, c.Font.Unit);
                    if (c.Controls.Count > 0)
                    {
                        newCon(newW, newH, cons);
                    }
                }
            }
        }

        #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值