简体   繁体   English

在其他应用程序中捕获按钮单击事件

[英]Catching the button click event in other application

I want to catch the click event of the button which is clicked on Windows Form Application lets say in Application A and I want to catch it in Application B which is also a Windows Form Application. 我想捕获在Windows Form Application中单击的按钮的click事件,在应用程序A中说,我想在也是Windows Form Application的Application B中捕获它。

I have created both the Applications and added the button in Application A. Now when the button is clicked in Application A, I want to catch the event in Application B. 我已经创建了两个应用程序,并在应用程序A中添加了按钮。现在在应用程序A中单击按钮时,我想在应用程序B中捕获事件。

Please guide. 请指导。

There isn't a "quick" way to share .NET events between different processes. 没有在不同进程之间共享.NET事件的“快速”方法。 You need to catch the event in the same application which raised it, implement an inter-process communication mechanism (WCF, socket, ...) between Application A and application B and use it to send the event data from A to B. 您需要在引发该事件的同一应用程序中捕获该事件,在应用程序A与应用程序B之间实现进程间通信机制(WCF,套接字等),并使用它将事件数据从A发送到B。

See this question and related answers for more details: Listen for events in another application 有关更多详细信息,请参见此问题和相关答案: 侦听另一个应用程序中的事件

You can use SetWinEventHook or a WH_MOUSE_LL hook (P/Invoke) 您可以使用SetWinEventHookWH_MOUSE_LL钩(P / Invoke的)

(tested on Windows 10, with VS 2015) (已在Windows 10和VS 2015上测试)

(also IUIAutomationEventHandler but more complicated) (也是IUIAutomationEventHandler,但更复杂)

I can explain you, how you may catch the Button Click event a little bit otherwise. 我可以向您解释,否则您将如何捕获Button Click事件。

You can create the instance of your 2nd Form in your first one or in a reachable Class, then calling a Public Method in your 2nd Form or in your class, which does what you want. 您可以在第一个窗体或可访问的类中创建第二个窗体的实例,然后在您的第二个窗体或您的类中调用所需的公共方法。

Example Form 1: 示例表格1:

var _secondForm = new Form2();

private void Form1_Load(object sender, EventArgs e)
{
    _secondForm.Open();
}

private void button1_Click(object sender, EventArgs e)
{
    MethodInSecondForm()
}

and your Second Form: 和您的第二种形式:

public void MethodInSecondForm()
{
    // Event here
}

You can create this instance in a Class, too and make it easier. 您也可以在Class中创建此实例,并使其更容易。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM