C#實現(xiàn)窗體中的各個控件同比自動放縮大小
實現(xiàn)方式主要是利用panel控件為主題,對于每個控件的大小位置和字體這幾個屬性進行記錄,然后根據(jù)窗體改變的大小同時放縮。
簡要步驟如下:
1、創(chuàng)建C#窗體程序項目。
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()));
//字體也可以實現(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、效果測試


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

