当前位置: 首页 > news >正文

如何使不同的窗体控件,适应不同分辨率的屏幕?

问题

当屏幕分辨率提高或降低时,原分辨率显示正常的控件,将变得很小或很大,字体也变得太大或太小。

解决办法

当分辨率变化时,采用递归的方法,对所有的控件放大或缩小。


        public static void MainForm_Load(object sender, EventArgs e)
        {
            // 获取当前屏幕分辨率
            Screen screen = Screen.PrimaryScreen;
            int currentWidth = screen.Bounds.Width;
            int currentHeight = screen.Bounds.Height;
            Control t =(Control) sender;
            if (t.Width > 1700|| t.Width<1000)
            {
                // 计算宽度和高度的缩放因子
                float widthScale = (float)currentWidth / 1920;
                float heightScale = (float)currentHeight / 1080;

                // 应用缩放因子
                ScaleControls((Control)sender, widthScale, heightScale);
            }
        }

        public static void ScaleControls(Control parentControl, float widthScale, float heightScale)
        {
            foreach (Control control in parentControl.Controls)
            {
                
                // 调整控件的大小和位置
                control.Left = (int)(control.Left * widthScale);
                control.Top = (int)(control.Top * heightScale);
                control.Width = (int)(control.Width * widthScale);
                control.Height = (int)(control.Height * heightScale);
                if (control.Width > 1700)
                {
                    // 调整字体大小
                    control.Font = new Font(control.Font.FontFamily, control.Font.Size * Math.Min(widthScale, heightScale));
                }
                else
                    control.Font = new Font("宋体", 9);

                // 递归处理子控件
                if (control.HasChildren)
                {
                    ScaleControls(control, widthScale, heightScale);
                }
            }

相关文章:

  • 从零开始:Windows 系统中 PowerShell 配置 FFmpeg 的详细步骤
  • 基于javaweb的SpringBoot驾校预约学习系统设计与实现(源码+文档+部署讲解)
  • Mysql 索引性能分析
  • 欢迎使用Markdown编辑器
  • 职能型组织、项目型组织、矩阵型组织的介绍及优缺点比较
  • 华为OD机试2025A卷 - 正则表达式替换(Java Python JS C++ C )
  • NX/UG二次开发—CAM获取加工操作的最低Z深度值的方法
  • 【数据结构篇】算法征途:穿越时间复杂度与空间复杂度的迷雾森林
  • 基于javaweb的SpringBoot实验室管理系统设计与实现(源码+文档+部署讲解)
  • 【差分隐私相关概念】差分隐私中的稀疏向量技术
  • Java虚拟机JVM知识点(持续更新)
  • 解决element plus el-dialog 被el-header覆盖问题
  • 【 <二> 丹方改良:Spring 时代的 JavaWeb】之 Spring Boot 中的 AOP:实现日志记录与性能监控
  • 阻塞式IO与非阻塞IO的区别
  • 【当你在抖音滑动手指时,时间正在被做成期货交易】
  • 八股总结(go)实时更新!
  • 【AI大模型系列】DeepSeek V3的混合专家模型机制-MoE架构(八)
  • HarmonyOS NEXT开发进阶(十五):日志打印 hilog 与 console.log 的区别
  • Android adb 的功能和用法
  • OpenCV图像拼接(8)用于实现并查集(也称为不相交集合)数据结构类cv::detail::DisjointSets
  • 网站建设的公司有哪些内容/如何推广自己的产品
  • wordpress comicpress/上海网络seo
  • 网站下拉框怎么做/emlog友情链接代码
  • 湖南企业网站制作/上海搜索推广
  • 企业活动网站创意案例/在线的crm系统软件
  • ip做网站域名/网页设计代码案例