C#窗體最大化最小化等比例縮放


不廢話,直接代碼

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/// <summary>
/// 20200620 likewei for the windows maxsize
/// </summary>
namespace WindowsFormsApp1
{
    public class WindowZoom
    {

        private float x;//當前窗體的寬度
        private float y;//當前窗體的高度

        public void SetForm(Control form)
        {
            x = form.Width;           
            y = form.Height;
            SetTag(form);
        }

        public void SetReSize(Control form)
        {
            float reSizeX = (form.Width) / x;
            float reSizeY = (form.Height) / y;

            SetControls(reSizeX, reSizeY, form);
        }

        private void SetTag(Control controls)
        {
            foreach (Control ctr in controls.Controls)
            {
                ctr.Tag = ctr.Width + ";" + ctr.Height + ";" + ctr.Left + ";" + ctr.Top + ";" + ctr.Font.Size;
                if (ctr.Controls.Count > 0)
                {
                    SetTag(ctr);
                }
            }
        }

        private void SetControls(float reSizeX, float reSizeY, Control controls)
        {
            //遍歷窗體中的控件,重新設置控件的值
            foreach (Control ctr in controls.Controls)
            {
                //獲取控件的Tag屬性值,並分割后存儲字符串數組
                if (ctr.Tag != null)
                {
                    string[] mytag = ctr.Tag.ToString().Split(new char[] { ';' });
                    //根據窗體縮放的比例確定控件的值
                    ctr.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * reSizeX);//寬度
                    ctr.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * reSizeY);//高度
                    ctr.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * reSizeX);//左邊距
                    ctr.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * reSizeY);//頂邊距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * reSizeY;//字體大小
                    ctr.Font = new Font(ctr.Font.Name, currentSize, ctr.Font.Style, ctr.Font.Unit);
                    if (ctr.Controls.Count > 0)
                    {
                        SetControls(reSizeX, reSizeY, ctr);
                    }
                }
            }
        }


    }
}

 

使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {      

        WindowZoom windowZoom = new WindowZoom();
        public Form1()
        {
            InitializeComponent();         
            windowZoom.SetForm(this);
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Resize(object sender, EventArgs e)
        {           

            windowZoom.SetReSize(this);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM