以编程方式单击按钮 C#
Click a button programmatically C#
我正在寻找一种以编程方式单击按钮的方法 (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 是私有的,不能对其进行任何更改function/project
你有如下几个选项
1- button1.PerformClick();
2- button1_Click(this, EventArgs.Empty);
3- button1(null, null)
原帖:
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自动化。 UIA 本质上允许您从 GUI 的角度自动化另一个应用程序(而不是通过 COM/Ole 自动化在幕后说),无论是否你只是想驱动另一个应用程序,比如点击一个按钮,或者在 QA 中执行 full-on 自动化测试。
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...
此代码将单击包含文本 “单击我” 的按钮,该按钮位于标题为 “目标演示” 的 window 中.请注意,我没有提及任何特定的 object 类型;回调或 GUI 技术(适用于本机;WinForms;WPF 和网络)
//
// 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();
}
好处
- 目标应用所需的零更改
- 不用担心找到 window 个句柄
- 不用担心尝试向应用程序的消息泵发送 BN_CLICK 通知,希望它能正常工作
- 适用于 C++ 应用程序;窗体;工作框架;网页
我正在寻找一种以编程方式单击按钮的方法 (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 是私有的,不能对其进行任何更改function/project
你有如下几个选项
1- button1.PerformClick();
2- button1_Click(this, EventArgs.Empty);
3- button1(null, null)
原帖:
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自动化。 UIA 本质上允许您从 GUI 的角度自动化另一个应用程序(而不是通过 COM/Ole 自动化在幕后说),无论是否你只是想驱动另一个应用程序,比如点击一个按钮,或者在 QA 中执行 full-on 自动化测试。
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...
此代码将单击包含文本 “单击我” 的按钮,该按钮位于标题为 “目标演示” 的 window 中.请注意,我没有提及任何特定的 object 类型;回调或 GUI 技术(适用于本机;WinForms;WPF 和网络)
//
// 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();
}
好处
- 目标应用所需的零更改
- 不用担心找到 window 个句柄
- 不用担心尝试向应用程序的消息泵发送 BN_CLICK 通知,希望它能正常工作
- 适用于 C++ 应用程序;窗体;工作框架;网页