是否有在屏幕中央打开包含鼠标光标的窗口的正确方法?

问题描述

我已经看到很多解决这个问题的好方法,但没有一个适合我的所有情况。

我最大的问题是窗口的 WindowStartupLocation 属性无法正常工作 (.NET 4.7.2)。在枚举的描述中,它说 CenterScreen 的值在包含 鼠标光标 的屏幕中心打开窗口。

就我而言,它似乎是在屏幕中央打开,其中包含我尝试打开的窗口的所有者窗口

我检查了 Caliburn Micro 的 WindowManager 做了什么,如果我没有指定所有者,它确实将窗口的所有者设置为应用程序的主窗口。这个主窗口根据启动位置描述在正确的屏幕上打开,但我想在任何地方打开消息框和弹出窗口,而不仅仅是在第一个屏幕上。

如果我将所有者显式设置为 null,则在与不同 DPI 监视器相关的许多情况下,它不再居中。

我还尝试将不可见的应用程序主窗口移动到光标位置,这部分解决了我的开发机器上的问题,表明 CenterScreen 确实转到了屏幕所在的位置所有者在,而不是鼠标光标所在的位置。但在测试者的机器上,它根本没有集中在 3 个屏幕中的最后 2 个(1 台笔记本电脑 125% 和 2 台显示器 100%)。

我还能尝试什么?也许与所有者关系有关,也许我在 Caliburn WindowManager 中遗漏了一些东西?

解决方法

帮助窗口呢?在找不到任何其他解决方案的情况下,我自己使用它。此外,它证明还有其他好处,我记得我还不得不使用它的 onactivate / deactitave 事件来获得一些有用的东西。

这是我的 init,连同 System.Windows.Forms.Screen.AllScreens > screen.getworkingarea > 它应该可以解决问题 :

                helperW.ShowInTaskbar = false;
                helperW.Top = -10000000; // Location of new window is outside of visible part of screen
                helperW.Left = -10000000;
                helperW.Width = 1; // size of window is enough small to avoid its appearance at the beginning
                helperW.Height = 1;
                helperW.WindowStyle = WindowStyle.ToolWindow; // Set window style as ToolWindow to avoid its icon in AltTab 
                helperW.Show(); // We need to show window before set is as owner to our main window
                this.Owner = helperW; 

连同类似的东西:

    internal ScreenInfo GetScreenUnderCursor()
    {

        foreach (var screen in WorkingAreas)
        {
            System.Windows.Point pos = AddonInterface.Peripherals.Mouse.GetMousePosition();

            var b = screen.Bounds;
            if (b.X < pos.X && b.X + b.Width > pos.X &&
                b.Y < pos.Y && pos.Y < b.Y + b.Height)
            {
                return screen; 
            }
        }
        return null; //not possible???>>>??!/ my screens are more limited than what c# thinks
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...