Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:哪个监视器中的应用程序?_C#_.net - Fatal编程技术网

C#:哪个监视器中的应用程序?

C#:哪个监视器中的应用程序?,c#,.net,C#,.net,有两个监视器,一个应用程序(如IE)当前处于活动状态/显示在第二个监视器中。如何检测应用程序是否位于第一个或第二个监视器中。 我需要知道这一点——在应用程序顶部显示一个userform,而不管它在哪个监视器中。我知道WindowText(标题)是什么(如果有帮助的话) 现在,我只是在系统托盘附近显示我的表单,但希望将其显示在应用程序顶部 // FORM POSITION this.StartPosition = FormStartPosition.Manual; Rectangle workAr

有两个监视器,一个应用程序(如IE)当前处于活动状态/显示在第二个监视器中。如何检测应用程序是否位于第一个或第二个监视器中。 我需要知道这一点——在应用程序顶部显示一个userform,而不管它在哪个监视器中。我知道WindowText(标题)是什么(如果有帮助的话)

现在,我只是在系统托盘附近显示我的表单,但希望将其显示在应用程序顶部

// FORM POSITION
this.StartPosition = FormStartPosition.Manual;
Rectangle workArea = Screen.PrimaryScreen.WorkingArea;
int left = workArea.Width - this.Width;
int top = workArea.Height - this.Height;
this.Location = new Point(left, top);

要获取另一个进程的windows位置,可以使用此处提到的库

然后,您应该能够检查矩形的位置在哪个屏幕上

private Screen IsVisibleOnScreen(Rectangle rect)
{
    foreach (Screen screen in Screen.AllScreens)
    {
        if (screen.WorkingArea.IntersectsWith(rect))
        {
            return Screen;
        }
    }

    return null;
}

这是从VB转换而来的,但应该可以工作

Test()方法显示了如何使用它

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

public static RECT GetWindowLocationByName(string name)
{
    IntPtr handle = FindWindow(default(string), name);
    RECT result = default(RECT);
    GetWindowRect(handle, ref result);
    return result;
}

public static void Test()
{
    dynamic location = GetWindowLocationByName("Untitled - Notepad");
    Screen result = null;

    foreach (Screen s in Screen.AllScreens) {
        if (s.WorkingArea.IntersectsWith(new Rectangle(location.Left, location.Top, location.Right - location.Left, location.Bottom - location.Top))) {
            result = s;
        }
    }
}
编辑:更多信息

步骤1:获取窗口句柄 步骤2:获取窗口矩形(位置/大小) 步骤3:确定窗口所在的监视器


如果您看到一个窗口重叠在另一个窗口上,您实际上不需要知道它位于哪个监视器上,只需要知道窗口相对于桌面的位置和大小。在windows窗体和WPF中,当您设置窗口位置时,X/Left是距离最左侧监视器的距离(以像素为单位)。例如,如果您有两个月的1024像素宽,将X/Left设置为2000将使窗口86像素进入右侧显示器。

您不能使用要在其上显示的窗体作为指南吗?@LasseV.Karlsen:我没有收到您的要求。你想让我显示整个代码吗?我可以改变int-top=0;在上面的代码中,部分解决了我的问题,但不是在两个监视器中。我同意,如果可能的话,您应该在没有任何其他信息的情况下使用目标窗口。如果这不起作用,您可以将
顶部
左侧
坐标与
System.Windows.Forms.Screen.AllScreens[]
进行比较。如果他已将表单最大化,则表单边界在屏幕区域之外。此外,表单可以在两个或多个屏幕上显示。谢谢!。注意:Constants.vbNullString不适用于我,所以我将其更改为默认值(string)。哦,对不起,这是vb的问题。我把整个过程都通过了转换器。