当前位置: 首页 > 知识库问答 >
问题:

如何从C#中的Windows10Forms获取按钮句柄?

颛孙镜
2023-03-14

我一直在尝试在程序中找到特定帮助按钮的句柄,然后向其发送BN\U 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。我从间谍那里得到了这些课程。1.

这可能会因为应用程序中具有相同类的两个“帮助”按钮而变得复杂。这是我试图使用帮助按钮获取句柄的应用程序窗口的图片,我想单击用红框突出显示的帮助按钮。2

我尝试添加通过AutoIT Info获得的实例号。

IntPtr button = FindWindowEx(hWndParent, IntPtr.Zero, "WindowsForms10.BUTTON.app.0.2c040a7_r9_ad113", "Help");

这也返回了一个0 for按钮,将“Help”替换为null也是如此。如果有人熟悉从Windows 10窗体获取句柄,并且知道如何执行此操作,我们将非常感谢您的帮助。谢谢

安得烈

共有1个答案

公羊嘉
2023-03-14

感谢汉斯的帖子,它为我指明了正确的方向。我支持使用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窗口信息。将查找器拖动到所需的按钮上。然后,在摘要选项卡中,您的AccessibilityId被简单地标记为“ID”。我选择AccessibilityId,因为我想控制的应用程序中有多个同名按钮。

您需要安装WinAppDriver并将正确的位置放入代码中。Appium和Selenium可以通过Nuget Manager添加,但我的代码中使用的一些功能是deprated。我刚刚使用了3.0.1版本的Selenium。WebDriver和Selenium。支持和3.0.0.1版本的Appium。WebDriver(其他人可能会工作,这些只是第一个工作)。

 类似资料:
  • 我想得到工具栏按钮的句柄。我知道它的ID和工具栏按钮所在的窗口句柄。但是GetDlgItem函数似乎不适用于工具栏按钮。 我原以为具有正确参数的函数SendMessage()可以获得句柄,但我找不到任何有助于实现此目的的msg参数。 什么函数可以帮助我获得它的句柄?

  • 使用Inspect.exe我可以看到应用程序组件的树结构中存在一个按钮,但我找不到获取该按钮句柄的方法。这是控件的Inspect.exe输出: 这个按钮的特殊之处在于它没有hwnd值。(hwnd: 0x0)。这是我试图获得对该按钮的引用的内容: 在本例中,hwnd变量是包含按钮的应用程序主窗口的句柄。 在阅读FindWindowEx的留档时,看起来似乎有多种方法可以使用不同的参数,我觉得我已经尝试

  • 我到处寻找,并尽一切努力从SQLQueriesRadion按钮获取选定的mySQL值。 这是我的密码 谢啦

  • 问题内容: 我搜寻了互联网,但找不到答案: 我正在使用for循环创建36个名为a1,a2等的按钮,并同时为每个按钮分配一个唯一的操作命令。 稍后,我想从actionPerformed(ActionEvent e)方法中获取按钮的名称。 我可以使ActionCommand非常容易,但是我也需要按钮的名称。 任何帮助,不胜感激! 编辑: 这是我正在使用的代码: 这为6x6网格提供了36个按钮,分别为a

  • 问题内容: 我已经在单选组中动态创建了两个单选按钮,并选中其中一个。我需要在我按下另一个按钮时将其值保存在字符串中。但是我已经为此实现了,但是第一次没有用。这是我的代码。 问题答案: xml文件 Java代码

  • 我有一个窗口,每个类型有不同的项目和不同的视图。我想从内容控件后面的代码访问窗口(父)中的按钮。 OutputConfigView: OutputRenderReview.xaml.cs: 在本例中,parentWindow为空。 如何从控件后面的代码访问调用窗口的按钮?