c# - 以编程方式单击按钮 C#

标签 c# winforms reflection buttonclick findwindow

我正在寻找一种以编程方式单击按钮的方法(Winform)。我的项目有一个表单(例如 Form2)和按钮(Button2)。当我单击按钮时,它应该单击 Button1 (位于另一个项目中),但两者都在同一解决方案中。 这就是我尝试做的:

private void button2(object sender, EventArgs e)
{

    System.IntPtr hwnd;

    System.IntPtr hwndChild = IntPtr.Zero;

    hwnd = FindWindow(null, "form1");

    if (hwnd.Equals(IntPtr.Zero))
    {
     MessageBox.Show("Couldn't find the form");
    }
    else
    {
    hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, null, "BUTTON1");

    if(!hwndChild.Equals(IntPtr.Zero))
    SendMessage(hwndChild, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
    }
}

我应该使用任何其他方法(例如反射)来代替这种方法吗?

P.S:Button1 是私有(private)的,不能对该功能/项目进行任何更改

最佳答案

操作:

I am looking for a way to click a button programatically (Winform).

Button1 and Button2 are in two different process. I am not supposed to make any changes to the code containing button1.

当然,不需要对目标应用进行任何更改的最简单方法是使用Windows UI Automation。 UIA 本质上允许您从 GUI 角度自动化另一个应用程序(而不是说通过 COM/Ole 自动化在幕后),无论您是否只想驱动另一个应用程序,例如单击按钮或执行QA 中的全面自动化测试。

MSDN:

Microsoft UI Automation is an accessibility framework that enables Windows applications to provide and consume programmatic information about user interfaces (UIs). more...

...和:

UIA can be used to automate native windows applications , WPF applications as well as Silverlight applications. more...

此代码将单击标题为“目标演示”的窗口中包含文本“Click Me”的按钮。注意我没有引用任何特定的对象类型;回调或 GUI 技术(这适用于 native 、WinForms、WPF 和 Web)

//
// This is the handler in the ButtonClicker app, the app that is doing the clicking
//
private void go_Click(object sender, EventArgs e)
{
    // Look for a top-level window/application called "Target Demo"
    var root = AutomationElement.RootElement;
    var condition1 = new PropertyCondition(AutomationElement.NameProperty, "Target Demo");

    var treeWalker = new TreeWalker(condition1);
    AutomationElement element = treeWalker.GetFirstChild(root);
    
    if (element == null)
    {
        // not found
        return;
    }

    // great!  window found
    var window = element;

    // now look for a button with the text "Click Me"
    var buttonElement =window.FindFirst(TreeScope.Children, 
        new PropertyCondition(AutomationElement.NameProperty, "Click Me"));
    if (buttonElement == null)
    {
        // not found
        return;
    }

    // great! now click the button
    var invokePattern = buttonElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    invokePattern?.Invoke();

}

好处

  • 定位应用所需的零更改
  • 无需费力查找窗口句柄
  • 无需费力尝试向应用的消息泵发送 BN_CLICK 通知,祈祷它能正常工作
  • 适用于 C++ 应用程序; WinForms; WPF;网络

关于c# - 以编程方式单击按钮 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41961339/

相关文章:

c# - 如何在 MVC 3 中使用多个布局?

c# - 可能的 GetObjectsOfType 替换

c# - 使用 C# 在启动时加载字典

c# - 水平滚动条在 winforms 用户控件中不起作用

c# - 加载程序集、查找类和调用 Run() 方法的正确方法

c# - 在没有 generic.xaml 的情况下创建自定义控件?

c# - 参数异常 : method arguments are incompatible

c# - 如何在 C# 中创建多个对象?

c# - 从动态类型创建泛型类的实例

java - 反射 - 继承的字段全部为 null 或为空