Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercepting click event for all controls in an app in C# (WinForms)

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!

like image 341
Gratian Lup Avatar asked Nov 25 '10 17:11

Gratian Lup


1 Answers

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).

like image 159
Tim Robinson Avatar answered Sep 28 '22 07:09

Tim Robinson