码迷,mamicode.com
首页 > Windows程序 > 详细

winform 控件大小随着窗体自适应

时间:2018-04-24 21:52:59      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:void   ble   red   todo   情况下   arp   字体   width   new   

3个方法:

#region 改变控件大小
        //获取控件原始信息
        protected void GetAllInitInfo(Control ctrlContainer)
        {
            //int tempWidth = Screen.PrimaryScreen.Bounds.Width / 5 * 4;
            //int tempHeight = Screen.PrimaryScreen.Bounds.Height / 5 * 4;
            if (ctrlContainer.Parent == this)//获取窗体的高度和宽度
            {
                formWidth = Convert.ToDouble(ctrlContainer.Width);
                formHeight = Convert.ToDouble(ctrlContainer.Height);
            }
            foreach (Control item in ctrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                {
                    //添加信息:键值:控件名,内容:据左边距离,距顶部距离,控件宽度,控件高度,控件字体。
                    ControlsInfo.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 ctrlContainer)
        {
            //scaleX = (double)4 / 5;
            //scaleY = (double)4 / 5;
            scaleX = (Convert.ToDouble(ctrlContainer.Width) / formWidth);
            scaleY = (Convert.ToDouble(ctrlContainer.Height) / formHeight);
        }

        //窗体改变时修改控件大小
        private void ControlsChange(Control ctrlContainer)
        {
            double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size

            foreach (Control item in ctrlContainer.Controls)//遍历控件
            {
                if (item.Name.Trim() != "")//如果控件名不是空,则执行
                {
                    if ((item as UserControl) == null && item.Controls.Count > 0)//如果不是自定义控件
                    {
                        ControlsChange(item);//循环执行
                    }
                    string[] strs = ControlsInfo[item.Name].Split(‘,‘);//从字典中查出的数据,以‘,’分割成字符串组

                    for (int i = 0; i < 5; i++)
                    {
                        pos[i] = Convert.ToDouble(strs[i]);//添加到临时数组
                    }
                    double itemWidth = pos[2] * scaleX;     //计算控件宽度,double类型
                    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);//控件宽度,int类型
                    item.Height = Convert.ToInt32(itemHeight);//控件高度
                    item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));//字体
                }
            }
        }

        private void FormNewInfraredPicture_SizeChanged(object sender, EventArgs e)
        {
            if (sizeBool2)
            {
                if (ControlsInfo.Count > 0)//如果字典中有数据,即窗体改变
                {
                    ControlsChangeInit(this.Controls[0]);//表示pannel控件
                    ControlsChange(this.Controls[0]);
                }
            }
            //if (ControlsInfo.Count > 0)//如果字典中有数据,即窗体改变
            //{
            //    ControlsChangeInit(this.Controls[0]);//表示pannel控件
            //    ControlsChange(this.Controls[0]);
            //}
        }
        #endregion

其中是窗体sizeChanged事件调用和构造函数开始记录控件初始化信息;

注:在非开发环境的电脑上会出现改变分辨率出粗情况,

看其执行顺序会发现,改变分辨率情况下,窗口自动改变,程序运行sizechanged事件会出错,

仅需在初始化窗体时候屏蔽sizeChanged事件执行即可。

winform 控件大小随着窗体自适应

标签:void   ble   red   todo   情况下   arp   字体   width   new   

原文地址:https://www.cnblogs.com/gaara-zhang/p/8933411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!