c# Winform 开发分屏显示应用程序

分屏显示即可把一台主机内运行的多个程序分别显示在不同的两个(或多个)屏幕上。目前市面上主流的显卡都支持分屏显示(显示双屏幕),如果需要显示2个以上的屏幕,则应使用“拖机卡”类的硬件。

 

设置分屏显示的两种方法如下:

1、用两个显卡连接两台显示器,进入系统后,分清楚哪一个是主显卡,在桌面空白处右键单击,点属性,然后在窗口中点“设置”选项卡,会看到有两个显示,分别是1(主显卡)和2(副显卡),点击那个2,在下面的“将windows桌面扩展到该监视器”打上对号,确定后,你试着把鼠标往主显示器右边界移动,再移动,鼠标会跑到第二台显示器上去了,这样,同样运行几个程序,分别将它们的窗口拖拽到两个显示器的区域中就可以了,这实际上是将桌面扩展了一下。

2、使用专门的硬件。可以使用“一拖多”的拖机卡,只要将设备插入usb口中,将设备上引出的两个ps/2口分别接鼠标和键盘,主机中还是有两块显卡,然后再装上这个设备的专用软件,重启后,经过简单的配置,即可实现“完全”独立的两个系统。

 

所谓的分屏或多屏软件,就是把软件中的多个窗体,在主屏幕运行,但是把各个窗体(坐标)移动到各个扩展屏幕位置上如下图所示:

 

主屏幕
(MainForm)
index=0
扩展屏幕1
(Form1)
index=1
扩展屏幕2
(Form2)
index=...
扩展屏幕3
(Form3)
index=...

 

 

以下介绍最常用的双屏幕显示,也就是左右模式的屏幕显示的方法。

WinForm 的实现办法:

利用WinForm中的Screen类,即可比较方便地实现多窗体分别在多个屏幕上显示。

  • 获取当前系统连接的屏幕数量: Screen.AllScreens.Count();
  • 获取当前屏幕的名称:string CurrentScreenName = Screen.FromControl(this).DeviceName;
  • 获取当前屏幕对象:Screen CurrentScreen = Screen.FromControl(this);
  • 获取当前鼠标所在的屏幕:Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));

让窗体在第2个屏幕上居中显示:

在窗体的OnLoad事件中,插入如下代码:
     this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);
     this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);
 
把任何窗体显示在任何屏幕的方法:
[csharp] view plaincopy
  1. //在窗体的OnLoad事件中调用该方法  
  2. protected void Form1_OnLoad(...) {  
  3.     showOnMonitor(1);//index=1  
  4. }  
  5.   
  6. private void showOnMonitor(int showOnMonitor)   
  7. {   
  8.     Screen[] sc;   
  9.     sc = Screen.AllScreens;   
  10.     if (showOnMonitor >= sc.Length) {  
  11.         showOnMonitor = 0;  
  12.     }  
  13.   
  14.   
  15.     this.StartPosition = FormStartPosition.Manual;   // 窗体的位置由 System.Windows.Forms.Control.Location 属性指定
  16.     this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);  
  17.     // If you intend the form to be maximized, change it to normal then maximized.  
  18.     this.WindowState = FormWindowState.Normal;  //默认大小的窗口
  19.     this.WindowState = FormWindowState.Maximized;  //最大化的窗口
  20. }  

对WPF窗体来说,只要简单的更改即可:
首先要添加对 System.Windows.Forms 和 System.Drawing 的引用
简单的参考代码如下:
        protected override void OnStartup(StartupEventArgs e)  
        {  
            base.OnStartup(e);  
  
            Window1 w1 = new Window1();  
            Window2 w2 = new Window2();  
  
            Screen s1 = Screen.AllScreens[0];  
            Screen s2 = Screen.AllScreens[1];  
  
            Rectangle r1 = s1.WorkingArea;  
            Rectangle r2 = s2.WorkingArea;  
  
            w1.Top = r1.Top;  
            w1.Left = r1.Left;  
  
            w2.Top = r2.Top;  
            w2.Left = r2.Left;  
  
            w1.Show();  
            w2.Show();  
  
            w2.Owner = w1;  
  
        } 
注意:一定应该在窗体加载前,判断所要显示的屏幕是否存在,否则会报错!

 

出处:https://www.cnblogs.com/lizi/archive/2012/02/21/2361229.html

================================================================

通过C#判断Screen.AllScreens的数量,然后确认有副屏幕。
把界面显示在副屏,再通过事件发布订阅来传递信息彼此之间的信息。

//smartPartInfo是窗口类的实例
smartPartInfo.StartPosition = FormStartPosition.Manual;
smartPartInfo.Location = new Point(screens[1].Bounds.Left, screens[1].Bounds.Top);
smartPartInfo.Width = screens[1].Bounds.Width;
smartPartInfo.Height = screens[1].Bounds.Height;

 

事件发布与订阅可参考:C#中的事件的订阅与发布 

A窗口的计算结果,给到B窗口:

a中定义事件,b中实现事件并后续继续处理a的计算结果

在a中绑定a的事件和b的事件处理函数,需要b的事件处理函数public访问修饰

在b中绑定a的事件和b的事件处理函数,需要a的事定义为public访问修饰

具体如何绑定,可根据业务需要,自行决定

 

出处:https://bbs.csdn.net/topics/392194646

================================================================

自己写的

    public partial class FromChild : Form
    {
        private int showAtScreenNum = 0;
        private bool isShowVideo = false;

        public FromChild()
        {
            InitializeComponent();

            Load += FromChild_Load;
            this.FormClosing += FromChild_FormClosing;
        }

        private void FromChild_FormClosing(object sender, FormClosingEventArgs e)
        {
            isShowVideo = false;
        }

        private void FromChild_Load(object sender, EventArgs e)
        {
            label1.Parent = pictureBox1;
            label1.BackColor = Color.Transparent;

            if (Screen.AllScreens.Count() != 1)
            {
                SetChildScreen();
            }
            isShowVideo = true;
        }

        private void SetChildScreen()
        {
            showAtScreenNum = 1;
            var screens = Screen.AllScreens;
            this.Left = screens[1].Bounds.Left;
            this.Top = screens[1].Bounds.Top;
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(screens[1].Bounds.Left, screens[1].Bounds.Top);
            this.Width = screens[1].Bounds.Width;
            this.Height = screens[1].Bounds.Height;
            //this.Size = new System.Drawing.Size(screens[1].WorkingArea.Width, screens[1].WorkingArea.Height);
            //this.Size = new System.Drawing.Size(screens[1].Bounds.Width + 20, screens[1].Bounds.Height + 40);
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        }


        /// <summary>
        /// 接收视频帧数据
        /// </summary>
        /// <param name="img"></param>
        public void OnShowNewFrame(byte[] img)
        {
            if (isShowVideo)
                ShowImage(img);
        }

        private void ShowImage(byte[] img)
        {
            if (pictureBox1.InvokeRequired)
            {
                this.BeginInvoke(new Action(() => ShowImage(img)));
            }
            else
            {
                pictureBox1.Image?.Dispose();
                pictureBox1.Image = Image.FromStream(new MemoryStream(img));
            }
        }


        const int WM_SYSCOMMAND = 0x112;
        const int SC_MAXIMIZE = 0xF030;
        const int SC_MAXIMIZE_DOUBLECLICK = 0xF012;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                if (m.WParam.ToInt32() == SC_MAXIMIZE && Screen.AllScreens.Count() != 1)
                    SetChildScreen();
            }
            base.WndProc(ref m);
        }

        private void FromChild_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape && showAtScreenNum == 1)
            {
                showAtScreenNum = 0;
                this.Left = 0;
                this.Top = 0;
                this.Size = new System.Drawing.Size(640, 480);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                this.WindowState = FormWindowState.Normal;
            }
        }
    }
View Code

 

posted on 2019-10-12 12:46  jack_Meng  阅读(2381)  评论(0编辑  收藏  举报

导航