2012-06-27 16 views
1

我想單擊使用UI自動化的按鈕。我在Winform VC++中使用UI Automation。在VC++的Winform應用程序中使用UI自動化單擊按鈕

這裏是我的代碼..

AutomationElement^ Select_connect_button= aeForm->FindFirst(TreeScope::Children,gcnew PropertyCondition(AutomationElement::NameProperty, "Select/Connect")); 
InvokePattern^ ipClickButton1 = (InvokePattern)Select_connect_button->GetCurrentPattern(InvokePattern::Pattern); 
ipClickButton1->Invoke(); 

但它顯示這些錯誤:

error C2440: 'type cast' : cannot convert from 'System::Object ^' to 'System::Windows::Automation::InvokePattern' 

error C2440: 'initializing' : cannot convert from 'System::Windows::Automation::InvokePattern' to 'System::Windows::Automation::InvokePattern ^' 

任何人可以幫我解決這些錯誤。

謝謝。

回答

0

構建錯誤是您將(InvokePattern)轉換爲「InvokePattern ^」。

在我的測試中,更新了第二條線下面的代碼將解決這個問題:

InvokePattern^ ipClickButton1 = (InvokePattern^)Select_connect_button->GetCurrentPattern(InvokePattern::Pattern);

0

你不能與那些對象投射。這裏有一種方法可以在C#中完成。您可以在這裏獲取方法名稱等。你所需要的各種常數是這樣的:

C:\ Program Files文件\微軟的SDK \的Windows \ V7.0 \包含\ UIAutomationClient.h (可能是v7.1的目錄,而不是)

public static IUIAutomationInvokePattern elementToInvokePattern(this IUIAutomationElement element) 
    { 
     var conditionInvokePattern = auto.CreatePropertyCondition(
                 WindowsConstants.UIA_IsInvokePatternAvailablePropertyId, 
                 true); 

     var cacheRequest = auto.CreateCacheRequest(); 
     cacheRequest.AddPattern(WindowsConstants.UIA_InvokePatternId); 

     var cachedElement = element.FindFirstBuildCache(TreeScope.TreeScope_Element, 
              conditionInvokePattern, 
              cacheRequest); 

     var invokePattern = (IUIAutomationInvokePattern) 
      cachedElement.GetCachedPattern(WindowsConstants.UIA_InvokePatternId); 
     return invokePattern; 
    } 

看起來像常量在這個例子中是從這裏:http://msdn.microsoft.com/en-us/library/dd757483.aspx

相關問題