Reputation:
There are two monitors and an application (e.g. IE) is currently active/displayed in the 2nd monitor. How do I detect if an application is in 1st or 2nd monitor. I need to know this - to show a userform just on top of the application irrespective of the monitor in which it is. I know what the WindowText (Title) would be (if that helps).
Right now I just show my form near the system tray but would like to show it on top of the application.
// 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);
Upvotes: 2
Views: 787
Reputation: 5566
This was converted from VB but should work.
The Test() method shows how you would use it.
[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;
}
}
}
Edit: More Info
Step 1: Get the window handle Step 2: Get the window rect (location/size) Step 3: Determin which monitor the window resides on
If your looking at overlyaying one window on top of another you don't actually need to know which monitor it is on just the position and size of the window relative to the desktop. In both windows forms and WPF when you set the window location X / Left is the distance in pixels from the left side of the left most monitor. E.g if you have two montior 1024 pixels wide setting X / Left to 2000 will put the window 86 pixels into right hand monitor.
Upvotes: 1
Reputation: 2362
To get the windows position of another process you could use the library mentioned here
How to get and set the window position of another application in C#
You should then be able to check on which screen the position of the rectangle is
private Screen IsVisibleOnScreen(Rectangle rect)
{
foreach (Screen screen in Screen.AllScreens)
{
if (screen.WorkingArea.IntersectsWith(rect))
{
return Screen;
}
}
return null;
}
Upvotes: 0