窗體內的控件隨窗體的大小改變控件大小
阿新 • • 發佈:2018-07-04
控件大小 窗體 orm () vat bsp initial oid spa
public Form1() { InitializeComponent(); int count = this.Controls.Count * 2+2; float[] factor = new float[count]; int i = 0; factor[i++] = Size.Width; factor[i++] = Size.Height; foreach(Control ctrl inthis.Controls) { factor[i++] = ctrl.Location.X / (float)Size.Width; factor[i++] = ctrl.Location.Y / (float)Size.Height; ctrl.Tag = ctrl.Size; } Tag = factor; } private void Form1_Resize(objectsender, EventArgs e) { float[] scale = (float[])Tag; int i = 2; foreach (Control ctrl in this.Controls) { ctrl.Left = (int)(Size.Width * scale[i++]); ctrl.Top = (int)(Size.Height * scale[i++]); ctrl.Width= (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width); ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height); //每次使用的都是最初始的控件大小,保證準確無誤。 } }
窗體內的控件隨窗體的大小改變控件大小