C# Winform 實(shí)現(xiàn)控件自適應(yīng)父容器大小的示例代碼
在日常開發(fā)中經(jīng)常遇到控件不能隨著父容器大小的改變而且自動(dòng)改變控件的所在位置和大小。以下是實(shí)現(xiàn)的代碼
/// <summary>
/// 根據(jù)父容器實(shí)現(xiàn)控件自適應(yīng)大小位置
/// </summary>
/// <param name="control">所需自適應(yīng)大小位置的控件</param>
private void ChangeLocationSizeByParent (Control control)
{
//記錄父容器大小,來(lái)判斷改變控件大小位置是因?yàn)楦溉萜鞯母淖冞€是通過(guò)設(shè)置控件大小位置去改變
Size parentOldSize = control.Parent.Size;
PointF locationPF = new PointF();
locationPF.X = (float)control.Location.X / (float)control.Parent.Width;
locationPF.Y = (float)control.Location.Y / (float)control.Parent.Height;
PointF sizePF = new PointF();
sizePF.X = (float)control.Width / (float)control.Parent.Width;
sizePF.Y = (float)control.Height / (float)control.Parent.Height;
control.LocationChanged += delegate (Object o, EventArgs e) {
if (control.Parent != null&&parentOldSize.Equals(control.Parent.Size))
{
locationPF.X = (float)control.Location.X / (float)control.Parent.Width;
locationPF.Y = (float)control.Location.Y / (float)control.Parent.Height;
}
};
control.SizeChanged += delegate (Object o, EventArgs e) {
if (control.Parent != null && parentOldSize.Equals(control.Parent.Size))
{
sizePF.X = (float)control.Width / (float)control.Parent.Width;
sizePF.Y = (float)control.Height / (float)control.Parent.Height;
}
};
control.ParentChanged += delegate (Object o, EventArgs e) {
if (control.Parent == null)
{
return;
}
locationPF.X = (float)control.Location.X / (float)control.Parent.Width;
locationPF.Y = (float)control.Location.Y / (float)control.Parent.Height;
sizePF.X = (float)control.Width / (float)control.Parent.Width;
sizePF.Y = (float)control.Height / (float)control.Parent.Height;
};
control.Parent.SizeChanged += delegate (Object po, EventArgs pe) {
Control pControl = (Control)po;
int x = (int)(pControl.Width * locationPF.X);
int y = (int)(pControl.Height * locationPF.Y);
control.Location = new Point(x, y);
int width = (int)(pControl.Width * sizePF.X);
int hetght = (int)(pControl.Height * sizePF.Y);
control.Size = new Size(width, hetght);
control.Refresh();
parentOldSize = pControl.Size;
};
}
到此這篇關(guān)于C# Winform 實(shí)現(xiàn)控件自適應(yīng)父容器大小的示例代碼的文章就介紹到這了,更多相關(guān)C# Winform 控件自適應(yīng)父容器大小內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C# WinForm遍歷窗體控件的3種方法
- C# WinForm-Timer控件的使用
- c# Winform自定義控件-儀表盤功能
- C# winform自定義翻頁(yè)控件詳解
- C# WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能示例
- C# WinForm制作異形窗體與控件的方法
- C#中WinForm控件的拖動(dòng)和縮放的實(shí)現(xiàn)代碼
- C# WinForm控件對(duì)透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡(jiǎn)單解決方法
- C# Winform 子窗體訪問(wèn)父級(jí)窗體的控件和屬性
- c# Winform同一數(shù)據(jù)源多個(gè)控件保持同步
相關(guān)文章
WPF+SkiaSharp實(shí)現(xiàn)自繪彈幕效果
這篇文章主要為大家詳細(xì)介紹了如何利用WPF和SkiaSharp實(shí)現(xiàn)自制彈幕效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09
C#中實(shí)現(xiàn)在32位、64位系統(tǒng)下自動(dòng)切換不同的SQLite dll文件
這篇文章主要介紹了C#中實(shí)現(xiàn)在32位、64位系統(tǒng)下自動(dòng)切換不同的SQLite dll文件,本文使用C#代碼實(shí)現(xiàn)DLL文件的切換,需要的朋友可以參考下2014-09-09
VS2019屬性管理器沒有Microsoft.Cpp.x64.user的解決辦法
這篇文章主要介紹了VS2019屬性管理器沒有Microsoft.Cpp.x64.user的解決辦法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
基于C#的音樂播放器主Form實(shí)現(xiàn)代碼
這篇文章主要介紹了基于C#的音樂播放器主Form實(shí)現(xiàn)代碼,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
C# DataSet的內(nèi)容寫成XML時(shí)如何格式化字段數(shù)據(jù)
許多讀者經(jīng)常詢問(wèn)一個(gè)問(wèn)題,那就是在將DataSet的內(nèi)容寫成XML時(shí),如何格式化字段數(shù)據(jù)。最常見的需求,就是希望日期時(shí)間值與數(shù)值數(shù)據(jù)能夠以所需的格式呈現(xiàn)于XML中。2009-02-02

