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);
    }
}
Share:
41,891

Related videos on Youtube

Patryk
Author by

Patryk

:)

Updated on March 19, 2020

Comments

  • Patryk
    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;
    
  • Lost_In_Library
    Lost_In_Library about 12 years
    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 )
  • hazem
    hazem over 10 years
    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...
  • Nick
    Nick over 10 years
    the Rect member var has wrong order. It should be Left, Top, Right Bottom. Otherwise, you will get incorrect values.
  • SimpleVar
    SimpleVar almost 8 years
    100% sure lol variable once stood for league of legends window :P
  • rayzinnz
    rayzinnz over 7 years
    Why have you got the FindWindow function included?
  • shruti singh
    shruti singh over 5 years
    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.
  • Cloud Cho
    Cloud Cho about 5 years
    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);