c# winfrom 子窗體分屏顯示


參考博客:https://blog.csdn.net/kailan818/article/details/8517126

實現代碼:

private void button1_Click(object sender, EventArgs e)
        {
            var frmChild = Application.OpenForms["frmChild"];
            if (frmChild != null)
            {
                frmChild.Activate();
            }
            else
            {
                frmChild frm = new frmChild();
                frm.Owner = this;//申明當前窗體是子窗體
                ShowOnMonitor(frm);
                frm.Show();
            }
        }

        private void ShowOnMonitor(frmChild frm)
        {
            Screen[] sc = Screen.AllScreens;
            if (sc.Length > 1)
            {
                //獲取當前屏幕
                Screen CurrentScreen = Screen.FromControl(this);
                //獲取當前鼠標所在的屏幕
                //Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));
                var child = sc.Where(it => it.DeviceName != CurrentScreen.DeviceName).FirstOrDefault();
                frm.StartPosition = FormStartPosition.Manual;
                frm.Location = new Point(child.Bounds.Left, child.Bounds.Top);

            }
            // If you intend the form to be maximized, change it to normal then maximized.  
            frm.WindowState = FormWindowState.Normal;
            frm.WindowState = FormWindowState.Maximized;
        }

demo地址:https://gitee.com/cainiaoA/winformSplit


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM