C#實(shí)現(xiàn)窗體中的各個(gè)控件同比自動(dòng)放縮大小
實(shí)現(xiàn)方式主要是利用panel控件為主題,對(duì)于每個(gè)控件的大小位置和字體這幾個(gè)屬性進(jìn)行記錄,然后根據(jù)窗體改變的大小同時(shí)放縮。
簡(jiǎn)要步驟如下:
1、創(chuàng)建C#窗體程序項(xiàng)目。
2、Panel放置到窗體。
3、設(shè)置屬性dock為fill。
4、注意MinnumSize不能設(shè)置為0, 改成大于0都行。
public partial class FrmDemo : Form
{
double dFrmWidth;
double dFrmHeight;
double dZoomHorizon;
double dZoomVerticality;
Dictionary<string, string> dicControlsAttribute = new Dictionary<string, string>();
protected void GetAllInitiateContrlInfo(Control CrlContainer)
{
if (CrlContainer.Parent == this)
{
dFrmWidth = Convert.ToDouble(CrlContainer.Width);
dFrmHeight = Convert.ToDouble(CrlContainer.Height);
}
foreach (Control item in CrlContainer.Controls)
{
if (item.Name.Trim() != "")
dicControlsAttribute.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2)
+ "," + item.Width + "," + item.Height + "," + item.Font.Size);
if ((item as UserControl) == null && item.Controls.Count > 0)
GetAllInitiateContrlInfo(item);
}
}
private void ChangeControlsInitiate(Control CrlContainer)
{
dZoomHorizon = (Convert.ToDouble(CrlContainer.Width) / dFrmWidth);
dZoomVerticality = (Convert.ToDouble(CrlContainer.Height) / dFrmHeight);
}
private void ChangeCurrentControlAttr(Control CrlContainer)
{
double[] dPosition = new double[5];
foreach (Control item in CrlContainer.Controls)
{
if (item.Name.Trim() != "")
{
if ((item as UserControl) == null && item.Controls.Count > 0)
ChangeCurrentControlAttr(item);
string[] strs = dicControlsAttribute[item.Name].Split(',');
for (int j = 0; j < 5; j++)
{
dPosition[j] = Convert.ToDouble(strs[j]);
}
double itemWidth = dPosition[2] * dZoomHorizon;
double itemHeight = dPosition[3] * dZoomVerticality;
item.Left = Convert.ToInt32(dPosition[0] * dZoomHorizon - itemWidth / 2);
item.Top = Convert.ToInt32(dPosition[1] * dZoomVerticality - itemHeight / 2);
item.Width = Convert.ToInt32(itemWidth);
item.Height = Convert.ToInt32(itemHeight);
//item.Font = new Font(item.Font.Name, float.Parse
//((dPosition[4] * Math.Min(dZoomHorizon, dZoomVerticality)).ToString()));
//字體也可以實(shí)現(xiàn)同比放縮。
}
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (dicControlsAttribute.Count > 0)
{
ChangeControlsInitiate(this.Controls[0]);
ChangeCurrentControlAttr(this.Controls[0]);
}
}
public FrmDemo()
{
InitializeComponent();
GetAllInitiateContrlInfo(this.Controls[0]);//構(gòu)造函數(shù)里面調(diào)用即可。
}
}
5、效果測(cè)試


相關(guān)文章
mongodb命令行連接及基礎(chǔ)命令總結(jié)大全
大家可能平時(shí)在開(kāi)發(fā)過(guò)程中都使用客戶端工具來(lái)連接和查詢mongodb,但是一般生產(chǎn)當(dāng)中的數(shù)據(jù)庫(kù)是不允許本地客戶端連接的,下面這篇文章主要給大家介紹了關(guān)于mongodb命令行連接及基礎(chǔ)命令總結(jié)的相關(guān)資料,需要的朋友可以參考下2024-04-04
MongoDB在系統(tǒng)數(shù)據(jù)庫(kù)local中無(wú)法創(chuàng)建用戶的解決辦法
這篇文章主要給大家介紹了關(guān)于MongoDB在系統(tǒng)數(shù)據(jù)庫(kù)local中無(wú)法創(chuàng)建用戶的解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
MongoDB錯(cuò)誤32-bit servers don''t have journaling enabled by de
這篇文章主要介紹了MongoDB錯(cuò)誤32-bit servers don't have journaling enabled by default解決方法,需要的朋友可以參考下2014-10-10
Linux下MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)自動(dòng)備份詳解
這篇文章主要給大家介紹了在Linux系統(tǒng)下下MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)自動(dòng)備份的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
MongoDB的mongo shell常用操作方法及操作腳本筆記
mongo shell即相當(dāng)于SQL語(yǔ)句在關(guān)系型數(shù)據(jù)庫(kù)中的作用,MongoDB使用JavaScript作為shell操作命令,這里我們就來(lái)整理MongoDB的mongo shell常用操作方法及操作腳本筆記2016-07-07
使用mongoose和bcrypt實(shí)現(xiàn)用戶密碼加密的示例
下面小編就為大家分享一篇使用mongoose和bcrypt實(shí)現(xiàn)用戶密碼加密的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

