以编程方式单击按钮 C# [英] Click a button programmatically C#

查看:72
本文介绍了以编程方式单击按钮 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a way to click a button programmatically (Winform). I have a form (say Form2) and button (Button2) of my project. When I click my button it should click Button1 (which is in another project), but both are in same solution. This is what I tried to do:

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

我应该使用任何其他方法(例如反射)而不是这种方法吗?

Should I use any other approach like reflection instead of this?

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

P.S: Button1 is private and no changes can be made to that function/project

推荐答案

OP:

我正在寻找一种以编程方式单击按钮的方法 (Winform).

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

Button1 和 Button2 处于两个不同的进程中.我不应该对包含 button1 的代码进行任何更改.

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

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

Sure, the easiest thing to do that does not require any changes to the target app is to use Windows UI Automation. UIA essentially allows you to automate another app from the GUI perspective (instead of say behind the scenes via COM/Ole Automation) regardless of whether you just want to drive another app, say click a button, or perform full-on automation testing in QA.

MSDN:

Microsoft UI 自动化是一个辅助功能框架,它使 Windows 应用程序能够提供和使用有关用户界面 (UI) 的编程信息.更多...

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

...和:

UIA 可用于自动化本机 Windows 应用程序、WPF 应用程序以及 Silverlight 应用程序.更多...

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

此代码将在标题为 Target Demo" 的窗口中单击包含文本 Click Me" 的按钮.注意我没有引用任何特定的对象类型;回调或 GUI 技术(这适用于本机;WinForms;WPF 和 Web)

This code will click a button containing the text "Click Me" in a window captioned "Target Demo". Note I do not make any reference to any particular object type; callback or GUI technology (this works for native; WinForms; WPF and 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆