Win forms, register all clicks?

Is there a way to register all clicks in a Win Forms application? I would like to capture clicks and record the action and name of the control that caused it.

Is it possible?

Thanks in advance.

UPDATE: I am looking for a solution for a wide range of applications, is there no way to add a listener to the Windows event queue (or what is ever called)?

+5
source share
5 answers

You can do this by specifying the main form of your application on the IMessageFilter interface. You can display received Window messages and search for clicks. For instance:

  public partial class Form1 : Form, IMessageFilter {
    public Form1() {
      InitializeComponent();
      Application.AddMessageFilter(this);
      this.FormClosed += (o, e) => Application.RemoveMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m) {
      if (m.Msg == 0x201 || m.Msg == 0x203) {  // Trap left click + double-click
        string name = "Unknown";
        Control ctl = Control.FromHandle(m.HWnd);
        if (ctl != null) name = ctl.Name;
        Point pos = new Point(m.LParam.ToInt32());
        Console.WriteLine("Click {0} at {1}", name, pos);
      }
      return false;
    }
  }

Please note that this logs all clicks in any window of your application.

+5
source

, Click MouseDown .

0

MouseEventArgs :

    private  void Form_MouseDown(object sender, System.WinForms.MouseEventArgs e) 
{ 
switch (e.Button) 
{ 
    case MouseButtons.Left: 
    MessageBox.Show(this,"Left Button Click"); 
    break; 
    case MouseButtons.Right: 
    MessageBox.Show(this,"Right Button Click" ); 
    break; 
    case MouseButtons.Middle: 
    break; 
    default: 
    break; 
} 

EventLog.WriteEntry("source", e.X.ToString() + " " + e.Y.ToString()); //or your own Log function

} 
0

The NunitForms test project has a recorder application that monitors this and many other events. The code is very smart and worth a good look. This is a ThoughtWorks project.

What a roll royce solution though! ...

Try recursively go through a collection of form controls and a subscibe to a type-based event.

PK :-)

0
source

All Articles