开发者

Getting Active Window Coordinates and Height Width in C#

开发者 https://www.devze.com 2023-03-03 05:23 出处:网络
I just check in s开发者_StackOverflow社区ome 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 cod

I just check in s开发者_StackOverflow社区ome 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.


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


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

Then get the window position with 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  
}


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

    static void Main()
    {
        SetProcessDPIAware();
        Rectangle t2;
        GetWindowRect(GetForegroundWindow(),out t2);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消