Code Wave Code Wave - 1 year ago 129
C# Question

Getting Active Window Coordinates and Height Width in C#

I just check in some of the posts here, but none were helpful to me.

The thing I am trying to do is to run a background process of Screen Capturing. Now I want a piece of code that would give me the X, Y or any Active/Current Window opened (Say Notepad) and its Height and Width.

Just that and nothing else.

Answer Source
static extern IntPtr GetForegroundWindow();  

private IntPtr GetActiveWindow()  
    IntPtr handle = IntPtr.Zero;  
    return GetForegroundWindow();  

Then get the window position with GetWindowRect.

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

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  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download