如何获得窗口的位置?

时间:2012-03-12 14:23:42

标签: c# process window position intptr

我想知道获取进程窗口位置的方法。我一直在网上寻找,但没有结果。谢谢:))

Process[] processes = Process.GetProcessesByName("notepad");
Process lol = processes[0];

IntPtr p = lol.MainWindowHandle;

2 个答案:

答案 0 :(得分:14)

试试这个:

[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);

答案 1 :(得分: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);
    }
}