zoukankan      html  css  js  c++  java
  • [转]winform缩放时,控制控件的比例

    今天在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}
     
  • 相关阅读:
    双色球相关
    儿童教学相关网站
    [转]javascript实现限制上传文件的大​​小
    使用存储过程 修改符合条件的数据表的相关字段 的数据类型
    [转]bat批处理实现TXT文本合并
    [转]在SQL中用正则表达式替换html标签
    [转]T4模版引擎之生成数据库实体类
    [转]C#中图片.BYTE[]和base64string的转换
    [书目20131114]微软技术丛书•快速软件开发(珍藏版) Rapid Development Taming Wild Software Schedules By Steve McConnell
    人体穴位
  • 原文地址:https://www.cnblogs.com/luofuxian/p/2608380.html
Copyright © 2011-2022 走看看