資源簡介
WinForm窗體及其控件的自適應,對于容器內(nèi)的控件同樣有效

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
namespace?WinForm窗體及其控件的自適應
{
????class?AutoSizeFormClass
????{
????????//(1).聲明結構只記錄窗體和其控件的初始位置和大小。
????????public?struct?controlRect
????????{
????????????public?int?Left;
????????????public?int?Top;
????????????public?int?Width;
????????????public?int?Height;
????????}
????????//(2).聲明?1個對象
????????//注意這里不能使用控件列表記錄?List?nCtrl;,因為控件的關聯(lián)性,記錄的始終是當前的大小。
????????//??????public?List?oldCtrl=?new?List();//這里將西文的大于小于號都過濾掉了,只能改為中文的,使用中要改回西文
????????public?List?oldCtrl?=?new?List();
????????int?ctrlNo?=?0;//1;
????????//(3).?創(chuàng)建兩個函數(shù)
????????//(3.1)記錄窗體和其控件的初始位置和大小
????????public?void?controllInitializeSize(Control?mForm)
????????{
????????????controlRect?cR;
????????????cR.Left?=?mForm.Left;?cR.Top?=?mForm.Top;?cR.Width?=?mForm.Width;?cR.Height?=?mForm.Height;
????????????oldCtrl.Add(cR);//第一個為“窗體本身“只加入一次即可
????????????AddControl(mForm);//窗體內(nèi)其余控件還可能嵌套控件(比如panel)要單獨抽出因為要遞歸調(diào)用
????????????//this.WindowState?=?(System.Windows.Forms.FormWindowState)(2);//記錄完控件的初始位置和大小后,再最大化
????????????//0?-?Normalize??1?-?Minimize2-?Maximize
????????}
????????private?void?AddControl(Control?ctl)
????????{
????????????foreach?(Control?c?in?ctl.Controls)
????????????{??//**放在這里,是先記錄控件的子控件,后記錄控件本身
????????????????//if?(c.Controls.Count?>?0)
????????????????//????AddControl(c);//窗體內(nèi)其余控件還可能嵌套控件(比如panel)要單獨抽出因為要遞歸調(diào)用
????????????????controlRect?objCtrl;
????????????????objCtrl.Left?=?c.Left;?objCtrl.Top?=?c.Top;?objCtrl.Width?=?c.Width;?objCtrl.Height?=?c.Height;
????????????????oldCtrl.Add(objCtrl);
????????????????//**放在這里,是先記錄控件本身,后記錄控件的子控件
????????????????if?(c.Controls.Count?>?0)
????????????????????AddControl(c);//窗體內(nèi)其余控件還可能嵌套控件(比如panel)要單獨抽出因為要遞歸調(diào)用
????????????}
????????}
????????//(3.2)控件自適應大小
????????public?void?controlAutoSize(Control?mForm)
????????{
????????????if?(ctrlNo?==?0)
????????????{?//*如果在窗體的Form1_Load中,記錄控件原始的大小和位置,正常沒有問題,但要加入皮膚就會出現(xiàn)問題,因為有些控件如dataGridView的的子控件還沒有完成,個數(shù)少
????????????????//*要在窗體的Form1_SizeChanged中,第一次改變大小時,記錄控件原始的大小和位置這里所有控件的子控件都已經(jīng)形成
????????????????controlRect?cR;
????????????????//??cR.Left?=?mForm.Left;?cR.Top?=?mForm.Top;?cR.Width?=?mForm.Width;?cR.Height?=?mForm.Height;
????????????????cR.Left?=?0;?cR.Top?=?0;?cR.Width?=?mForm.PreferredSize.Width;?cR.Height?=?mForm.PreferredSize.Height;
????????????????oldCtrl.Add(cR);//第一個為“窗體本身“只加入一次即可
????????????????AddControl(mForm);//窗體內(nèi)其余控件可能嵌套其它控件(比如panel)故單獨抽出以便遞歸調(diào)用
????????????}
????????????float?wScale?=?(float)mForm.Width?/?(float)oldCtrl[0].Width;//新舊窗體之間的比例,與最早的舊窗體
????????????float?hScale?=?(float)mForm.Height?/?(float)oldCtrl[0].Height;//.Height;
????????????ctrlNo?=?1;//進入=1,第0個為窗體本身窗體內(nèi)的控件從序號1開始
????????????AutoScaleControl(mForm?wScale?hScale);//窗體內(nèi)其余控件還可能嵌套控件(比如panel)要單獨抽出因為要遞歸調(diào)用
????????}
????????private?void?AutoScaleControl(Control?ctl?float?wScale?float?hScale)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-10-29?11:07??WinForm窗體及其控件的自適應\
?????文件????????1001??2015-10-29?10:52??WinForm窗體及其控件的自適應.sln
?????文件????????8704??2015-10-29?10:52??WinForm窗體及其控件的自適應.suo
?????文件????????6729??2015-10-29?10:56??WinForm窗體及其控件的自適應\AutoSizeFormClass.cs
?????目錄???????????0??2015-10-29?10:52??WinForm窗體及其控件的自適應\bin\
?????目錄???????????0??2015-10-29?10:56??WinForm窗體及其控件的自適應\bin\Debug\
?????文件???????13312??2015-10-29?11:07??WinForm窗體及其控件的自適應\bin\Debug\WinForm窗體及其控件的自適應.exe
?????文件???????34304??2015-10-29?11:07??WinForm窗體及其控件的自適應\bin\Debug\WinForm窗體及其控件的自適應.pdb
?????文件???????14328??2015-10-29?11:08??WinForm窗體及其控件的自適應\bin\Debug\WinForm窗體及其控件的自適應.vshost.exe
?????文件?????????490??2009-06-11?05:14??WinForm窗體及其控件的自適應\bin\Debug\WinForm窗體及其控件的自適應.vshost.exe.manifest
?????文件?????????711??2015-10-29?11:07??WinForm窗體及其控件的自適應\Form1.cs
?????文件???????10616??2015-10-29?11:07??WinForm窗體及其控件的自適應\Form1.Designer.cs
?????文件????????5814??2015-10-29?11:07??WinForm窗體及其控件的自適應\Form1.resx
?????目錄???????????0??2015-10-29?10:52??WinForm窗體及其控件的自適應\obj\
?????目錄???????????0??2015-10-29?11:07??WinForm窗體及其控件的自適應\obj\Debug\
?????目錄???????????0??2015-10-29?10:52??WinForm窗體及其控件的自適應\obj\Debug\TempPE\
?????文件????????1518??2015-10-29?11:08??WinForm窗體及其控件的自適應\obj\Debug\WinForm窗體及其控件的自適應.csproj.FileListAbsolute.txt
?????文件?????????847??2015-10-29?11:07??WinForm窗體及其控件的自適應\obj\Debug\WinForm窗體及其控件的自適應.csproj.GenerateResource.Cache
?????文件???????13312??2015-10-29?11:07??WinForm窗體及其控件的自適應\obj\Debug\WinForm窗體及其控件的自適應.exe
?????文件?????????180??2015-10-29?11:07??WinForm窗體及其控件的自適應\obj\Debug\WinForm窗體及其控件的自適應.Form1.resources
?????文件???????34304??2015-10-29?11:07??WinForm窗體及其控件的自適應\obj\Debug\WinForm窗體及其控件的自適應.pdb
?????文件?????????180??2015-10-29?10:56??WinForm窗體及其控件的自適應\obj\Debug\WinForm窗體及其控件的自適應.Properties.Resources.resources
?????文件?????????518??2015-10-29?10:52??WinForm窗體及其控件的自適應\Program.cs
?????目錄???????????0??2015-10-29?10:52??WinForm窗體及其控件的自適應\Properties\
?????文件????????1406??2015-10-29?10:52??WinForm窗體及其控件的自適應\Properties\AssemblyInfo.cs
?????文件????????2924??2015-10-29?10:52??WinForm窗體及其控件的自適應\Properties\Resources.Designer.cs
?????文件????????5612??2015-10-29?10:52??WinForm窗體及其控件的自適應\Properties\Resources.resx
?????文件????????1122??2015-10-29?10:52??WinForm窗體及其控件的自適應\Properties\Settings.Designer.cs
?????文件?????????249??2015-10-29?10:52??WinForm窗體及其控件的自適應\Properties\Settings.settings
?????文件????????3833??2015-10-29?10:56??WinForm窗體及其控件的自適應\WinForm窗體及其控件的自適應.csproj
- 上一篇:C#編寫的溫度采集上位機程序源碼
- 下一篇:C#自動升級程序
評論
共有 條評論