How to get window's position?
41,891
Solution 1
Try this:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
public struct Rect {
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
Process[] processes = Process.GetProcessesByName("notepad");
Process lol = processes[0];
IntPtr ptr = lol.MainWindowHandle;
Rect NotepadRect = new Rect();
GetWindowRect(ptr, ref NotepadRect);
Solution 2
using System.Runtime.InteropServices;
using System.Diagnostics;
public class GetNotePadLocation
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
public struct Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
public static void NotePadLocation()
{
Process[] processes = Process.GetProcessesByName("notepad");
Process lol = processes[0];
IntPtr ptr = lol.MainWindowHandle;
Rect NotepadRect = new Rect();
GetWindowRect(ptr, ref NotepadRect);
}
}
Related videos on Youtube
Comments
-
Patryk about 4 years
I'd like to know the way of getting process'es window position. I've been looking for that on the internet but with no results. Thanks :)
Process[] processes = Process.GetProcessesByName("notepad"); Process lol = processes[0]; IntPtr p = lol.MainWindowHandle;
-
Micah Armantrout about 12 years
-
-
Lost_In_Library about 12 yearsIt will work. Ok. But this code will not work when notepad has been minized to taskbar. You should check that. ( Also, I suggest to use try-catch to avoid "notepad is NOT running" error )
-
hazem over 10 yearsI think I have found the missing part and there where order error in the struct part; I have fix it... but I actually don't know how to get these property for all the opened notepad windows and how to get the dimension of the active one thanks for the help...
-
Nick over 10 yearsthe Rect member var has wrong order. It should be Left, Top, Right Bottom. Otherwise, you will get incorrect values.
-
SimpleVar almost 8 years100% sure
lol
variable once stood for league of legends window :P -
rayzinnz over 7 yearsWhy have you got the FindWindow function included?
-
shruti singh over 5 yearsIs there a specific reason why it does not work for Microsoft Sticky Notes? I'm using Windows 10. On doing
GetWindowRect
for sticky notes, I am getting a rectangle with X=Y=0, and Width = Height = 1. -
Cloud Cho about 5 yearsto find the active window, have you tried
[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
and call the function byIntPtr handle; handle = GetForegroundWindow();
? For the all opened windows, please tryhandle = FindWindowA(null, null);