6

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

RV.
  • 2,782
  • 8
  • 39
  • 51
  • I'm not sure what you mean when you say the return values are location dependent. You will get the bounding rect (this of course depends also on the location of the window). So the width is (Right-Left) and height is (Top-Bottom). Those differences should, of course, not vary if you move the window. – f3lix Mar 18 '09 at 14:15

5 Answers5

10

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.

Community
  • 1
  • 1
f3lix
  • 29,500
  • 10
  • 66
  • 86
  • 1
    You can't use the standard Rectangle - you need to define a struct like this: [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } – Rory Mar 12 '09 at 09:35
  • 1
    Thank you. As I wrote, I was not really sure about my answer. But I thought it would point into the right direction. I added your comment to my answer and added it to the community wiki, so you can edit it, too. – f3lix Mar 12 '09 at 09:45
  • +1 for "Don't forget to replace Rectangle with RECT in the first piece of code." – User 12345678 Aug 14 '14 at 00:16
2
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);           

public static Size GetControlSize(IntPtr hWnd)
{
    RECT pRect;
    Size cSize = new Size();
    // get coordinates relative to window
    GetWindowRect(hWnd, out pRect);

    cSize.Width = pRect.Right - pRect.Left;
    cSize.Height = pRect.Bottom - pRect.Top;

    return cSize;
}
Taryn
  • 242,637
  • 56
  • 362
  • 405
0

If you're building an MDI app, you could use:

parentForm.ActiveMDIChild.Size
Taryn
  • 242,637
  • 56
  • 362
  • 405
dommer
  • 19,610
  • 14
  • 75
  • 137
0

What exactly is your question?

  1. How to get the focused window?
  2. How to get the width and height of a Window?

If question 2, use Window.ActualWidth and Window.ActualHeight

0

If your window is inside your aplication, having an MDI app, you might use this, having

public static extern IntPtr GetForegroundWindow();

with you, try this

int wHeight = Control.FromHandle(GetForegroundWindow()).Height;
int wWidth = Control.FromHandle(GetForegroundWindow()).Width;
Kunal S
  • 97
  • 5
  • 11