如何以编程方式切换屏幕后居中表格

P.B*_*key 4 c# winforms

鉴于Form我希望在切换屏幕后将表单置于中心位置.我怎样才能完成任务?

 internal static void SetFormToBiggestMonitor(Form form, bool center = true)
    {
        Screen biggestScreen = FindBiggestMonitor();//assume this works
        form.Location = biggestScreen.Bounds.Location;

        if (center)
        {
            form.StartPosition = FormStartPosition.CenterScreen;
        }
    }
Run Code Online (Sandbox Code Playgroud)

P.B*_*key 7

一种不那么循环的方式来完成任务......

    private static Point CenterForm(Form form, Screen screen)
    {
        return new Point(
                         screen.Bounds.Location.X + (screen.WorkingArea.Width - form.Width) / 2,
                         screen.Bounds.Location.Y + (screen.WorkingArea.Height - form.Height) / 2
                         );
    }
Run Code Online (Sandbox Code Playgroud)