gpuguy
gpuguy

Reputation: 4585

Possible ways for Finding Window coordinate using C++ Windows Form Application

I want to write an application that will automatically detect and fill the text field in the window shown below:

enter image description here

(assuming the data to be entered is in a file).

The question is how does my application find this text field?

I can do this job if I am able to find the location of the text field on the desktop through program.

Can someone help me understand possible ways for finding this text field?

I am using Windows Form application in C++.

Update:

I played with spy++.
I used spy++, to find the window handle. I did it by putting finder on the window I am interested in. Its giving handle in hex values: 00080086 (actually just for testing purpose I put the finder tool on Visual Studio new project page ). How do I interpret this Hex value into meaningful window name ?

See the below figure. What is the next step to get to the text field " Enter name" under "name" field.

****Any sample code will be highly appreciated.**

I am open to any solution not necessarily how I am doing this.

enter image description here

Upvotes: 0

Views: 1252

Answers (3)

gpuguy
gpuguy

Reputation: 4585

Although the answer given by Simon is accepted and is the best one, but still for future visitors I am providing this link which has more description for UI automation of windows applications. .

Also for automating a web application one may want to go to this link

Upvotes: 0

Simon Mourier
Simon Mourier

Reputation: 138776

One solution is to use the Microsoft UI Automation technology. It's shipped out-of-the-box with Windows since Vista. It's usable from .NET but also from C++ using COM.

Here is a short C++ console application example that displays the class name of the UI Automation Element currently at the middle of the desktop window, each second (you can have it run and see what it displays):

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);
    IUIAutomation *pAutomation; // requires Uiautomation.h
    HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation), (LPVOID *)&pAutomation);
    if (SUCCEEDED(hr))
    {
        RECT rc;
        GetWindowRect(GetDesktopWindow(), &rc);
        POINT center;
        center.x = (rc.right - rc.left) / 2;
        center.y = (rc.bottom - rc.top) / 2;
        printf("center x:%i y:%i'\n", center.x, center.y);
        do
        {
            IUIAutomationElement *pElement;
            hr = pAutomation->ElementFromPoint(center, &pElement);
            if (SUCCEEDED(hr))
            {
                BSTR str;
                hr = pElement->get_CurrentClassName(&str);
                if (SUCCEEDED(hr))
                {
                    printf("element name:'%S'\n", str);
                    ::SysFreeString(str);
                }
                pElement->Release();
            }
            Sleep(1000);
        }
        while(TRUE);
        pAutomation->Release();
    }

    CoUninitialize();
    return 0;
}

From this sample, what you can do is launch the application you want to automate and see if the sample detects it (it should).

You could also use the UISpy tool to display the full tree of what can be automated in your target app. You should see the windows and other elements (text field) of this target app and you should see the element displayed by the console application example.

From the pElement discovered in the sample, you can call FindFirst with the proper condition (class name, name, control type, etc...) to get to the text field. From this text field, you would use one of the UI Automation Patterns that should be available (probably TextPattern or ValuePattern) to get or set the text itself.

The cool thing is you can use the UISpy tool to check all this is possible before actually coding it.

Upvotes: 2

Paskas
Paskas

Reputation: 2848

You could enumerate windows and then find it. For exploring application on your screenshot you could you Spy++ (spyxx.exe) that is distributed with visual studio. In you code, you clould use EnumWindows and EnumChildWindows to enumerates all window or all child windows to find one you need.

Upvotes: 0

Related Questions