目录
  • 前言
  • 详细
    • 属性
    • 方法
  • 示例
    • 小结

      前言

      随着业务发展,应用程序在一些特定场景下,只在一个显示器上展示信息已经不能满足用户需求。我们如何把主屏运行程序中多个窗体移动到各个扩展屏幕位置显示呢?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类来实现桌面应用程序不同界面在多个显示器上展示信息。希望通过本文的简单案例能扩展大家思路。

      声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。