winform缩放时,控制控件的比例

1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.ComponentModel;
 4 using  System.Data;
 5 using  System.Drawing;
 6 using  System.Text;
 7 using  System.Windows.Forms;
 8
 9 namespace  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}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值