Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
How can we count how many times a mouse click hsa been made?
Posted

I'd prefer doing this via IMessageFilter interface and Application.AddMessageFilter. Please refer to this sample implementation:

C#
public class MessageFilter : IMessageFilter
{
    long countMouse;
    Form1 form;
    public MessageFilter(Form1 form)
    {
        countMouse = 0;
        this.form = form;
    }
    private const int WM_LBUTTONDOWN = 0x201;
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDOWN)
        {
            countMouse++;
            form.label1.Text = String.Format("Clicks: {0}", countMouse);
        }
        return false;
    }
}


in the Program code where the Application is setup do this:

XML
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 form = new Form1();
        Application.AddMessageFilter(new MessageFilter(form));
        Application.Run(form);
    }
}


Please take note that this is only for demonstration purposes. I'd never make a Label public as it needs to be in this simple example, but it's enough for a simple demonstration. The filter will be called even when you click on however deeply nested controls. The message filter should also be unaware of the concrete form implementation. This was also done only for demonstration purposes.

Happy programming and cheers!

-MRB
 
Share this answer
 
v2
Comments
JustWorking 7-Jun-11 9:31am    
Nice code, I will try it and get back to you within 24 hours.
If you feel my question was good mark it Good and vote.
Manfred Rudolf Bihy 7-Jun-11 9:48am    
I'll just bookmark it for now. Please add Winform tag and be a little more specific on what mouse clicks you wanted counted. All on a form or just a certain control or collection of controls. The question is a little to broad to give a specific solution. My solution is quite broad as well, but as you refine your question I'll be able to get more specific also. :)

Cheers!
Kim Togo 7-Jun-11 9:44am    
Good answer Manfred, my 5.
Manfred Rudolf Bihy 7-Jun-11 9:49am    
Thanks Kim!
Sandeep Mewara 7-Jun-11 11:23am    
My 5++! :)
You can use the MouseDown event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown.aspx[^]

Add a static counter inside the event or a private on form level and increase the counter in mousedown.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 7-Jun-11 9:23am    
My vote of 3:
You'll have to improve a bit on your solution as you'd have to handle the mouse down on every control inside your form. Please add something that will enable the capture of all mousedown events in that form.
Kim Togo 7-Jun-11 9:44am    
Good thinking :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900