views:

123

answers:

1

is there a way in any programming language, perhaps using the Microsoft SDK, to get the on screen position of UI elements within a window of any application ? If so, could you please provide guidance with an example perhaps? Thank you, It's been paining me for days!

Please consider that it is an external application. So for example, if I opened MS word, my application should be able to get the screen location of the "Bold" button.

So far, I believe I will need to use hooks.

+1  A: 

To get the position of a control in an external application you basically have to go through the following steps:

  • Call EnumWindows which enumerated all top level windows
  • For the top level window you are interested in call EnumChildWindows
  • Call GetWindowRect for the child window (control) whose position you want to retrieve

The details for this depend on what you are exactly trying to achieve.

Alternativly you could use the Windows Automation API (http://msdn.microsoft.com/en-us/library/dd561932(VS.85).aspx) with GetCurrentPorpertyValue(UIA_BoundingRectanglePropertyId, result).

I don't know if there are managed wrappers for this functions, but you could fallback to P/Invoke.

Have also a look into the UI Spy tool which ships with the Windows SDK (http://msdn.microsoft.com/en-us/library/ms727247.aspx).

JPW
Thanks a whole lot for the Direction JPW! However, are you sure that EnumChildWindows() will get buttons and so on? I think it only gets sub-windows. I have an idea of this from using UI Spy tool already. It's a nice tool. I've tried to get some sort of identification for buttons in Photoshop app. but they don't show up. UISpy was able to show the docking panels and some tabs.
Roger Gajraj
@Roger Button is also a window
Jujjuru
Unfortunately some applications draw the button theirselves (that is without registering them as subwindows); especially Office applications do this. The other approach (using Automation API) should work however, but also has some drawbacks (e.g. Adobe Reader will show up a window for configuring a Screen Reader). As far as I known there is no solution which solves ALL of these problems.
JPW