Как мне найти кнопку в другом приложении используя c#
Я пишу фарминг бота и мне нужно нажать на кнопку в игре, я использовал этот код чтобы попытаться на неё нажать:
string ApplicationName = "Form1";
string ButtonName = "Button1";
var windows = AutomationElement.RootElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
MessageBox.Show(windows.ToString());
foreach (AutomationElement window in windows)
{
var windowName = window.GetCurrentPropertyValue(AutomationElement.NameProperty) as string;
MessageBox.Show(windowName.ToString());
if (windowName.StartsWith(ApplicationName))
{
MessageBox.Show(windowName.StartsWith(ApplicationName).ToString());
var button = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, ButtonName));
System.Threading.Thread.Sleep(1000);
if (button != null)
{
var invokePattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern.Invoke();
MessageBox.Show("ButtonPress");
return;
}
//else
//{
// Console.WriteLine("ButtonNotPress");
//}
}
}
MessageBox.Show("ButtonNotFound");
Но он не очень хорошо работает, например: на простую кнопку в моей форме он нажмет, а уже на кнопку в игре не нажмет.
Есть ли какие-то способы лучше чем этот, который гарантированно нажмет на кнопку или придется делать анализацю изображения через openCV