C#应用程序实现多屏显示

devtools/2024/10/16 2:28:20/

前言

随着业务发展,应用程序在一些特定场景下,只在一个显示器上展示信息已经不能满足用户需求。我们如何把主屏运行程序中多个窗体移动到各个扩展屏幕位置显示呢?C# 是通过什么方式来实现的,下面介绍 C# 使用 Screen 类的方式来实现。

详细

Screen 是在 System.Windows.Forms 下的一个类,它表示单个系统上的一个或多个显示设备。

属性
名称描述
AllScreens获取系统上所有显示器
Bounds获取显示的边界
Primary显示是否为显示器
PrimaryScreen获取主显示器
WorkingArea显示器的工作区
方法

   下表是常用的一些方法:

名称描述
FromControl(Control)检索包含指定控件的最大部分的显示器。
GetBounds(Control)检索包含指定控件的最大部分的显示器的边界。
GetWorkingArea(Control)检索包含指定控件的最大区域的显示器工作区。

注意:Screen 只适用于.NET 4.8.1 以下或.NET 7 以上的Windows 桌面应用程序。

示例

 本示例在第二个显示屏同步显示主屏扫描产品后显该产品的图片,方便操作人员更清晰的核对产品信息。示例也用多了C#事件知识点。

参数用于传递信息

using System;using System.Drawing;
namespace Fountain.WinForm.MultiMonitor{    /// <summary>    /// 事件参数    /// </summary>    public class SyncEventArg : EventArgs    {        /// <summary>        /// 产品编码        /// </summary>        public string ProductNo { get; set; }        /// <summary>        /// 产品图片        /// </summary>        public Image ProductImage { get; set; }    }}

辅助显示界面:

using System;using System.Windows.Forms;
namespace Fountain.WinForm.MultiMonitor{    public partial class SecondForm : Form    {        public SecondForm()        {            InitializeComponent();        }        /// <summary>        /// 响应事件处理        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        internal void SysncTextChaned(object sender, EventArgs e)        {            try            {                //取到主窗体的传来的文本                SyncEventArg arg = e as SyncEventArg;                this.LabelTrackingNo.Text = arg.ProductNo;                this.PictureBoxProduct.Image = arg.ProductImage;            }            catch            {            }        }    }}

程序主界面

using System;using System.Drawing;using System.Windows.Forms;
namespace Fountain.WinForm.MultiMonitor{    public partial class MainForm : Form    {        //使用默认的事件处理委托,定义消息发布者事件        public event EventHandler SendMsgEvent;        /// <summary>        ///         /// </summary>        public MainForm()        {            InitializeComponent();        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void MainForm_Load(object sender, EventArgs e)        {            try            {                SecondForm secondForm = new SecondForm();                // 事件的订阅                 SendMsgEvent += secondForm.SysncTextChaned;
                // 获取系统上所有显示器                Screen[] screens = Screen.AllScreens;                // 判断多屏                if (screens.Length >1 )                 {                    // 获取第二个屏幕                    Screen screen = screens[1];                    secondForm.StartPosition = FormStartPosition.Manual;                    // 在 第二个屏幕 显示第二个窗体                    secondForm.Location = screen.Bounds.Location;                }                // 显示辅助界面                secondForm.Show();            }            catch             {            }           }        /// <summary>        /// 文本输入框回车事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void TextBoxProductNo_KeyDown(object sender, KeyEventArgs e)        {            try            {                if (e.KeyCode != Keys.Enter)                {                    return;                }                if (string.IsNullOrEmpty(this.TextBoxProductNo.Text))                {                    return;                }                Image image = Image.FromFile("P000001.png");                // 事件触发通知                SendMsgEvent(this, new SyncEventArg() { ProductNo = this.TextBoxProductNo.Text, ProductImage = image });            }            catch(Exception ex)            {                MessageBox.Show(ex.Message);            }        }    }}

小结

以上是使用C#中Screen类来实现桌面应用程序不同界面在多个显示器上展示信息。希望通过本文的简单案例能扩展大家思路。

C#应用程序实现多屏显示

C#应用程序实现多屏显示 (qq.com)icon-default.png?t=N7T8https://mp.weixin.qq.com/s?__biz=MzkzOTY0MTcyOA==&mid=2247483981&idx=1&sn=3bb547a47583bee0d0420e539c348746&chksm=c2ec9ca2f59b15b4023a3051febd41b1c768ea5b5059170a4d5db6f15f19816e79cd41485221&mpshare=1&scene=1&srcid=0419UmENkkKirdKfQOh18XL7&sharer_shareinfo=3a0e362623f889eb557575df8d44adb2&sharer_shareinfo_first=56afdc0edcb7c9b4f964803b58a44e49#rd特此记录

anlog

2024年4月24日


http://www.ppmy.cn/devtools/26761.html

相关文章

第十五届蓝桥杯总结

因为本人不是计院的&#xff0c;以后可能也不会打算法类的竞赛了&#xff0c;故作此总结&#xff0c;纪念我四个月的算法学习经历&#xff0c;还算是对算法有了一定的基础&#xff0c;碰运气拿下了湖北b组省二&#xff0c;个人感觉比赛题目没有第十四届难&#xff0c;感觉就是纯…

Java基础_集合类_List

List Collection、List接口1、继承结构2、方法 Collection实现类1、继承结构2、相关类&#xff08;1&#xff09;AbstractCollection&#xff08;2&#xff09;AbstractListAbstractSequentialList&#xff08;子类&#xff09; 其它接口RandomAccess【java.util】Cloneable【j…

OpenCV如何使用分水岭算法进行图像分割

OpenCV 使用分水岭算法进行图像分割的基本步骤如下&#xff1a; 加载图像&#xff1a;首先&#xff0c;你需要加载你要进行分割的图像。灰度化&#xff1a;将彩色图像转换为灰度图像&#xff0c;因为分水岭算法通常在灰度图像上操作。预处理&#xff1a;这一步可能包括滤波&am…

xLua详解

目录 环境准备xLua导入 C#调用LuaLua解析器Lua文件加载重定向Lua解析管理器全局变量的获取全局函数的获取List和Dictionary映射table类映射table接口映射tableLuaTable映射table Lua调用C#准备工作Lua使用C#类Lua调用C#枚举Lua使用C# 数组 List 字典数组List字典 Lua使用C#扩展…

leetcode-合并两个有序链表

目录 题目 图解 方法一 方法二 代码(解析在注释中) 方法一 ​编辑方法二 题目 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1&#xff1a; 输入&#xff1a;l1 [1,2,4], l2 [1,3,4] 输出&#xff1a;[1,1…

VS2022 .Net6.0 无法打开窗体设计器

拿Vs2022 建了个Demo&#xff0c;运行环境是net6.0-windows&#xff0c;无论双击或是右键都打不开窗体设计器 打开项目目录下的*.csproj.user <?xml version"1.0" encoding"utf-8"?> <Project ToolsVersion"Current" xmlns"htt…

华为5700配置

恢复出厂设置&#xff0c;清空配置 1、更改名字 system-view sysname tp-10-50-01-04 2、配置管理接口 int vlan 1 ip add 10.50.1.4 255.255.254.0 quit 2、链路汇聚 interface eth-trunk 1 mode lacp quit 3、绑定端口 interface eth-trunk 1 trunkport gigabitethernet …

网络培训议题@2

目录 1. 如何通过IP和掩码确定网关&#xff1a;2. 网络路由的意义和配置方法&#xff1a;3. 网络Bond模式的区别和配置场景&#xff1a;4. 堆叠、VLAN、Trunk、聚合的意义&#xff1a;5. 虚拟机环境下VIP配置和常见问题&#xff1a;6. VXLAN拓展&#xff1a; 1. 如何通过IP和掩…