简体   繁体   English

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

[英]How do I get a button handle from Windows10Forms in C#?

I've been trying to find the handle for a particular help button in a program and then send a BN_CLICK message to it.我一直试图在程序中找到特定帮助按钮的句柄,然后向它发送 BN_CLICK 消息。 To debug, I looked at the handles for the parent window and the button.为了调试,我查看了父窗口和按钮的句柄。

[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);
}

The debug returns a number for the hWndParent but 0 for button.调试为 hWndParent 返回一个数字,但为按钮返回 0。 I got the classes from Spy++.我从 Spy++ 那里得到了课程。 1 1

This might be complicated by the fact that there are two "Help" buttons in the application with the same class.由于应用程序中有两个具有相同类的“帮助”按钮,这可能会变得复杂。 Here is a picture of the application window I am trying to get handles for with the help button I want to click highlighted with a red box.这是我尝试使用帮助按钮获取句柄的应用程序窗口的图片,我想单击用红色框突出显示。 2 2

I tried adding on the instance number which I obtained through AutoIT Info.我尝试添加通过 AutoIT Info 获得的实例编号。

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

This also returned a 0 for button, as did replacing "Help" with null.这也为按钮返回了 0,就像用 null 替换“Help”一样。 If anyone is familiar with getting handles from Windows 10 Forms and knows how to do this, your help would be much appreciated.如果有人熟悉从 Windows 10 窗体获取句柄并知道如何执行此操作,我们将不胜感激。 Thanks!谢谢!

Andrew安德鲁

Thanks to Hans for his post - it pointed me in the right direction.感谢 Hans 的帖子 - 它为我指明了正确的方向。 I've gone with using Selenium and Appium to automate clicks/input.我已经使用 Selenium 和 Appium 来自动化点击/输入。 For anyone that stumbles across this in future looking for a solution, here is some code to help.对于将来在寻找解决方案时偶然发现的任何人,这里有一些代码可以提供帮助。

// 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();

To find the AccessibilityId, I found the easiest tool to use was AutoIT Window Info .为了找到 AccessibilityId,我发现最容易使用的工具是AutoIT Window Info Drag the finder over your desired button.将取景器拖到所需的按钮上。 Then in the summary tab your AccessibilityId is labeled simply as "ID".然后在摘要选项卡中,您的 AccessibilityId 被简单地标记为“ID”。 I went with AccessibilityId because I have multiple buttons with the same name in the app I wanted to control.我选择 AccessibilityId 是因为我想控制的应用程序中有多个同名按钮。

You will need to have WinAppDriver installed and put the proper location into the code.您需要安装WinAppDriver并将正确的位置放入代码中。 Appium and Selenium can be added through the Nuget Manager but a few of the functions used in my code are depricated. Appium 和 Selenium 可以通过 Nuget 管理器添加,但我的代码中使用的一些功能已被弃用。 I just went with the 3.0.1 versions of Selenium.WebDriver and Selenium.Support and the 3.0.0.1 version of Appium.WebDriver (others may work, these were just the first to work).我刚刚使用了 Selenium.WebDriver 和 Selenium.Support 的 3.0.1 版本以及 Appium.WebDriver 的 3.0.0.1 版本(其他人可能会工作,这些只是第一个工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从 web api 获取数据到 ac# windows 窗体应用程序类 - How do i get data from a web api into a c# windows forms app class 如何在Visual C#或C ++中正确处理Windows 10应用程序的暂停? - How do I handle suspension of Windows 10 apps correctly in Visual C# or C++? C#,如何在Windows Powershell(Windows 10)上配置Mono? (环境变量PATH) - C#, How Do I Get Mono Configured on Windows Powershell (Windows 10)? (Environmental Variable PATH) 如何以编程方式从富文本框编译 C# Windows Forms 应用程序? C# - How do I programmatically compile a C# Windows Forms App from a richtextbox? C# C#如何在Windows Forms App中使用方法? - c# how do I use a method in a Windows Forms App? 如何获得命令控制台在Windows Forms应用程序中写行? (Visual C#) - How Do I Get The Command Console To Write Lines In Windows Forms Application? (Visual c#) 如何将列表框中的数据添加到C#Windows窗体的SQL Server数据库中? - How do I add data from ListBox into SQL Server database in C# Windows forms? 如何将Windows窗体应用程序从C#转换为VB? - How do I convert a windows forms app from C# to VB? 如何使用 Windows Forms 从 C# 创建 EXE 文件? - How do I create an EXE file from C# using Windows Forms? 我该怎么办水平菜单C#Windows窗体 - How can I do horizontal Menu C# Windows Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM