C#——实现窗口与控件的比例缩放

休息时,学习一下C#

  • 示例代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormGeneral
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        ControlSizeDivide ctrMainForm;
        ControlSizeDivide ctrGroupBox1_;
        ControlSizeAndLocation ctrButton1;
        ControlSizeAndLocation ctrGroupBox1;
        ControlSizeAndLocation ctrGroupLabel1; 
        private void FormMain_Load(object sender, EventArgs e)
        {
            // this.Location = new Point(0,0);
            this.MinimumSize = new Size(300,210);
            ctrMainForm = new ControlSizeDivide(this, 100, 100, 1000, 700);
            ctrButton1 = new ControlSizeAndLocation(button1, ctrMainForm, 90, 90, 10, 8);
            ctrGroupBox1 = new ControlSizeAndLocation(groupBox1, ctrMainForm, 10, 10, 50, 50);
            ctrGroupBox1_ = new ControlSizeDivide(this,100,100);
            ctrGroupLabel1 = new ControlSizeAndLocation(label1, ctrGroupBox1_,20, 20,20,20);
        }

        private void FormMain_SizeChanged(object sender, EventArgs e)
        {
            ctrGroupBox1.updateControlSizeAndLocation();
            ctrGroupLabel1.updateControlSizeAndLocation();
            ctrButton1.updateControlSizeAndLocation();
        }
    }
}

 

  • 功能代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace FormGeneral
{
    public struct ControlSizeAndLocation
    {
        public int topTableRow;
        public int leftTableCol;
        public int numOccupyTableRows;
        public int numOccupyTableCols;
        public Control ctr;
        public ControlSizeDivide ctrBase;

        public ControlSizeAndLocation(Control ctr, ControlSizeDivide ctrBase, int topTableRow, int leftTableCol, int numOccupyTableRows, int numOccupyTableCols)
        {
            this.topTableRow = topTableRow;
            this.leftTableCol = leftTableCol;
            this.numOccupyTableCols = numOccupyTableCols;
            this.numOccupyTableRows = numOccupyTableRows;
            this.ctr = ctr;
            this.ctrBase = ctrBase;

            double heightTableRows = 1.0 * (ctrBase.ctr.Height- ctrBase.nonHeight) / ctrBase.numTableRows;
            double widthTableCols = 1.0 * (ctrBase.ctr.Width-ctrBase.nonWidth) / ctrBase.numTableCols;
            if (this.ctrBase.ctr.Name == "FormMain")
            {
                this.ctr.Location = new Point(0, 0) + new Size((int)(leftTableCol * widthTableCols), (int)(topTableRow * heightTableRows));
            }
            else
            {
                this.ctr.Location = this.ctrBase.ctr.Location + new Size((int)(leftTableCol * widthTableCols), (int)(topTableRow * heightTableRows));
            }

            this.ctr.Size = new Size((int)(numOccupyTableCols * widthTableCols), (int)(numOccupyTableRows * heightTableRows));

        }

        public void updateControlSizeAndLocation()
        {
            double heightTableRows = 1.0 * (ctrBase.ctr.Height - ctrBase.nonHeight) / ctrBase.numTableRows;
            double widthTableCols = 1.0 * (ctrBase.ctr.Width - ctrBase.nonWidth) / ctrBase.numTableCols;

            if (this.ctrBase.ctr.Name == "FormMain")
            {
                this.ctr.Location = new Point(0,0)+ new Size((int)(leftTableCol * widthTableCols), (int)(topTableRow * heightTableRows));
            }
            else
            {
                this.ctr.Location = this.ctrBase.ctr.Location + new Size((int)(leftTableCol * widthTableCols), (int)(topTableRow * heightTableRows));
            }
                       
            this.ctr.Size = new Size((int)(numOccupyTableCols * widthTableCols), (int)(numOccupyTableRows * heightTableRows));
        }
    }



    public struct ControlSizeDivide
    {
        public int nonWidth; 
        public int nonHeight;
        
        public int numTableRows;
        public int numTableCols;
        public Control ctr;

        // 手动设定控件属性大小,仅适用于主窗体
        public ControlSizeDivide(Control ctr, int numTableRows, int numTableCols, int maxWidth, int maxHeight)
        {
            this.nonHeight = 38;
            this.nonWidth = 19;
            this.numTableRows = numTableRows;
            this.numTableCols = numTableCols;
            this.ctr = ctr;
            this.ctr.MaximumSize= new Size(maxWidth, maxHeight);
            this.ctr.Size = new Size(maxWidth, maxHeight);
        }

        // 根据控制件属性大小来设置,仅适用于主窗体
        public ControlSizeDivide(Control ctr, int numTableRows, int numTableCols)
        {
            this.nonHeight = 38;
            this.nonWidth = 19;
            this.numTableRows = numTableRows;
            this.numTableCols = numTableCols;
            this.ctr = ctr;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值