今天在csdn的icode中C#专栏中看到这样的代码,自己觉得挺好用,就拿出来分享了。
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication13
10{
11 public partial class Form1 : Form
12 {
13 double formoldwidth; //窗体原始宽度
14 double formoldheight; //窗体原始高度
15
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void Form1_Load(object sender, EventArgs e)
22 {
23 double scalewh; //控件宽高比
24
25 formoldwidth = (double)this.Width;
26 formoldheight = (double)this.Height;
27 foreach (Control ctrl in this.Controls)
28 {
29 scalewh = (double)ctrl.Width / (double)ctrl.Height;
30 ctrl.Tag = ctrl.Left + " " + ctrl.Top + " " + ctrl.Width + " " + scalewh.ToString() + " "; //将控件的Left,Top,Width,宽高比放入控件的Tag内
31 }
32 }
33
34 private void Form1_Resize(object sender, EventArgs e)
35 {
36 double scalex; //水平伸缩比
37 double scaley; //垂直伸缩比
38 long i;
39 int temppos;
40 string temptag;
41 double[] pos = new double[4]; //pos数组保存当前控件的left,top,width,height
42
43 scalex = (double)this.Width / formoldwidth;
44 scaley = (double)this.Height / formoldheight;
45 foreach (Control ctrl in this.Controls)
46 {
47 temptag = ctrl.Tag.ToString();
48 for (i = 0; i <= 3; i++)
49 {
50 temppos = temptag.IndexOf(" ");
51 if (temppos > 0)
52 {
53 pos[i] = Convert.ToDouble(temptag.Substring(0, temppos)); //从Tag中取出参数
54 temptag = temptag.Substring(temppos + 1);
55 }
56 else
57 pos[i] = 0;
58 }
59 ctrl.Left = (int)(pos[0] * scalex);
60 ctrl.Top = (int)(pos[1] * scaley);
61 ctrl.Width = (int)(pos[2] * scalex);
62 ctrl.Height = (int)((double)ctrl.Width / pos[3]); //高度由宽高比算出
63 }
64 }
65 }
66}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication13
10{
11 public partial class Form1 : Form
12 {
13 double formoldwidth; //窗体原始宽度
14 double formoldheight; //窗体原始高度
15
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void Form1_Load(object sender, EventArgs e)
22 {
23 double scalewh; //控件宽高比
24
25 formoldwidth = (double)this.Width;
26 formoldheight = (double)this.Height;
27 foreach (Control ctrl in this.Controls)
28 {
29 scalewh = (double)ctrl.Width / (double)ctrl.Height;
30 ctrl.Tag = ctrl.Left + " " + ctrl.Top + " " + ctrl.Width + " " + scalewh.ToString() + " "; //将控件的Left,Top,Width,宽高比放入控件的Tag内
31 }
32 }
33
34 private void Form1_Resize(object sender, EventArgs e)
35 {
36 double scalex; //水平伸缩比
37 double scaley; //垂直伸缩比
38 long i;
39 int temppos;
40 string temptag;
41 double[] pos = new double[4]; //pos数组保存当前控件的left,top,width,height
42
43 scalex = (double)this.Width / formoldwidth;
44 scaley = (double)this.Height / formoldheight;
45 foreach (Control ctrl in this.Controls)
46 {
47 temptag = ctrl.Tag.ToString();
48 for (i = 0; i <= 3; i++)
49 {
50 temppos = temptag.IndexOf(" ");
51 if (temppos > 0)
52 {
53 pos[i] = Convert.ToDouble(temptag.Substring(0, temppos)); //从Tag中取出参数
54 temptag = temptag.Substring(temppos + 1);
55 }
56 else
57 pos[i] = 0;
58 }
59 ctrl.Left = (int)(pos[0] * scalex);
60 ctrl.Top = (int)(pos[1] * scaley);
61 ctrl.Width = (int)(pos[2] * scalex);
62 ctrl.Height = (int)((double)ctrl.Width / pos[3]); //高度由宽高比算出
63 }
64 }
65 }
66}