模拟在另一个过程中单击Windows Froms ToolStrip按钮

时间:2018-12-12 17:17:11

标签: winforms click toolstrip toolstripbutton

我在另一个应用程序中找到了Windows Forms ToolStrip的句柄。 (窗口名称为toolStrip1,类名称为WindowsForms10.Window.8.app.0.378734a。)

是否可以枚举子按钮,按标题查找按钮并模拟按钮单击?这些按钮不是子窗口,因此EnumChildWindows不起作用。

在ToolStrip自身上以恒定坐标模拟鼠标单击不是一个很好的选择,因为可用的按钮和按钮标题可能会更改。

1 个答案:

答案 0 :(得分:0)

正如@Jimi所建议的,解决方案是使用UI Automation Windows API。我设法通过从桌面窗口到给定元素的UI元素的UI自动化树中向下查找来找到按钮,匹配元素的名称。然后,我在其上调用了Invoke以模拟点击。 这里是一个高度非结构化的“ C风格” C ++ 11测试代码,用于演示,以防其他人发现它有用。您将需要链接到ole32.lib和oleaut32.lib。

#include <iostream>
#include <UIAutomation.h>

bool GetChildElementByName(IUIAutomationElement * * Child,
                           IUIAutomationElement * Parent,
                           const wchar_t * Name, IUIAutomation * UIAut);

int main(int argc, char * argv[])
{
    // Initialize COM
    switch ( CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED) )
    {
    case S_OK:
        break;
    case S_FALSE:
        CoUninitialize();
        // fall through
    default:
        std::cout << "CoInitializeEx error.\n";
        return 1;
    }
    // Create a CUIAutomation object and query IUIAutomation interface
    IUIAutomation * uiautomation;
    if ( CoCreateInstance(CLSID_CUIAutomation, nullptr, CLSCTX_INPROC_SERVER,
                          IID_IUIAutomation, reinterpret_cast<LPVOID *>(&uiautomation)) != S_OK )
    {
        std::cout << "CoCreateInstance error.\n";
        CoUninitialize();
        return 1;
    }

    // Get the desktop UI element
    IUIAutomationElement * desktop;
    if ( uiautomation->GetRootElement(&desktop) != S_OK )
    {
        std::cout << "GetRootElement error.\n";
        uiautomation->Release();
        CoUninitialize();
        return 1;
    }

    // Find a button element in a window inside a toolstrip.
    IUIAutomationElement * elem;
    // Fix memory leak...
    if
    (
        !GetChildElementByName(&elem, desktop, L"Thermo Scientific LabWriter  V4.6", uiautomation) ||
        !GetChildElementByName(&elem, elem, L"toolStrip1", uiautomation) ||
        !GetChildElementByName(&elem, elem, L"Print", uiautomation)
    )
    {
        desktop->Release();
        uiautomation->Release();
        CoUninitialize();
        return 1;
    }
    desktop->Release();

    // The invoke control pattern contains the Invoke method that can be used to click the button
    IUIAutomationInvokePattern * invokepattern;
    if ( elem->GetCurrentPattern(UIA_InvokePatternId,
                                 reinterpret_cast<IUnknown * *>(&invokepattern)) != S_OK )
    {
        std::cout << "GetCurrentPattern error.\n";
        elem->Release();
        uiautomation->Release();
        CoUninitialize();
        return 1;
    }
    if ( invokepattern == nullptr )
    {
        // Possibly element is not even a button, as it should have the invoke control pattern.
        std::cout << "Invoke pattern not present.\n";
        elem->Release();
        uiautomation->Release();
        CoUninitialize();
        return 1;
    }

    // Click the button
    if ( invokepattern->Invoke() != S_OK )
    {
        std::cout << "Button click failed.\n";
        invokepattern->Release();
        elem->Release();
        uiautomation->Release();
        CoUninitialize();
        return 1;
    }

    elem->Release();
    invokepattern->Release();
    uiautomation->Release();
    CoUninitialize();

    std::cout << "Done.\n";
    return 0;
}

bool GetChildElementByName(IUIAutomationElement * * Child,
                           IUIAutomationElement * Parent,
                           const wchar_t * Name, IUIAutomation * UIAut)
{
    // Create a condition that matches elements with name *Name
    IUIAutomationCondition * cond;
    // Parameter for the condition is a string-typed VARIANT containing the name.
    VARIANT name;
    VariantInit(&name);
    name.vt = VT_BSTR;
    name.bstrVal = SysAllocString(Name);
    if ( name.bstrVal == nullptr )
    {
        std::cout << "SysAllocString error.\n";
        return false;
    }
    if ( UIAut->CreatePropertyCondition(UIA_NamePropertyId, name, &cond) != S_OK )
    {
        std::cout << "CreatePropertyCondition error.\n";
        VariantClear(&name);
        return false;
    }
    VariantClear(&name);

    // Find the first child element satisfying the condition.
    if ( Parent->FindFirst(TreeScope_Children, cond, Child)  != S_OK )
    {
        std::cout << "FindFirst error.\n";
        cond->Release();
        return false;
    }
    cond->Release();
    if ( *Child == nullptr )
    {
        std::cout << "Child element \"";
        std::wcout << Name;
        std::cout << "\" not found.\n";
        return false;
    }
    // Child element found
    return true;
}