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

问题描述

我要提出申请,以拦截在我的应用程序的所有形式的所有UI事件,并写入到一个日志。这些数据可以不是被用来查看哪些控制是最常用的,以什么顺序等。问题是,我想这自动发生,而无需修改现有的类。

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.

在此先感谢!

推荐答案

您可以安装一个message过滤。

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

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 09:36