10

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;
Patryk
  • 3,042
  • 11
  • 41
  • 83

2 Answers2

18

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);
cdiggins
  • 17,602
  • 7
  • 105
  • 102
ionden
  • 12,536
  • 1
  • 45
  • 37
  • 1
    It 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 ) – Lost_In_Library Apr 25 '12 at 07:24
  • 7
    the Rect member var has wrong order. It should be Left, Top, Right Bottom. Otherwise, you will get incorrect values. – Nick Nov 06 '13 at 04:06
  • 1
    100% sure `lol` variable once stood for league of legends window :P – SimpleVar Jul 21 '16 at 01:55
  • 5
    Why have you got the FindWindow function included? – rayzinnz Sep 20 '16 at 22:51
  • Is 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. – shruti singh Jan 03 '19 at 06:51
3
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);
    }
}
hazem
  • 39
  • 2
  • I 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... – hazem Sep 07 '13 at 12:06
  • to find the active window, have you tried `[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();` and call the function by `IntPtr handle; handle = GetForegroundWindow();`? For the all opened windows, please try `handle = FindWindowA(null, null);` – Cloud Cho Feb 27 '19 at 21:55