马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

form控件随窗体大小变化

[复制链接]
查看3296 | 回复2 | 2018-3-19 14:50:24 | 显示全部楼层 |阅读模式
对于设计完窗体后,运行时发现窗体最大化后控件和原来一样,这段祖传代码帮新人解决问题

[AppleScript] 纯文本查看 复制代码
        /*
         * 设置窗口控件随窗口大小改变而改变
         */
        public new void AutoScale(Form frm)
        {
            frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
            frm.SizeChanged += new EventHandler(frm_SizeChanged);
        }
        public void frm_SizeChanged(object sender, EventArgs e)
        {
            string[] tmp = ((Form)sender).Tag.ToString().Split(',');
            float width = (float)((Form)sender).Width / (float)Convert.ToInt32(tmp[0]);
            float height = (float)((Form)sender).Height / (float)Convert.ToInt32(tmp[1]);
            ((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
            string str = ((Form)sender).Tag.ToString();
            // int font_size = Int32.Parse(str.Substring(0, str.IndexOf(','))) / 100;
            //也可使字体随之改变
            float tempWidth = 0F;
            float tempHeight = 0F;
            foreach (Control control in ((Form)sender).Controls)
            {
                if (control is DataGridView)
                    continue;
                if (control is TextBox)
                {
                    tempHeight = height;
                    tempWidth = width;
                }
                if (control is Button)
                {
                    if (this.WindowState == FormWindowState.Maximized)
                        tempHeight -= 0.4F;
                    else
                        tempHeight += 0.2F;
                    control.Scale(new SizeF(tempWidth, tempHeight));
                }
                else
                {
                    try
                    {
                        control.Scale(new SizeF(width, height));
                    }
                    catch
                    {
                    }
                }
            }
        }

bestwmb | 2018-3-19 14:52:33 | 显示全部楼层
在form 的load 事件中 调用
[AppleScript] 纯文本查看 复制代码
AutoScale(this);//设置窗口控件随窗口大小改变而改变
ibcadmin | 2018-3-20 14:13:41 | 显示全部楼层
6666666666
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则