c#:窗口转到第二个监视器,然后跳回到主监视器

本文关键字:监视器 然后 第二个 窗口 | 更新日期: 2023-09-27 18:22:07

Hi下面是简化的代码,奇怪的是,窗口在几秒钟内停留在第二个监视器上,然后返回到第一个主监视器。必须有一个窗口设置或外部程序导致它。因为相同的代码在另一台双屏计算机中工作。我确信并仔细检查了第二个监视器是否在索引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;
        }

c#:窗口转到第二个监视器,然后跳回到主监视器

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

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;
        }