c# 移动窗体到副屏

//GuestForm 是当前的窗体名称,此名称可根据自己定义的窗体名称修改

public void MoveForm()
        {
         	Screen[] screens = Screen.AllScreens;
            dynamic currentForm = null;
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is GuestForm) currentForm = frm;
            }
            if(currentForm != null && screens.Length > 1)
            {
                var lastScreen = screens.AsQueryable().Last();//获取最后一个屏幕
                int Left = lastScreen.WorkingArea.Location.X;
                int Top = lastScreen.WorkingArea.Location.Y;

                Rectangle ScreenArea = Screen.GetWorkingArea(currentForm);
                currentForm.Left = Left + (ScreenArea.Width / 2) - (currentForm.Width / 2);
                currentForm.Top =  Top + (ScreenArea.Height / 2) - (currentForm.Height / 2);
                //设置最大化无法移动到副屏,这先移动再最大化
                currentForm.WindowState = FormWindowState.Maximized;
            }
        }

或者

public void MoveForm()
        {
        	Screen[] screens = Screen.AllScreens;
       		foreach (Form frm in Application.OpenForms)
            {
                if (frm is GuestForm)
                {
                    if (screens.Length > 1)         //如果有多个显示器,显示在第二个显示器
                    {
                        foreach (Screen screen in screens)
                        {
                            if (!screen.Primary)
                            {
                                frm.Left = screen.WorkingArea.Location.X;      //第二块屏的x坐标
                                frm.Top  = screen.WorkingArea.Location.Y;      //第二块屏的y坐标
                                //Console.WriteLine(frm.Left);                 //打印显示的是主屏的分辨率
                                //break;
                                //移动到副屏幕,居中打开
                                Rectangle ScreenArea = Screen.GetWorkingArea(frm);
                                //Console.WriteLine("X--:"+screen.WorkingArea.Location.X);
                                //Console.WriteLine("Y--:"+screen.WorkingArea.Location.Y);
                                //Console.WriteLine(ScreenArea.Width);
                                //Console.WriteLine(ScreenArea.Height);
                                frm.Left = frm.Left + (ScreenArea.Width / 2) - (frm.Width / 2);
                                frm.Top  = frm.Top + (ScreenArea.Height / 2) - (frm.Height / 2);
                                //loading中设置最大化无法移动到副屏,这先移动再最大化
                                frm.WindowState = FormWindowState.Maximized;
                            }
                        }
                    }
                    else
                    {
                        //如果只有一个显示器                        
                    }
                    break;
                }
            }
        }

最简洁一个

public void MoveForm()
        {
            Screen[] screens = Screen.AllScreens;
            int count = screens.Count();
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is GuestForm) 
                {
                    frm.StartPosition = FormStartPosition.Manual;
                    frm.Location = new Point(screens[count-1].Bounds.Left, screens[count-1].Bounds.Top);
                    this.WindowState = FormWindowState.Maximized;
                }
            }
        }

你可能感兴趣的:(c#,开发语言)