C# 如何从外部.NET应用程序订阅.NET控件事件?
我的公司有很多产品是用C#.NET Windows窗体编写的。世界上不同的团队管理不同的产品,所以我没有权利对主机应用程序进行重大更改,但可以进行一些小的更改C# 如何从外部.NET应用程序订阅.NET控件事件?,c#,.net,winforms,events,C#,.net,Winforms,Events,我的公司有很多产品是用C#.NET Windows窗体编写的。世界上不同的团队管理不同的产品,所以我没有权利对主机应用程序进行重大更改,但可以进行一些小的更改 我想编写一个通用库,给定一个控件句柄,它可以订阅该控件的所有事件。由于主机应用程序和库都在C#.NET中,我希望有一种非常简单的方法来实现它。根据评论讨论,为了满足您问题的要求,您最好的选择可能是NativeWindow解决方案 然而,我仍然建议你和你的同事谈谈,看看是否有另一种方法可以让你的工作更轻松如果您根据同事提供的解决方案为客户创
我想编写一个通用库,给定一个控件句柄,它可以订阅该控件的所有事件。由于主机应用程序和库都在C#.NET中,我希望有一种非常简单的方法来实现它。根据评论讨论,为了满足您问题的要求,您最好的选择可能是
NativeWindow
解决方案
然而,我仍然建议你和你的同事谈谈,看看是否有另一种方法可以让你的工作更轻松如果您根据同事提供的解决方案为客户创建解决方案,则很有可能在同事部署最新更新时,您必须经常重写自定义解决方案…
这取决于你的成就。是关于在应用程序之间共享数据的吗?这可能不起作用,因为
控件。FromHandle
只能从当前AppDomain
返回控件。AFAIK的最佳选择是将这些控件捕获为NativeWindow
对象,并截取它们的WndProc
。这不是一个很好的方法。。与@Rotem所说的相同:您可以使用WndProc和hooks@wes如果您对某些控件有句柄
,您可以使用NativeWindow
截取消息和fire
相应的事件。为什么你认为这不是一个很好的方法?当然,这不是一种原生的方式,而是一种.NET
方式。使用native
方式,您必须使用GetWindowLong
和SetWindowLong
来更改控件/窗口的默认WindowProc
。所谓“漂亮”,我的意思是它不像使用DLL或类和重写方法那么简单。NativeWindow肯定比本机方法更易于使用(就初始设置而言)。一般来说,特别是在同一个组织内,似乎应该有更好的方法,而不是上述解决方案
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace NativeWindowApplication
{
// Summary description for Form1.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class Form1 : System.Windows.Forms.Form
{
private MyNativeWindowListener nwl;
private MyNativeWindow nw;
internal void ApplicationActivated(bool ApplicationActivated)
{
// The application has been activated or deactivated
System.Diagnostics.Debug.WriteLine("Application Active = " + ApplicationActivated.ToString());
}
private Form1()
{
this.Size = new System.Drawing.Size(300, 300);
this.Text = "Form1";
nwl = new MyNativeWindowListener(this);
nw = new MyNativeWindow(this);
}
// The main entry point for the application.
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
// NativeWindow class to listen to operating system messages.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
internal class MyNativeWindowListener : NativeWindow
{
// Constant value was found in the "windows.h" header file.
private const int WM_ACTIVATEAPP = 0x001C;
private Form1 parent;
public MyNativeWindowListener(Form1 parent)
{
parent.HandleCreated += new EventHandler(this.OnHandleCreated);
parent.HandleDestroyed += new EventHandler(this.OnHandleDestroyed);
this.parent = parent;
}
// Listen for the control's window creation and then hook into it.
internal void OnHandleCreated(object sender, EventArgs e)
{
// Window is now created, assign handle to NativeWindow.
AssignHandle(((Form1)sender).Handle);
}
internal void OnHandleDestroyed(object sender, EventArgs e)
{
// Window was destroyed, release hook.
ReleaseHandle();
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
// Listen for operating system messages
switch (m.Msg)
{
case WM_ACTIVATEAPP:
// Notify the form that this message was received.
// Application is activated or deactivated,
// based upon the WParam parameter.
parent.ApplicationActivated(((int)m.WParam != 0));
break;
}
base.WndProc(ref m);
}
}
// MyNativeWindow class to create a window given a class name.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
internal class MyNativeWindow : NativeWindow
{
// Constant values were found in the "windows.h" header file.
private const int WS_CHILD = 0x40000000,
WS_VISIBLE = 0x10000000,
WM_ACTIVATEAPP = 0x001C;
private int windowHandle;
public MyNativeWindow(Form parent)
{
CreateParams cp = new CreateParams();
// Fill in the CreateParams details.
cp.Caption = "Click here";
cp.ClassName = "Button";
// Set the position on the form
cp.X = 100;
cp.Y = 100;
cp.Height = 100;
cp.Width = 100;
// Specify the form as the parent.
cp.Parent = parent.Handle;
// Create as a child of the specified parent
cp.Style = WS_CHILD | WS_VISIBLE;
// Create the actual window
this.CreateHandle(cp);
}
// Listen to when the handle changes to keep the variable in sync
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void OnHandleChange()
{
windowHandle = (int)this.Handle;
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
// Listen for messages that are sent to the button window. Some messages are sent
// to the parent window instead of the button's window.
switch (m.Msg)
{
case WM_ACTIVATEAPP:
// Do something here in response to messages
break;
}
base.WndProc(ref m);
}
}
}