获取C#中的活动窗口坐标和高度宽度

Abh*_*ral 5 c# window

我只是在这里查看一些帖子,但没有一个对我有帮助.

我想要做的是运行Screen Capturing的后台进程.现在我想要一段代码,它会给我X,Y或任何打开的Active/Current Window(Say Notepad)及其高度和宽度.

就是这样,没有别的.

Emi*_*nem 13

[DllImport("user32.dll")]  
static extern IntPtr GetForegroundWindow();  


private IntPtr GetActiveWindow()  
{  
    IntPtr handle = IntPtr.Zero;  
    return GetForegroundWindow();  
}
Run Code Online (Sandbox Code Playgroud)

然后使用GetWindowRect获取窗口位置.

[DllImport("user32.dll")]  
[return: MarshalAs(UnmanagedType.Bool)]  
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);  

[StructLayout(LayoutKind.Sequential)]  
public struct RECT  
{
    public int Left;        // x position of upper-left corner  
    public int Top;         // y position of upper-left corner  
    public int Right;       // x position of lower-right corner  
    public int Bottom;      // y position of lower-right corner  
}
Run Code Online (Sandbox Code Playgroud)