Search code examples
c#javawindowscoordinatesbounds

Get the bounds of any window on the desktop in Screen Coordinates, is it possible?


I am searching for a way to get the screen coordinate bounds of any arbitrary window that may be displayed on the desktop at any time on a Windows machine. For instance, say you have a pdf document open in adobe on the desktop, I'd like to be able to get the coordinates/bounds of that arbitrary window with some written program, which language is used is not important.

My specific question: Is the above possible?

What I've done so far is simply get the screen position of the mouse in Java using the PointerInfo class, specifically the getPointerInfo() method. (API here: http://docs.oracle.com/javase/6/docs/api/java/awt/PointerInfo.html). Again, all I've done is find out where the mouse is in screen coordinates, this is not directly related to my problem, but I thought it was a good place to start.

I'm not looking for someone to give me a code snippet of how this is done, simply acknowledgement that it can be done and perhaps a link to an API or class documentation to help out. Again, I'm not looking for a solution in any particular language.

Thank you for your time, -Kevin

EDIT: I've posted this in c# as it seems this may be the best language for this problem.


Solution

  • Yes, of course it is possible. (And under windows the best language to use for this job is C#.)

    There may be some way to do that by using only dotnet objects and invoking only dotnet functions, but I do not know of any such method.

    The way that I know involves pinvoke, which is the mechanism through which managed code in dotnet can call unmanaged code in the native windows dlls.

    Look at the WindowFromPoint function: http://www.pinvoke.net/default.aspx/user32.windowfrompoint

    And once you have found the window, look at the GetWindowRect function: http://www.pinvoke.net/default.aspx/user32.getwindowrect