拦截C#(WinForms)应用程序中所有控件的点击事件 [英] Intercepting click event for all controls in an app in C# (WinForms)

查看:896
本文介绍了拦截C#(WinForms)应用程序中所有控件的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个应用程序拦截我应用程序的所有形式的所有UI事件并将其写入日志。这个数据可以用于查看哪些控件是最常用的,按什么顺序等等。问题是我希望这可以自动发生,而不需要修改现有的类。



我制作了一个原型,将一个方法附加到窗体中所有控件的点击事件,但是如何才能对所有窗体进行操作?在操作事件时,反射需要一个目标对象,但只有启动窗体才能轻松访问。



有没有办法挂钩对象的构造函数?然后我可以注入我的方法在所有的新形式的事件。或者也许有另一种方法来做到这一点。



提前感谢

解决方案>

您可以安装消息过滤器



消息过滤器是一个实现 IMessageFilter 的对象。 WinForms为通过线程的消息循环的每个消息调用您的 PreFilterMessage 方法。这足以监控整个应用程序的用户输入(并提供操纵它的选项)。


I want to make an application to intercept all UI events in all the forms of my application and to write them to a log. This data can than be used to see which controls are the most used, in what order, etc. The problem is that I want this to happen automatically, without modifying the existing classes.

I made a prototype that attaches a method to a click event for all controls in a form, but how can this be done for all forms? Reflection needs a target object when manipulating events, but only the startup form can be easily accessed.

Is there a way to hook the constructor of an object? Then I could "inject" my method in all the events of the new form. Or maybe there is another way to do this.

Thanks in advance!

解决方案

You can install a message filter.

A message filter is an object that implements IMessageFilter. WinForms calls your PreFilterMessage method for every message that passes through your thread's message loop. This is enough to monitor user input across the application (and gives you the option of manipulating it).

这篇关于拦截C#(WinForms)应用程序中所有控件的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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