Winapi MFC或WIN32 UI自动化客户端示例?

Winapi MFC或WIN32 UI自动化客户端示例?,winapi,ui-automation,Winapi,Ui Automation,我一直在寻找在MFC(或Win32)中使用UI自动化作为客户端滚动另一个应用程序窗口的示例。但我找不到任何样品 谁知道一个或可以提供一个? < P>下面是一个Win32 C++在记事本窗口上垂直滚动的例子: TestWindow .txt -记事本< /C> > /P> 主要步骤: 使用查找目标应用程序的主窗口句柄 FindWindow(L“记事本”,L“testWindow.txt-Notepad”) 从上面找到的窗口句柄获取对象 pClientUIA->ElementFromHandle

我一直在寻找在MFC(或Win32)中使用UI自动化作为客户端滚动另一个应用程序窗口的示例。但我找不到任何样品


谁知道一个或可以提供一个?

< P>下面是一个Win32 C++在记事本窗口上垂直滚动的例子:<代码> TestWindow .txt -记事本< /C> > /P>

主要步骤:

  • 使用查找目标应用程序的主窗口句柄

    FindWindow(L“记事本”,L“testWindow.txt-Notepad”)

  • 从上面找到的窗口句柄获取对象

    pClientUIA->ElementFromHandle(targetWindow和prooteElement)

  • 使用和查找包含滚动条的窗口的句柄

  • 向包含滚动条的窗口发送消息

    PostMessage(foundHwnd、WM_VSCROLL、SB_-LINEUP,0)

  • 这是您可以参考的完整代码:

    #include <windows.h>
    #include <uiautomation.h>
    
    IUIAutomation *pClientUIA;
    IUIAutomationElement *pRootElement;
    
    HWND FindScrollbarContainerWindow(const long controlType)
    {
        HRESULT hr;
        BSTR name;
        IUIAutomationCondition *pCondition;
        VARIANT varProp;
        varProp.vt = VT_I4;
        varProp.uintVal = controlType;
        hr = pClientUIA->CreatePropertyCondition(UIA_ControlTypePropertyId, varProp, &pCondition);
        if (S_OK != hr)
        {
            printf("CreatePropertyCondition error: %d\n", hr);
        }
    
        IUIAutomationElementArray *pElementFound;
        hr = pRootElement->FindAll(TreeScope_Subtree, pCondition, &pElementFound);
        if (S_OK != hr)
        {
            printf("CreatePropertyCondition error: %d\n", hr);
        }
        int eleCount;
        pElementFound->get_Length(&eleCount);
        if (eleCount == 0)
            return NULL;
    
        for (int i = 0; i <= eleCount; i++)
        {
            IUIAutomationElement *pElement;
            hr = pElementFound->GetElement(i, &pElement);
            if (S_OK != hr)
            {
                printf("CreatePropertyCondition error: %d\n", hr);
            }
            hr = pElement->get_CurrentName(&name);
            if (S_OK != hr)
            {
                printf("CreatePropertyCondition error: %d\n", hr);
            }
            wprintf(L"Control Name: %s\n", name);
    
            hr = pElement->get_CurrentClassName(&name);
            if (S_OK != hr)
            {
                printf("CreatePropertyCondition error: %d\n", hr);
            }
            wprintf(L"Class Name: %s\n", name);
    
            IUIAutomationTreeWalker* pContentWalker = NULL;
    
            hr = pClientUIA->get_ContentViewWalker(&pContentWalker);
            if (pContentWalker == NULL)
                return NULL;
    
            // Get ancestor element nearest to the scrollbar UI Automation element in the tree view
            IUIAutomationElement *ncestorElement;
            hr = pContentWalker->NormalizeElement(pElement, &ncestorElement);
    
            hr = ncestorElement->get_CurrentName(&name);
            wprintf(name);
    
            // Get window handle of ancestor element
            UIA_HWND controlContainerHwnd = NULL;
            hr = ncestorElement->get_CurrentNativeWindowHandle(&controlContainerHwnd);
            printf("");
    
            if (controlContainerHwnd)
            {
                return (HWND)controlContainerHwnd;
            }
        }
    
        return NULL;
    }
    
    int main()
    {
        // Find target window
        HWND targetWindow = FindWindow(L"Notepad", L"testWindow.txt - Notepad");
        if (NULL == targetWindow)
        {
            printf("FindWindow fails with error: %d\n", GetLastError());
            return FALSE;
        }
    
        HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
        if (S_OK != hr)
        {
            printf("CoInitializeEx error: %d\n", hr);
            return 1;
        }
    
        hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_IUIAutomation, reinterpret_cast<void**>(&pClientUIA));
        if (S_OK != hr)
        {
            printf("CoCreateInstance error: %d\n", hr);
            return 1;
        }
    
        hr = pClientUIA->ElementFromHandle(targetWindow, &pRootElement);
        if (S_OK != hr)
        {
            printf("ElementFromHandle error: %d\n", hr);
            return 1;
        }
    
        // Find scroll bar and its containing window
        HWND foundHwnd = FindScrollbarContainerWindow(UIA_ScrollBarControlTypeId);
        if (NULL == foundHwnd)
            return 1;
    
        // Vertical scroll bar
        // Line up - Like click top arrow button to scroll up one line
        PostMessage(foundHwnd, WM_VSCROLL, SB_LINEUP, 0);
        Sleep(1000);
    
        // Line down
        PostMessage(foundHwnd, WM_VSCROLL, SB_LINEDOWN, 0);
        Sleep(1000);
    
        // Page up
        PostMessage(foundHwnd, WM_VSCROLL, SB_PAGEUP, 0);
        Sleep(1000);
    
        // Page down
        PostMessage(foundHwnd, WM_VSCROLL, SB_LINEDOWN, 0);
        Sleep(1000);
    }
    
    #包括
    #包括
    i自动化*p客户;
    IUIAutomationElement*pRootElement;
    HWND FindScrollBarContainerWindows(常量长控制类型)
    {
    HRESULT-hr;
    BSTR名称;
    IUIAutomationCondition*pccondition;
    变异varProp;
    varProp.vt=vt_I4;
    varProp.uintVal=控制类型;
    hr=pClientUIA->CreatePropertyCondition(UIA\u ControlTypePropertyId、varProp和pcCondition);
    如果(S_OK!=小时)
    {
    printf(“CreatePropertyCondition错误:%d\n”,hr);
    }
    IUIAutomationElementArray*pElementFound;
    hr=prootement->FindAll(树范围子树、pCondition和pElementFound);
    如果(S_OK!=小时)
    {
    printf(“CreatePropertyCondition错误:%d\n”,hr);
    }
    整数计数;
    pElementFound->获取长度(&eleCount);
    if(eleCount==0)
    返回NULL;
    for(int i=0;i GetElement(i,&pElement);
    如果(S_OK!=小时)
    {
    printf(“CreatePropertyCondition错误:%d\n”,hr);
    }
    hr=pElement->get_CurrentName(&name);
    如果(S_OK!=小时)
    {
    printf(“CreatePropertyCondition错误:%d\n”,hr);
    }
    wprintf(L“控件名称:%s\n”,名称);
    hr=pElement->get_CurrentClassName(&name);
    如果(S_OK!=小时)
    {
    printf(“CreatePropertyCondition错误:%d\n”,hr);
    }
    wprintf(L“类名:%s\n”,名称);
    IUIAutomationTreeWalker*pContentWalker=NULL;
    hr=pClientUIA->get\u ContentViewWalker(&pContentWalker);
    if(pContentWalker==NULL)
    返回NULL;
    //获取树视图中最靠近滚动条UI自动化元素的祖先元素
    IUIAutomationElement*ncestorElement;
    hr=pContentWalker->NormalizeElement(pElement和ncestoreelement);
    hr=ncestorElement->get\u CurrentName(&name);
    wprintf(名称);
    //获取祖先元素的窗口句柄
    UIA_HWND controlContainerHwnd=NULL;
    hr=nceStoreElement->get_CurrentNativeWindowHandle(&controlContainerHwnd);
    printf(“”);
    if(controlContainerHwnd)
    {
    返回(HWND)控制容器HWND;
    }
    }
    返回NULL;
    }
    int main()
    {
    //查找目标窗口
    HWND targetWindow=FindWindow(L“记事本”,L“testWindow.txt-记事本”);
    if(NULL==targetWindow)
    {
    printf(“FindWindow失败,错误:%d\n”,GetLastError());
    返回FALSE;
    }
    HRESULT hr=CoInitializeX(空,Conit_APARTMENTTHREADED);
    如果(S_OK!=小时)
    {
    printf(“CoInitializeEx错误:%d\n”,hr);
    返回1;
    }
    hr=CoCreateInstance(CLSID\u CUIAutomation,NULL,CLSCTX\u INPROC\u SERVER,IID\u IUIAutomation,reinterpret\u cast(&pClientUIA));
    如果(S_OK!=小时)
    {
    printf(“CoCreateInstance错误:%d\n”,hr);
    返回1;
    }
    hr=pClientUIA->ElementFromHandle(targetWindow和prooteElement);
    如果(S_OK!=小时)
    {
    printf(“ElementFromHandle错误:%d\n”,hr);
    返回1;
    }
    //查找滚动条及其包含窗口
    HWND foundHwnd=FindScrollbarContainerWindow(UIA_ScrollBarControlTypeId);
    if(NULL==foundHwnd)
    返回1;
    //垂直滚动条
    //向上排列-类似于单击顶部箭头按钮向上滚动一行
    PostMessage(foundHwnd、WM_VSCROLL、SB_-LINEUP,0);
    睡眠(1000);
    //排队
    PostMessage(foundHwnd、WM_VSCROLL、SB_LINEDOWN,0);
    睡眠(1000);
    //翻页
    PostMessage(foundHwnd,WM_VSCROLL,SB_PAGEUP,0);
    睡眠(1000);
    //翻页
    PostMessage(foundHwnd、WM_VSCROLL、SB_LINEDOWN,0);
    睡眠(1000);
    }
    
    ---------------------------------------------------------------

    更新:

    另一种方法是使用。更直接、更简单。