双显示器屏幕中央显示表格如何?

时间:2015-07-15 08:11:24

标签: c# .net winforms multiple-monitors

我正在开发一个Windows桌面应用程序双显示器,我需要在主屏幕上有时显示我的表单,有时在辅助显示器上 哪个工作正常,但当我在我的辅助屏幕上显示它时,我希望它显示在我的屏幕中心不能正常工作。

这是我的代码:

if (Screen.AllScreens.Length > 1)
myForm.Location = Screen.AllScreens[1].WorkingArea.Location;
myForm.StartPosition = FormStartPosition.Manual; // because i wrote manual it is displayed on Top left of my secondaryScreen which is ok
myForm.show();

但是我想把它放在中心,所以我写了

myForm.StartPosition = FormStartPosition.CentreScreen;
//it is not working again a form is displayed on Centre of PrimaryScreen..

知道为什么吗?

3 个答案:

答案 0 :(得分:0)

查找名为StartPosition的winform的属性,然后将其设置为Center Screen

答案 1 :(得分:0)

你可以写一个extension method

{{1}}

答案 2 :(得分:0)

    private void CenterOnTheCurrentScreen()
    {
        Rectangle workingArea = Screen.FromControl(this).WorkingArea;
        Point center = new Point((workingArea.Width - this.Width) / 2, (workingArea.Height - this.Height) / 2);

        this.Location = new Point(workingArea.X + center.X, workingArea.Y + center.Y);
    }