Я сам решил проблему!!! Я так счастлив... лол. Я не пробовал автогорячие клавиши, но решение, которое я нашел, хорошее, или, по крайней мере, я так думаю. Здесь дается отличное объяснение... http://msdn.microsoft.com/en-us/magazine/cc163288.aspx
В любом случае, решение заключается в том, что vb.net имеет библиотеку/справочник «Автоматизация пользовательского интерфейса», которую вы можете добавить в свой проект. Я видел, как это упоминалось несколько раз в Интернете, но не мог понять, как его импортировать (извините, я новичок в vb.net/coding). В любом случае, вы добавляете ссылку «Автоматизация пользовательского интерфейса» в свой проект, а затем импортируете ее. Затем вы можете использовать библиотеку, чтобы найти все элементы управления на экране. Это включает в себя все кнопки на панели инструментов.
Я добавил пример кода ниже, чтобы продемонстрировать, как я его решил. Я уверен, что это в некотором роде веревочное и, вероятно, могло бы быть сделано лучше, но это работает. Дайте мне знать, что вы думаете о коде/решении.
Чтобы запустить код, вам нужно знать индекс кнопки и индекс панели инструментов. Если вы переходите в режим отладки, вы можете увеличивать индекс вручную и проверять controlName, пока не появится правильная кнопка. В приведенном ниже примере мне нужно было найти родительский элемент (окно), затем дочерний элемент (панель инструментов), а затем дочерние элементы (все кнопки под панелью инструментов).
Imports system.windows.automation
Imports system.eventargs
'RoutedEvenArgs has to exist as a class so I declare it here...
Public class RoutedEventArgs Inherits EventArgs
end class
'my form code is all under one class - I'll probably break it up better
' but at the moment this is how it is
Public Class form1
Private Sub Button1_Click_1(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'click the button of hte automation front end and
' call the findtreeviewdescendants procedure
FindTreeViewDescendants()
End Sub
Private Sub FindTreeViewDescendants()
'define the desktop as the rootelement as everywindow is a child of this element
Dim aedesktop As AutomationElement = AutomationElement.RootElement
'create an automationelementcollection variable to store the buttons
Dim aebuttons As AutomationElementCollection
'find the screen using the screen title
aeform = aedesktop.FindFirst(TreeScope.Children, New PropertyCondition _
(AutomationElement.NameProperty, "Single Stock View"))
'find all the child controls (this brings back all controls including the toolstrip)
aebuttons = aeform.FindAll(TreeScope.Children, New PropertyCondition _(AutomationElement.IsControlElementProperty, True))
'create an automationelement to store the button and get information out of it
Dim a As AutomationElement
'each button, in the collection, has an index (incidentally the index number corresponds with the order in which the window loads each of the elements into the window), in this case the toolstrip is index 1 as it's in the header of the screen
a = aebuttons.Item(1)
'get the child elements of the toolstrip element (something interesting is that in this case there were 19 elements but when you use findwindowex you only get back 4)
aebuttons = a.FindAll(TreeScope.Children, New PropertyCondition(AutomationElement.IsControlElementProperty, True))
'again use the index of the button to pull back the element information
a = aebuttons.Item(11)
'create a stringbuilder to store information about the element
Dim elementInfoCompile = New StringBuilder()
'identify the controlname, which in my case is the tooltip tag of the button (thus solving the problem of how I can find a button with an image instead of text in it)
Dim controlName As String
If (a.Current.Name = "") Then
controlName = "Unnamed control"
Else
controlName = a.Current.Name
End If
'identify the autoidname - which in all cases seemed to be null - I've no idea why but this didn't matter anyway
Dim autoIdName As String
If (a.Current.AutomationId = "") Then
autoIdName = "No AutomationID"
Else
autoIdName = a.Current.AutomationId
End If
'invoke a click of the button
InvokeControl(a)
End Sub 'FindTreeViewDescendants
'the rest is self-explanatory....
Private Sub InvokeControl(ByVal targetControl As AutomationElement)
Dim invokePattern As InvokePattern = Nothing
Try
invokePattern = _
DirectCast(targetControl.GetCurrentPattern(invokePattern.Pattern), _
InvokePattern)
Catch e As ElementNotEnabledException
' Object is not enabled.
Return
Catch e As InvalidOperationException
' Object doesn't support the InvokePattern control pattern
Return
End Try
invokePattern.Invoke()
End Sub 'InvokeControl
End Class
person
user2068871
schedule
14.02.2013