C# Winfrom 控件等比例放大的解决方案

Winfrom 控件跟随窗体大小变化而变化

 #region 窗体基本事件,这里需要根据自己的窗体设置相应事件
        /// 
        /// 窗体加载事件
        /// 
        /// 
        /// 
        private void Form_Load(object sender, EventArgs e)
        {
            serverFrom = this;
            
            ChangeFormSize();//设置变化窗体大小
        }

        /// 
        /// 窗体的 Resize 事件
        /// 
        /// 
        /// 
        private void From_Resize(object sender, EventArgs e)
        {
            float WidthChangeProportion = (this.Width) / CurrentWidth;//宽度变化比例
            float HeightChangeProportion = (this.Height) / CurrentHeight;//高度变化比例
            UpdateControlsSize(WidthChangeProportion, HeightChangeProportion, this);
        }

 #endregion
  #region 控件大等比例放大,可以直接搬运代码

        /// 
        /// 当前窗体宽度
        /// 
        private float CurrentWidth { get; set; } 
        /// 
        /// 当前窗体高度
        /// 
        private float CurrentHeight { get; set; }
        /// 
        /// 改变窗体大小
        /// 
        private void ChangeFormSize()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            //SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲DoubleBuffer
            CurrentWidth = this.Width;
            CurrentHeight = this.Height;
            SetControTag(this);
        }

      /// 
      /// 设置控件 Tag 数据
      /// 
      /// 
        private void SetControTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    SetControTag(con);
                }
            }
        }
        /// 
        /// 设置控件双缓冲区
        /// 
        /// 
        public static void SetControlDoubleBuffer(Control cc)
        {

            cc.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance |
                         System.Reflection.BindingFlags.NonPublic).SetValue(cc, true, null);

        }
        /// 
        /// 更新控件的大小
        /// 
        /// 
        /// 
        /// 
        private void UpdateControlsSize(float WidthChangeProportion, float HeightChangeProportion, Control cons)
        {
            //遍历窗体中的控件,重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值,并分割后存储字符串数组
                SetControlDoubleBuffer(this);
                SetControlDoubleBuffer(con);
                if (con.Tag != null)
                {

                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * WidthChangeProportion);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * HeightChangeProportion);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * WidthChangeProportion);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * HeightChangeProportion);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * HeightChangeProportion;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        UpdateControlsSize(HeightChangeProportion, HeightChangeProportion, con);
                    }
                }


            }
        }

        #endregion

你可能感兴趣的:(C#Winfrom,ASP.NET,C#,自适应大小,winfrom)