在辅助监视器中创建 WPF 窗口时遇到问题 [英] Trouble creating a WPF window in a secondary monitor

查看:23
本文介绍了在辅助监视器中创建 WPF 窗口时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在应用中实现一些类似 Chrome 的标签功能,但在正确生成新实例时遇到了一些问题.我已经对各种解决方案进行了大量搜索和迭代,但还没有能够在第二台显示器上生成一个新窗口.

I'm working on implementing some Chrome-like tab functionality in an app and am having some trouble getting the new instance to spawn correctly. I've done quite a bit of searching and iterating over various solutions but have not yet been able to make a new window spawn on the second monitor.

这是使用线程:

  1. 打开文件
  2. 将当前标签拖到其他显示器
  3. 新应用实例会在其中设置该选项卡的情况下生成,位于用户将选项卡拖到的位置.

断开连接在第 3 步.新实例始终在主监视器上生成.

The disconnect is in step 3. The new instance is always spawned on the Primary monitor.

所以,一些代码来扩展这个问题.

So, some code to expand upon the problem.

namespace app {
    public class AppView {
      public void OpenInNewWindow()
      {
        // Create a new viewmodel
        var appViewModel = new AppVM();

        //// On my machine this returns the correct screen "DISPLAY2".  The Top and Left properties are 0 and 1680, respectively.
        var targetScreen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);

        ////So we can set the position of the new view
        var appView = new AppView(appViewModel);

        //This seats the currently selected data tab inside the new AppViewModel
        RelocateSelectedViewModel(appViewModel);

        appView.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
        appView.Top = targetScreen.WorkingArea.Top;
        appView.Left = targetScreen.WorkingArea.Left;
        appView.Show();
        // Have to maximize after we Show() or it won't appera on secondary monitors according to THE INTERNET!
        appView.WindowState = System.Windows.WindowState.Maximized;
        appView.Focus();            
      }
    }
}

我想我应该提到我获得第二个屏幕没有问题.上面代码中的 targetScreen 正确地找到了我想要的屏幕,并且新窗口的 Top 和 Left 值被正确设置,分别为 0 和 1680.只是 AppView.Show() 命令(实际上是 Window.Show())在主屏幕上创建了窗口.

I suppose I should mention that I have no problems getting the second Screen. targetScreen in the code above is correctly finding the screen I want, and the Top and Left values of the new window are getting set correctly, to 0 and 1680, respectively. It's just that the AppView.Show() command (which is actually Window.Show()) creates the window on the primary screen.

我已将相同的代码应用到一个独立的项目中并且它起作用了,这让我相信我的新 appView 和当前在此处覆盖我的集合的 appView 之间存在某种联系.有没有人遇到过这个问题?

I have taken this same code to a standalone project and it has worked, which leads me to believe that there is some kind of tie between my new appView and the current one that is overriding my sets here. Has anyone encountered this problem before?

推荐答案

您是否尝试使用 Winforms Screen.FromControl?请参阅这篇文章.

Did you try using the Winforms Screen.FromControl? See this post.

你可以从这篇文章尝试这个hack:

You can try this hack from this post:

appView.SourceInitialized += (_, __) => appView.WindowState = WindowState.Maximized;
appView.Show();

这篇关于在辅助监视器中创建 WPF 窗口时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆