Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find window Height & Width

Tags:

c#

how to find focused window Height & Width ..

it might be any windows window like notepad,mspaint etc... i can get the focused window by help of this code

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

hi f3lix it's working but it's return the value depends on the location only.. if i change the location it's return some other values

Kunal it's return error msg....like object refrence not set

like image 289
RV. Avatar asked Dec 23 '22 12:12

RV.


1 Answers

I think you have to use user32.dll functions via PInvoke. I'm not sure, but I would do it somewhat like this:

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

[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect); 


Rectangle rect = new Rectangle();
GetWindowRect(GetForegroundWindow(), out rect);

Note: I did not try this code, because I am currently not working on Windows...

EDIT: Rory pointed out to me (see comments) that we can't use the standard Rectangle here and we need to define our own RECT.

[StructLayout(LayoutKind.Sequential)]
public struct RECT {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

Don't forget to replace Rectangle with RECT in the first piece of code.

like image 97
3 revs Avatar answered Jan 12 '23 09:01

3 revs