• 窗体控件随窗体大小改变(仍有不足)


      private void frmWatch_Load(object sender, EventArgs e)
            {
                FormWidth = this.Width;
                FormHeight = this.Height;
                SetTag(this);
            }
         /// <summary>
               /// 窗体控件自适应大小
               /// </summary>
    
            private float formWidth;
            private float formHeight;
    
            public float FormHeight
            {
                get { return formHeight; }
                set { formHeight = value; }
            }
    
            public float FormWidth
            {
                get { return formWidth; }
                set { formWidth = value; }
            }
            /// <summary>
            /// 设置Tag标签
            /// </summary>
            /// <param name="controls">控件</param>
            public void SetTag(Control controls)
            {
                foreach (Control control in controls.Controls)
                {
                    control.Tag = control.Width + ":" + control.Height + ":" + control.Left + ":" + control.Top + ":" + control.Font.Size;
                    if (control.Controls.Count > 0)
                    {
                        SetTag(control);
                    }
                }
            }
            /// <summary>
            /// 设置控件大小
            /// </summary>
            /// <param name="newX">X坐标</param>
            /// <param name="newY">Y坐标</param>
            /// <param name="controls">控件</param>
            /// <summary>
            private void SetControls(float newX, float newY, Control controls)
            {
                foreach (Control control in controls.Controls)
                {
                    string[] myTag = control.Tag.ToString().Split(':');
                    //控件的宽
                    float length = Convert.ToSingle(myTag[0]) * newX;
                    control.Width = (int)length;
                    //控件的高
                    length = Convert.ToSingle(myTag[1]) * newY;
                    control.Height = (int)length;
                    //控件的X坐标
                    length = Convert.ToSingle(myTag[2]) * newX;
                    control.Left = (int)length;
                    //控件的Y坐标
                    length = Convert.ToSingle(myTag[3]) * newY;
                    control.Top = (int)length;
    
                    Single currentSize = Convert.ToSingle(myTag[4])*newY;
    
                    control.Font = new System.Drawing.Font(control.Font.Name, currentSize, control.Font.Style, control.Font.Unit);
    
                    if (control.Controls.Count > 0)
                    {
                        SetControls(newX,newY,control);
                    }
                }
            }
            /// <summary>
            /// 调整控件的大小
            /// </summary>
            public void ControlResize(Control control)
            {
                float newX = control.Width / FormWidth;
                float newY = control.Height / FormHeight; ;
                SetControls(newX,newY,control);
            }
    
            private void frmWatch_Resize(object sender, EventArgs e)
            {
                ControlResize(this);
            }
            //form的属性AutoSize  默认为faule 不要设置成true
  • 相关阅读:
    Java可变参数
    为什么static方法中不可以调用非static方法
    用注解@DelcareParents实现引用增强
    在SpringBoot中用SpringAOP实现日志记录功能
    梳理一下我理解的aop
    包裹iframe的div与iframe存在高度差的问题解决方案
    非跨域情况下iframe 高度自适应的问题解决(一)
    flex布局较之float布局的优点新发现
    webpack4 动态导入文件 dynamic-import 报错的解决方法
    vue chrome 浏览器调试工具devtools插件安装
  • 原文地址:https://www.cnblogs.com/Iyce/p/2856107.html
Copyright © 2020-2023  润新知