窗体控件随窗体大小改变(仍有不足)
生活随笔
收集整理的這篇文章主要介紹了
窗体控件随窗体大小改变(仍有不足)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
private void frmWatch_Load(object sender, EventArgs e){FormWidth = this.Width;FormHeight = this.Height;SetTag(this);}/// <summary>/// 窗體控件自適應大小/// </summary>private float formWidth;private float formHeight;public float FormHeight{get { return formHeight; }set { formHeight = value; }}public float FormWidth{get { return formWidth; }set { formWidth = value; }}/// <summary>/// 設置Tag標簽/// </summary>/// <param name="controls">控件</param>public void SetTag(Control controls){foreach (Control control in controls.Controls){control.Tag = control.Width + ":" + control.Height + ":" + control.Left + ":" + control.Top + ":" + control.Font.Size;if (control.Controls.Count > 0){SetTag(control);}}}/// <summary>/// 設置控件大小/// </summary>/// <param name="newX">X坐標</param>/// <param name="newY">Y坐標</param>/// <param name="controls">控件</param>/// <summary>private void SetControls(float newX, float newY, Control controls){foreach (Control control in controls.Controls){string[] myTag = control.Tag.ToString().Split(':');//控件的寬float length = Convert.ToSingle(myTag[0]) * newX;control.Width = (int)length;//控件的高length = Convert.ToSingle(myTag[1]) * newY;control.Height = (int)length;//控件的X坐標length = Convert.ToSingle(myTag[2]) * newX;control.Left = (int)length;//控件的Y坐標length = Convert.ToSingle(myTag[3]) * newY;control.Top = (int)length;Single currentSize = Convert.ToSingle(myTag[4])*newY;control.Font = new System.Drawing.Font(control.Font.Name, currentSize, control.Font.Style, control.Font.Unit);if (control.Controls.Count > 0){SetControls(newX,newY,control);}}}/// <summary>/// 調整控件的大小/// </summary>public void ControlResize(Control control){float newX = control.Width / FormWidth;float newY = control.Height / FormHeight; ;SetControls(newX,newY,control);}private void frmWatch_Resize(object sender, EventArgs e){ControlResize(this);}//form的屬性AutoSize 默認為faule 不要設置成true
?
轉載于:https://www.cnblogs.com/Iyce/archive/2013/01/11/2856107.html
總結
以上是生活随笔為你收集整理的窗体控件随窗体大小改变(仍有不足)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DebugDiagx检测内存泄露
- 下一篇: Lesson_7 上课笔记_1 ----