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 WindowsFormsApp1
{
    //参考 https://www.bilibili.com/video/BV1xt4y1A7xb/?spm_id_from=333.788.top_right_bar_window_history.content.click&vd_source=e821a225c7ba4a7b85e5aa6d013ac92e
    public partial class Form1 : Form
    {
        private int originalWidth;
        private int originalHeight;
        public Form1()
        {
            InitializeComponent();
            originalHeight = this.Height;
            originalWidth = this.Width;
            originalTag(this);
        }
        /// <summary>
        /// 保存各个控件大小
        /// </summary>
        /// <param name="ctr"></param>
        private void originalTag(Control ctr)
        {
            foreach (Control ct in ctr.Controls)
            {
                ct.Tag =
                ct.Left.ToString() + ":" +
                ct.Top.ToString() + ":" +
                ct.Width.ToString() + ":" +
                ct.Height.ToString() + ":" +
                ct.Font.Size.ToString();
                if( ct.Controls.Count > 0)
                {
                    originalTag(ct);
                }
                Console.WriteLine(ct.Name + " -> " + ct.Tag);
            }
            

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            float newWidth = this.Width / (float)originalWidth;
            float newHeight = this.Height / (float)originalHeight;
            reSetContralSize(newWidth, newHeight, this);
        }
        /// <summary>
        /// 重绘各个控件大小
        /// </summary>
        /// <param name="newWidth"></param>
        /// <param name="newHeight"></param>
        /// <param name="ctr"></param>
        private void reSetContralSize( float newWidth, float newHeight, Control ctr)
        {
            foreach (Control ct in ctr.Controls)
            {
                if( ct.Tag != null) 
                {
                    string[] tagtmp= ct.Tag.ToString().Split(':');
                    ct.Left = Convert.ToInt32(Convert.ToSingle((string)tagtmp[0]) * newWidth);
                    ct.Top = Convert.ToInt32(Convert.ToSingle((string)tagtmp[1]) * newHeight);
                    ct.Width = Convert.ToInt32(Convert.ToSingle((string)tagtmp[2]) * newWidth);
                    ct.Height = Convert.ToInt32(Convert.ToSingle((string)tagtmp[3]) * newHeight);
                    ct.Font = new Font(ct.Font.Name,Convert.ToInt32(Convert.ToSingle((string)tagtmp[4]) * newHeight));
                    if( ct.Controls.Count > 0)
                    {
                        reSetContralSize(newWidth, newHeight, ct);
                    }
                }
            }

        }

    }
}

此文参考了博主的视频讲解,链接在代码内保留,不懂得可以去查看视频讲解

特此记录

anlog

2022年9月21日

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值