![](/img/trans.png)
[英]How do i get data from a web api into a c# windows forms app class
[英]How do I get a button handle from Windows10Forms in C#?
我一直试图在程序中找到特定帮助按钮的句柄,然后向它发送 BN_CLICK 消息。 为了调试,我查看了父窗口和按钮的句柄。
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
public Form1()
{
IntPtr hWndParent = FindWindow("WindowsForms10.Window.8.app.0.2c040a7_r9_ad1", null);
Debug.WriteLine(hWndParent,"\n");
IntPtr button = FindWindowEx(hWndParent, IntPtr.Zero, "WindowsForms10.BUTTON.app.0.2c040a7_r9_ad1", "Help");
Debug.WriteLine(button);
}
调试为 hWndParent 返回一个数字,但为按钮返回 0。 我从 Spy++ 那里得到了课程。 1
由于应用程序中有两个具有相同类的“帮助”按钮,这可能会变得复杂。 这是我尝试使用帮助按钮获取句柄的应用程序窗口的图片,我想单击用红色框突出显示。 2
我尝试添加通过 AutoIT Info 获得的实例编号。
IntPtr button = FindWindowEx(hWndParent, IntPtr.Zero, "WindowsForms10.BUTTON.app.0.2c040a7_r9_ad113", "Help");
这也为按钮返回了 0,就像用 null 替换“Help”一样。 如果有人熟悉从 Windows 10 窗体获取句柄并知道如何执行此操作,我们将不胜感激。 谢谢!
安德鲁
感谢 Hans 的帖子 - 它为我指明了正确的方向。 我已经使用 Selenium 和 Appium 来自动化点击/输入。 对于将来在寻找解决方案时偶然发现的任何人,这里有一些代码可以提供帮助。
// You will need a few resources
using System;
using System.Windows.Forms;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
// Making our driver
protected static WindowsDriver<WindowsElement> driver;
// I don't want to have to manually open WinAppDriver so..
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = @"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
// Now I connect to my app by setting up the driver
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("platformName", "Windows");
caps.SetCapability("platformVersion", "10");
caps.SetCapability("deviceName", "WindowsPC");
caps.SetCapability("app", "C:\\FileLocation.exe");
driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), caps);
// Commands can be sent as below
driver.FindElementByAccessibilityId("I'll explain this below").Click();
为了找到 AccessibilityId,我发现最容易使用的工具是AutoIT Window Info 。 将取景器拖到所需的按钮上。 然后在摘要选项卡中,您的 AccessibilityId 被简单地标记为“ID”。 我选择 AccessibilityId 是因为我想控制的应用程序中有多个同名按钮。
您需要安装WinAppDriver并将正确的位置放入代码中。 Appium 和 Selenium 可以通过 Nuget 管理器添加,但我的代码中使用的一些功能已被弃用。 我刚刚使用了 Selenium.WebDriver 和 Selenium.Support 的 3.0.1 版本以及 Appium.WebDriver 的 3.0.0.1 版本(其他人可能会工作,这些只是第一个工作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.