Winform 窗体控件最大化自适应

运行窗体效果如下:

 默认点击最大化效果如下:

修改后最大化效果如下:控件自动缩放,

步骤实现如下:

1.在窗体中放一个容器(Panel),将容器的Dock属性设置为Fill。窗体中所有控件都放入这个容器中。

2.创建一个窗体类,该窗体类继承于原始窗体类,原来的窗体继承创建的窗体类:如下图所示

 新建一个 NForm 窗体类,继承默认窗体类 Form ,而原来的 Form1 :Form 窗体类继承的默认窗体类修改为 Form1 :NForm 自定义新建的窗体类。

新建窗体类代码如下:

  public partial class NForm : Form
    {
        #region 控件缩放
        double formWidth;//窗体原始宽度
        double formHeight;//窗体原始高度
        double scaleX;//水平缩放比例
        double scaleY;//垂直缩放比例
        Dictionary<string, string> controlInfo = new Dictionary<string, string>();
        //控件中心Left,Top,控件Width,控件Height,控件字体Size
        /// <summary>
        /// 获取所有原始数据
        /// </summary>
        protected void GetAllInitInfo(Control CrlContainer)
        {
            if (CrlContainer.Parent == this)
            {
                formWidth = Convert.ToDouble(CrlContainer.Width);
                formHeight = Convert.ToDouble(CrlContainer.Height);
            }
            foreach (Control item in CrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                    controlInfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size);
                if ((item as UserControl) == null && item.Controls.Count > 0)
                    GetAllInitInfo(item);
            }
        }
        private void ControlsChangeInit(Control CrlContainer)
        {
            scaleX = (Convert.ToDouble(CrlContainer.Width) / formWidth);
            scaleY = (Convert.ToDouble(CrlContainer.Height) / formHeight);
        }
        private void ControlsChange(Control CrlContainer)
        {
            double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size
            foreach (Control item in CrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                {
                    if ((item as UserControl) == null && item.Controls.Count > 0)
                        ControlsChange(item);
                    string[] strs = controlInfo[item.Name].Split(',');
                    for (int j = 0; j < 5; j++)
                    {
                        pos[j] = Convert.ToDouble(strs[j]);
                    }
                    double itemWidth = pos[2] * scaleX;
                    double itemHeight = pos[3] * scaleY;
                    item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);
                    item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);
                    item.Width = Convert.ToInt32(itemWidth);
                    item.Height = Convert.ToInt32(itemHeight);
                    item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));
                }
            }
        }

        #endregion


        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (controlInfo.Count > 0)
            {
                ControlsChangeInit(this.Controls[0]);
                ControlsChange(this.Controls[0]);
            }
        }
    }

新建的窗体类中主要包括自定义几个方法,用以实现控件自适应

(1)获取控件初始信息;GetAllInitInfo()              

(2)获取窗体缩放比例;ControlsChaneInit()             

(3)窗体改变时修改控件大小。ControlsChange()

最后。在窗体类的构造函数中调用获取初始数据的方法:

 public Form1()
        {
            InitializeComponent();
            GetAllInitInfo(this.Controls[0]);
        }

这样,一个自适应窗体就实现了,再也不用担心最大化和拖拽后窗体控件位置错位的尴尬了。如果对你有用,请赞一个,欢迎评论留言,更多资料信息请关注  QQ 733530469

猜你喜欢

转载自blog.csdn.net/qq_21419015/article/details/83855275
今日推荐