tags:

views:

44

answers:

2

What is the best way for, using WinApi, find a given control in a external program window?

For example, I'm trying to change Internet Explorer's url text box. I am having trouble getting programatically the handle to the text box. I know its type is "Edit" but I'd like to avoid having to search through all the child windows for the "Edit" control (that's how I'm currently doing).

Is there any kind of unique identifier for a given control on a window? I tried using "Control ID" but it doesn't seem to be working.

Thanks

+1  A: 

The best way to do it is find it step by step.. E.g. Find the IE window with FindWindow, then find the child of that with FindWindowEx, then find the child of that with FindWindowEx ... until you get down to the textbox.

There is 1 program I can think of that will generate VB code from dragging a icon from the application to any part of any other application.. VB is way old but it'll give you a very good idea how to do it!

It's called API Spy, found under 'Downloadable Applications (Windows Only)' on http://patorjk.com/blog/software/

clocKwize
I know I can use FindWindowEx (as stated in the original post..). I was wondering if there isn't other way, just that :(. It's awful to have to do that when you have nested hierarchies, as in this case(it's 6 controls deep!).
devoured elysium
Just use recursion.
DeadMG
Sounds like a terrible use for recursion. Especially when you know exactly where the control is you are looking for... If it's 6 levels deep, that's 6 lines of code, if you keep it DRY. 6 lines that are self documenting.
clocKwize
@devoured elysium: it's low(ish) level operating system API... you can't expect too much..
clocKwize
+2  A: 

When you're delving into the windows of another application that wasn't designed to give you any particular special access to its windows, then you don't really have any simple solution. Functions like FindWindowEx, GetWindow, EnumChildWindows, and the rest are what you have to work with.

However, it's often not a great idea to even do this. Internet Explorer may have certain types of windows in a certain hierarchy in the particular version that you're developing against right now. But those windows and hierarchy may be different in previous versions and could be considerably different in future versions. You have no guarantee about these things.

In some cases, you might do well to investigate if there are alternative and more official ways to control the other program. For instance, Internet Explorer exposes a COM object that can be used for many purposes. Because this is an official interface, you have better guarantees about what previous versions this will be supported on and that it won't break for future versions.

TheUndeadFish
I answered the question but should have questioned the actual motive for doing it. for doing stuff with IE, the COM stuff exposed should be easier to work with from c++, but I have no experience with this.
clocKwize
Internet Explorer also responds to DDE commands, such as the "WWW_OpenURL" command.
Remy Lebeau - TeamB