資源簡介
窗體內的控件隨窗體大小變化,控件的大小變化也等比例變化
/// <summary>
/// 將控件的寬,高,左邊距,頂邊距和字體大小暫存到tag屬性中
/// </summary>
/// <param name="cons">遞歸控件中的控件</param>
private void setTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width ":" con.Height ":" con.Left ":" con.Top ":" con.Font.Size;
if (con.Controls.Count > 0)
setTag(con);
}
}
//根據窗體大小調整控件大小
private void setControls(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
/// <summary>
/// 將控件的寬,高,左邊距,頂邊距和字體大小暫存到tag屬性中
/// </summary>
/// <param name="cons">遞歸控件中的控件</param>
private void setTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width ":" con.Height ":" con.Left ":" con.Top ":" con.Font.Size;
if (con.Controls.Count > 0)
setTag(con);
}
}
//根據窗體大小調整控件大小
private void setControls(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
代碼片段和文件信息
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?Fineex.FineexPrint
{
????public?partial?class?Form1?:?Form
????{
????????private?float?X;//當前窗體的寬度
????????private?float?Y;//當前窗體的高度
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????this.Resize?+=?new?EventHandler(Form1_Resize);//窗體調整大小時引發事件
????????????X?=?this.Width;//獲取窗體的寬度
????????????Y?=?this.Height;//獲取窗體的高度
????????????setTag(this);//調用方法
????????}
????????void?Form1_Resize(object?sender?EventArgs?e)
????????{
????????????float?newx?=?(this.Width)?/?X;
????????????float?newy?=?this.Height?/?Y;
????????????setControls(newx?newy?this);
????????????this.Text?=?this.Width.ToString()?+?“?“?+?this.Height.ToString(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????189??2020-07-17?14:07??formSize\App.config
?????文件???????4197??2020-07-29?10:53??formSize\FineexPrint.csproj
?????文件???????2673??2020-07-29?10:36??formSize\Form1.cs
?????文件???????3126??2020-07-29?10:36??formSize\Form1.Designer.cs
?????文件???????5817??2020-07-29?10:36??formSize\Form1.resx
?????文件????????214??2020-07-17?14:07??formSize\obj\Debug\.NETfr
?????文件??????11516??2020-07-29?10:09??formSize\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7211??2020-07-29?10:53??formSize\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????180??2020-07-29?10:53??formSize\obj\Debug\Fineex.FineexPrint.Form1.resources
?????文件????????180??2020-07-29?10:53??formSize\obj\Debug\Fineex.FineexPrint.Properties.Resources.resources
?????文件??????????0??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.csproj.CopyComplete
?????文件?????????42??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.csproj.CoreCompileInputs.cache
?????文件????????970??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.csproj.FileListAbsolute.txt
?????文件???????1072??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.csproj.GenerateResource.cache
?????文件????????424??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.csprojAssemblyReference.cache
?????文件??????22016??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.exe
?????文件???????4440??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.FrmMain.resources
?????文件??????40448??2020-07-29?10:53??formSize\obj\Debug\FineexPrint.pdb
?????文件???????3584??2020-07-17?14:22??formSize\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件????????550??2020-07-29?10:57??formSize\Program.cs
?????文件???????1314??2020-07-17?14:07??formSize\Properties\AssemblyInfo.cs
?????文件???????2871??2020-07-17?14:10??formSize\Properties\Resources.Designer.cs
?????文件???????5612??2020-07-17?14:07??formSize\Properties\Resources.resx
?????文件???????1118??2020-07-17?14:10??formSize\Properties\Settings.Designer.cs
?????文件????????249??2020-07-17?14:07??formSize\Properties\Settings.settings
?????目錄??????????0??2020-07-29?10:57??formSize\obj\Debug\TempPE
?????目錄??????????0??2020-07-17?14:07??formSize\bin\Debug
?????目錄??????????0??2020-07-17?14:09??formSize\bin\Release
?????目錄??????????0??2020-07-29?10:57??formSize\obj\Debug
?????目錄??????????0??2020-07-29?10:57??formSize\bin
............此處省略6個文件信息
評論
共有 條評論