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

Halcon联合C# 添加工具类让winform自动根据窗体大小自适应缩放所有控件

为什么需要自适应布局?

在现代软件开发中,应用程序需要运行在各种不同分辨率和DPI设置的设备上。传统的固定像素布局方式已无法满足需求,自适应布局成为必备技能。本文将全面解析C# WinForm中实现完美自适应布局的各类技术方案,特别针对Halcon混合开发场景进行深度优化。

下面是一个完整的自适应布局 AutoControlSize.cs工具类实现,包含构造函数初始化和控件触发逻辑:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using System.Drawing;namespace WinForms1.Attrs
{public class AutoControlSize{private static Queue<Control> MyControlQuery_Init = new Queue<Control>();//初始化控件集合队列private static ArrayList MyControlInfoList_Init = new ArrayList();//初始化控件参数集合private static Int32 MainDlg_H_Init;//初始化主对话框参数private static Int32 MainDlg_W_Init;//初始化主对话框参数private static Int32 MainDlg_H_Curr;//当前主对话框参数private static Int32 MainDlg_W_Curr;//当前主对话框参数private struct ControlInfo//控件参数结构体{public string ControlName;public Int32 Height;public Int32 Width;public Int32 Loc_X;public Int32 Loc_Y;}private static void FormControlList(Control item)//递归遍历控件{for (int i = 0; i < item.Controls.Count; i++){if (item.Controls[i].HasChildren){FormControlList(item.Controls[i]);}ControlInfo Node = new ControlInfo();Node.ControlName = item.Controls[i].Name;Node.Height = item.Controls[i].Height;Node.Width = item.Controls[i].Width;Node.Loc_X = item.Controls[i].Location.X;Node.Loc_Y = item.Controls[i].Location.Y;MyControlInfoList_Init.Add(Node);MyControlQuery_Init.Enqueue(item.Controls[i]);}}private static void GetMainFromSize_Init(Form MyForm)//获取初始化对话框参数{MainDlg_H_Init = MyForm.Height;MainDlg_W_Init = MyForm.Width;}private static void GetMainFromSize_Curr(Form MyForm)//获取当前对话框参数{MainDlg_H_Curr = MyForm.Height;MainDlg_W_Curr = MyForm.Width;}public static void RegisterFormControl(Form MyForm)//注册对话框所以控件{FormControlList(MyForm);GetMainFromSize_Init(MyForm);}public static void ChangeFormControlSize(Form MyForm)//使能AutoSize{GetMainFromSize_Curr(MyForm);Control myQuery;Queue<Control> ControlQuery = new Queue<Control>(MyControlQuery_Init);ControlInfo Node = new ControlInfo();Int32 i = 0;Int32 count = ControlQuery.Count;for (i = 0; i < count; i++){myQuery = ControlQuery.Dequeue();Node = (ControlInfo)MyControlInfoList_Init[i];myQuery.Height = (Int32)(Node.Height * (MainDlg_H_Curr / (double)MainDlg_H_Init));myQuery.Width = (Int32)(Node.Width * (MainDlg_W_Curr / (double)MainDlg_W_Init));myQuery.Location = new Point((Int32)(Node.Loc_X * (MainDlg_W_Curr / (double)MainDlg_W_Init)),(Int32)(Node.Loc_Y * (MainDlg_H_Curr / (double)MainDlg_H_Init)));}}}
}

将"AutoControlSize.RegisterFormControl(this);"添加到Form的构造函数里控件初始化函数中;

AutoControlSize.RegisterFormControl(this);

打开窗体设计器 在 解决方案资源管理器 中,双击打开你的窗体文件(如 Form1.cs),进入 设计视图
找到 Resize 事件
  • 选中控件列表中的 窗体本身(不是里面的按钮或其他控件)。

  • 打开右侧的 属性窗口(按 F4 或右键 → 属性)。

  • 点击顶部的小闪电图标(事件图标),进入事件列表。

  • 找到 Resize 事件,双击它

3. 自动生成事件响应函数
  • Visual Studio 会自动在代码中生成如下函数

http://www.dtcms.com/a/338820.html

相关文章:

  • 知行社黄剑杰:金融跨界,重塑震区救援新章
  • 《基于大数据的全球用水量数据可视化分析系统》用Python+Django开发,为什么导师却推荐用Java+Spring Boot?真相揭秘……
  • sqli-labs通关笔记-第55关 GET数值型注入(括号闭合 限制14次探测机会)
  • 今日行情明日机会——20250819
  • 20.2 QLoRA微调全局参数实战:高点击率配置模板+显存节省50%技巧
  • Linux下Nginx安装及负载均衡配置
  • Python 3.14深度解析:革命性特性与性能优化实践
  • Go高效复用对象:sync.Pool详解
  • Windows内核开发笔记
  • 免费导航规划API接口详解:调用指南与实战示例
  • 一个基于前端技术的小狗寿命阶段计算网站,帮助用户了解狗狗在不同年龄阶段的特点和需求。
  • 数据链路层-网络层-传输层
  • js 值转换boolean方式
  • AutoSAR实战:DCM配置之Response On Event (0x86)事件响应配置指导
  • 【深度学习计算性能】06:多GPU的简洁实现
  • 守护通行安全,“AI+虚拟仿真”领航交通设施人才培育
  • ROS点云可视化工具——Foxglove工具使用
  • Spring Cloud 微服务架构:Eureka 与 ZooKeeper 服务发现原理与实战指南 NO.1
  • 前端如何处理首屏优化问题
  • 微信小程序实现蓝牙开启自动播放BGM
  • 八大排序简介
  • 【集合框架LinkedList底层添加元素机制】
  • el-table 动态列表渲染和动态表格背景设置
  • JavaWeb前端03(Vue用法及具体案例)
  • UniApp 微信小程序之间跳转指南
  • Ubuntu 18.04上安装GCC 9
  • 关于Linux内核中头文件问题相关总结
  • 《Dual Prompt Personalized Federated Learning in Foundation Models》——论文阅读
  • 【使用三化学习早期融合的非标记化架构】
  • ZooKeeper 一致性模型解析:线性一致性与顺序一致性的平衡