您的位置:首页 > 编程语言 > C#

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

2017-01-07 18:56 351 查看


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个屏幕上显示:

     this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);

     this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);

 

把任何窗体显示在任何屏幕的方法:

 

//在窗体的OnLoad事件中调用该方法  

protected void Form1_OnLoad(...) {  

    showOnMonitor(1);//index=1  

}  

  

private void showOnMonitor(int showOnMonitor)   

{   

    Screen[] sc;   

    sc = Screen.AllScreens;   

    if (showOnMonitor >= sc.Length) {  

        showOnMonitor = 0;  

    }  

  

  

    this.StartPosition = FormStartPosition.Manual;   

    this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);  

    // If you intend the form to be maximized, change it to normal then maximized.  

    this.WindowState = FormWindowState.Normal;  

    this.WindowState = FormWindowState.Maximized;  

}  

对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;  

  

  

        }  

(二)

双屏显示1

// 利用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个屏幕上显示:

        //this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);

        //this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);

        private void showOnMonitor(int showOnMonitor)

        {

            Screen[] sc;

            sc = Screen.AllScreens;

            if (showOnMonitor >= sc.Length)

            {

                showOnMonitor = 0;

            }

            this.FormBorderStyle = FormBorderStyle.None; //无边框全屏显示

            this.StartPosition = FormStartPosition.Manual;

            this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);  

            //this.Location = new Point(((sc[showOnMonitor].Bounds.Width-this.Width)/2), ((sc[showOnMonitor].Bounds.Height-this.Height)/2));

            // If you intend the form to be maximized, change it to normal then maximized.               

            //this.WindowState = FormWindowState.Normal;

            this.WindowState = FormWindowState.Maximized;//最大化窗口

        }

如果接双显卡时showOnMonitor 参数等于0为主屏,1为扩展屏

双屏显示2

 private void showOnMonitor2()

        {

            Screen[] sc;

            sc = Screen.AllScreens;

            //get all the screen width and heights 

            Form1 f = new Form1();

            f.FormBorderStyle = FormBorderStyle.None;

            f.Left = sc[1].Bounds.Width;

            f.Top = sc[1].Bounds.Height;

            f.StartPosition = FormStartPosition.Manual;

            f.Location = sc[1].Bounds.Location;

            Point p = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);

            f.Location = p;

            f.WindowState = FormWindowState.Maximized;

            f.Show();

        }

接入双显卡时sc[0]为主屏、sc[1]扩展屏

一个窗体双屏显示

  this.Location = new Point(0,0);            

            Screen[] sc;

            sc = Screen.AllScreens;

            this.Width = (sc[0].Bounds.Width + sc[1].Bounds.Width);//+20;

            this.Height = (sc[0].Bounds.Height); //+200;

            this.FormBorderStyle = FormBorderStyle.None; //边框样式

            webBrowser1.Width = sc[0].Bounds.Width;

            webBrowser1.Height = sc[0].Bounds.Height;

            webBrowser1.Location = new Point(sc[0].Bounds.Location.X, sc[0].Bounds.Location.Y);            

            webBrowser1.Url = new Uri("http://www.google.com.hk");

            webBrowser2.Width = sc[1].Bounds.Width;

            webBrowser2.Height = sc[1].Bounds.Height;

            webBrowser2.Location = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);

            webBrowser2.Url = new Uri("http://www.baidu.com");

(三)


C# winform 双屏显示

双屏显示1

// 利用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个屏幕上显示:

        //this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2);

        //this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2);

        private void showOnMonitor(int showOnMonitor)

        {

            Screen[] sc;

            sc = Screen.AllScreens;

            if (showOnMonitor >= sc.Length)

            {

                showOnMonitor = 0;

            }

            this.FormBorderStyle = FormBorderStyle.None; //无边框全屏显示

            this.StartPosition = FormStartPosition.Manual;

            this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);  

            //this.Location = new Point(((sc[showOnMonitor].Bounds.Width-this.Width)/2), ((sc[showOnMonitor].Bounds.Height-this.Height)/2));

            // If you intend the form to be maximized, change it to normal then maximized.               

            //this.WindowState = FormWindowState.Normal;

            this.WindowState = FormWindowState.Maximized;//最大化窗口

        }

如果接双显卡时showOnMonitor 参数等于0为主屏,1为扩展屏

双屏显示2

 private void showOnMonitor2()

        {

            Screen[] sc;

            sc = Screen.AllScreens;

            //get all the screen width and heights 

            Form1 f = new Form1();

            f.FormBorderStyle = FormBorderStyle.None;

            f.Left = sc[1].Bounds.Width;

            f.Top = sc[1].Bounds.Height;

            f.StartPosition = FormStartPosition.Manual;

            f.Location = sc[1].Bounds.Location;

            Point p = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);

            f.Location = p;

            f.WindowState = FormWindowState.Maximized;

            f.Show();

        }

接入双显卡时sc[0]为主屏、sc[1]扩展屏

双屏显示2

 private void showOnMonitor2()

        {

            Screen[] sc;

            sc = Screen.AllScreens;

            //get all the screen width and heights 

            Form1 f = new Form1();

            f.FormBorderStyle = FormBorderStyle.None;

            f.Left = sc[1].Bounds.Width;

            f.Top = sc[1].Bounds.Height;

            f.StartPosition = FormStartPosition.Manual;

            f.Location = sc[1].Bounds.Location;

            Point p = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);

            f.Location = p;

            f.WindowState = FormWindowState.Maximized;

            f.Show();

        }

接入双显卡时sc[0]为主屏、sc[1]扩展屏

一个窗体双屏显示

  this.Location = new Point(0,0);            

            Screen[] sc;

            sc = Screen.AllScreens;

            this.Width = (sc[0].Bounds.Width + sc[1].Bounds.Width);//+20;

            this.Height = (sc[0].Bounds.Height); //+200;

            this.FormBorderStyle = FormBorderStyle.None; //边框样式

            webBrowser1.Width = sc[0].Bounds.Width;

            webBrowser1.Height = sc[0].Bounds.Height;

            webBrowser1.Location = new Point(sc[0].Bounds.Location.X, sc[0].Bounds.Location.Y);            

            webBrowser1.Url = new Uri("http://www.google.com.hk");

            webBrowser2.Width = sc[1].Bounds.Width;

            webBrowser2.Height = sc[1].Bounds.Height;

            webBrowser2.Location = new Point(sc[1].Bounds.Location.X, sc[1].Bounds.Location.Y);

            webBrowser2.Url = new Uri("http://www.baidu.com");

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#分屏 多屏