前言
随着业务发展,应用程序在一些特定场景下,只在一个显示器上展示信息已经不能满足用户需求。我们如何把主屏运行程序中多个窗体移动到各个扩展屏幕位置显示呢?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)
https://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日



















