1

マネージ C++ で UI オートメーションを使用してControlType.DataItem、コントロールの子を見つけようとしています。DataGrid次のスニペットは、既知のHWND値に対してC# から機能します。

var automationElement = AutomationElement.FromHandle(new IntPtr(0x000602AE));
var propertyCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem);
var dataItems = automationElement.FindAll(TreeScope.Subtree, propertyCondition).Count;
Console.WriteLine("Found {0} DataItem(s)", dataItems);

これにより、次の出力が得られます。

Found 2 DataItem(s)

コードを MC++ に変換すると、結果はゼロになります。変換された MC++ コードは次のとおりです。

auto automationElement = AutomationElement::FromHandle(IntPtr(0x000602AE));
auto propertyCondition = gcnew PropertyCondition(AutomationElement::ControlTypeProperty, ControlType::DataItem);
auto dataItems = automationElement->FindAll(TreeScope::Subtree, propertyCondition)->Count;
Console::WriteLine("Found {0} DataItem(s)", dataItems);

マネージド C++ から UI オートメーションを使用してこの問題に遭遇した人はいますか? 私は過去に UIA に MC++ を使用したことがあり、これが C# からの使用との最初の違いです。情報をお寄せいただきありがとうございます。

4

1 に答える 1

0

なぜこれが発生するのかはわかりませんが、UIオートメーションコードを別のクラスに抽出してそのように呼び出すと、いつでも機能します。私はMC++またはMC++が.NETFrameworkをロードする方法の専門家であるとは主張していませんが、これが私の問題を修正した方法です。

AutomatedTableクラスを作成する

AutomatedTable.h

#pragma once
using namespace System;
using namespace System::Windows::Automation;

ref class AutomatedTable {
public:
    AutomatedTable(const HWND windowHandle);

    property int RowCount {
        int get();
    }

private:
    AutomationElement^ _tableElement;
};

AutomatedTable.cpp

#include "StdAfx.h"
#include "AutomatedTable.h"

AutomatedTable::AutomatedTable(const HWND windowHandle) {
    _tableElement = AutomationElement::FromHandle(IntPtr(windowHandle));
}

int AutomatedTable::RowCount::get() {
    auto dataItemProperty = gcnew PropertyCondition(AutomationElement::ControlTypeProperty, ControlType::DataItem);
    return _tableElement->FindAll(TreeScope::Subtree, dataItemProperty)->Count;
}

から呼び出すmain.cpp

main.cpp

#include "stdafx.h"
#include "AutomatedTable.h"

int main(array<System::String ^> ^args) {
    auto automatedTable = gcnew AutomatedTable((HWND)0x000602AE);
    Console::WriteLine("Found {0} DataItem(s)", automatedTable->RowCount);
    return 0;
}

出力

Found 2 DataItem(s)

なぜこれが違うのかについての洞察は大歓迎です:-)

于 2012-12-04T16:07:25.767 に答える