c#:窗口进入第二个监视器然后跳回主监视器

时间:2015-11-17 04:09:32

标签: c# wpf windows multiple-monitors

下面是简化代码,奇怪的是窗口在一秒钟内停留在第二台显示器上,然后返回第一台主显示器。必须有一个窗口设置或外部程序导致它。因为相同的代码适用于具有双屏幕的另一台计算机。我确定并仔细检查第二个监视器是否在索引1中:showOnMonitor(1,Q);

 Q = new queue(); 
 showOnMonitor(1, Q);
 Q.Show();

public static void showOnMonitor(int monitor, Window w2)
        {
            Screen[] sc;
            sc = Screen.AllScreens;

            if ( monitor >= sc.Length )
            {
                monitor = 0;
            }


            w2.WindowStartupLocation = WindowStartupLocation.Manual;

            var workingArea = sc[monitor].WorkingArea;
            w2.Left = workingArea.Left;
            w2.Top = workingArea.Top;
            w2.Width = workingArea.Width;
            w2.Height = workingArea.Height;

        }

1 个答案:

答案 0 :(得分:0)

好的,我不知道为什么会这样。但这是解决方案。

Q = new queue();
Q.Show();
Thread.Sleep( 100 ); //need to delay first before moving the position
showOnMonitor(1, Q);

public static void showOnMonitor(int monitor, Window w2)
        {
            Screen[] sc;
            sc = Screen.AllScreens;

            if ( monitor >= sc.Length )
            {
                monitor = 0;
            }


            w2.WindowStartupLocation = WindowStartupLocation.Manual;

            var workingArea = sc[monitor].WorkingArea;
            w2.Left = workingArea.Left;
            w2.Top = workingArea.Top;
            w2.Width = workingArea.Width;
            w2.Height = workingArea.Height;

        }