views:

52

answers:

3

My application is something like the Spy++ application: i want to be able to automatically retreive all the different controls of the active window (any application) and their children, and for each control i want to know the type, the name, and the value (caption or text).

I am using a C# windows app.

what is the solution to iterate all the controls of the foreground window and their children (and so on) and retrieve name, type and value? Your help is needed. Thanks,

A: 

Yes you will have to use the windows API if its a window thats not part of your current application. This will get you the currently active window:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;

public class MainClass

    // Declare external functions.
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

    public static void Main() {
        int chars = 256;
        StringBuilder buff = new StringBuilder(chars);

        // Obtain the handle of the active window.
        IntPtr handle = GetForegroundWindow();

        // Update the controls.
        if (GetWindowText(handle, buff, chars) > 0)
        {
            Console.WriteLine(buff.ToString());
            Console.WriteLine(handle.ToString());
        }
    }
}

It uses the GetWindowText() function to find the name of the window, so I assume it shouldn't be a problem to find out other properties of the windows such as its controls etc.

w69rdy
Thanks, but you didn;t answer the question and you just said that it shouldn't be a problem. What i need is how to iterate all the controls of the foreground window and their children (and so on) and retrieve name, type and value.
Zee99
I think i did, you asked for comments on how feasible it would be and if you'd have to use the Windows API. I said you would have to and demonstrated how feasible it is by giving you an example of how to use it. It is practically done, all you need to do is find out what functions you need to call to find out information about its controls and that is a BONUS as you asked for suggestions about feasibility, not a solution.
w69rdy
Thanks, but again, how to iterate all the controls of the foreground window and their children (and so on) and retrieve name, type and value?
Zee99
Very useful for window caption and textboxes, but i am not able to get text from other controls, like for example a grid, a listbox, a combobox, do you know how can i do that?
Zee99
A: 

There are a number of Win32 API functions you can use to write your own Spy++ program. This link explains how to write a Spy++ clone in Visual Basic. I know, you probably don't use Visual Basic, but this document does show you how to duplicate Spy++ using the Win32 API. It should not require much effort to translate this to C#.

Trevor Balcom
+2  A: 

To enumerate top level windows use EnumWindows(), to get their child windows use EnumChildWindows().

Using theHWNDs from the enumeration, a top level window with a title bars value can be read via GetWindowText(), for other windows you can use the WM_GETTEXT message, or depending on exactly what you want, a message specific to the windows class such as LB_GETTEXT for a listbox.

RealGetWindowClass() will give you the windows class.

Window API reference; http://msdn.microsoft.com/en-us/library/ff468919%28v=VS.85%29.aspx

Alex K.
Very useful for window caption and textboxes, but i am not able to get text from other controls, like for example a grid, a listbox, a combobox, do you know how can i do that?
Zee99