实现窗口与控件的等比例缩放 C#源码

//***********************************************

// 实现窗口与控件的等比例缩放 C#源码

//原文来自https://blog.csdn.net/qq_21090131/article/details/83715177

//示例代码:

//***********************************************

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;        

}    

}

}


评论

© Yue's Blog | Powered by LOFTER