开发者

Retrieve all the controls of any window with their types and value

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 t开发者_如何学编程he solution to iterate all the controls of the foreground window and their children (and so on) and retrieve name, type and value?


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


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#.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜