c# Winform 開發分屏顯示應用程序

分屏顯示便可把一臺主機內運行的多個程序分別顯示在不一樣的兩個(或多個)屏幕上。目前市面上主流的顯卡都支持分屏顯示(顯示雙屏幕),若是須要顯示2個以上的屏幕,則應使用「拖機卡」類的硬件。html

 

設置分屏顯示的兩種方法以下:windows

一、用兩個顯卡鏈接兩臺顯示器,進入系統後,分清楚哪個是主顯卡,在桌面空白處右鍵單擊,點屬性,而後在窗口中點「設置」選項卡,會看到有兩個顯示,分別是1(主顯卡)和2(副顯卡),點擊那個2,在下面的「將windows桌面擴展到該監視器」打上對號,肯定後,你試着把鼠標往主顯示器右邊界移動,再移動,鼠標會跑到第二臺顯示器上去了,這樣,一樣運行幾個程序,分別將它們的窗口拖拽到兩個顯示器的區域中就能夠了,這其實是將桌面擴展了一下。app

二、使用專門的硬件。可使用「一拖多」的拖機卡,只要將設備插入usb口中,將設備上引出的兩個ps/2口分別接鼠標和鍵盤,主機中仍是有兩塊顯卡,而後再裝上這個設備的專用軟件,重啓後,通過簡單的配置,便可實現「徹底」獨立的兩個系統。ide

 

所謂的分屏或多屏軟件,就是把軟件中的多個窗體,在主屏幕運行,可是把各個窗體(座標)移動到各個擴展屏幕位置上以下圖所示:函數

 

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

 

 

如下介紹最經常使用的雙屏幕顯示,也就是左右模式的屏幕顯示的方法。oop

WinForm 的實現辦法:post

利用WinForm中的Screen類,便可比較方便地實現多窗體分別在多個屏幕上顯示。ui

  • 獲取當前系統鏈接的屏幕數量: 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 plain copy
  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.htmlthis

================================================================spa

經過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
相關文章
相關標籤/搜索