Monitor mouse click left or right button

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;

Namespace SysInfo
{
public partial class Frm_main:form
{
MessageFilter MF = new MessageFilter ();

Public Frm_main ()
{
InitializeComponent ();
}
Form Load Message Filter
private void Frm_main_load (object sender, EventArgs e)
{
Add message filters to monitor these messages when a Windows message is delivered to the destination
Application.addmessagefilter (MF);
}
Remove a message filter from a form
private void Frm_main_formclosing (object sender, FormClosingEventArgs e)
{
Remove a message filter from the application's message pump
Application.removemessagefilter (MF);
}

Method One, rewrite WndProc virtual method, and method two cannot exist simultaneously
protected override void WndProc (ref Message m)
{
Switch (M.MSG)//Determine the ID number of the system message
{
Case 513:
MessageBox.Show ("Click the left mouse button! "," System Information ");
M.result = (INTPTR) 0;//The value returned to Windows in response to message processing
Break
Case 516:
MessageBox.Show ("right mouse click!") "," System Information ");
M.result = (INTPTR) 0;//The value returned to Windows in response to message processing
Break
Default
Base. WndProc (ref m);
Break
}
}
}

Method Two, implement the IMessageFilter interface to obtain Windows messages, and method one cannot exist simultaneously
public class Messagefilter:imessagefilter
{
public bool Prefiltermessage (ref message message)
{
Switch (message. MSG)//Determine the ID number of the system message
{
Case 513:
MessageBox.Show ("Click the left mouse button! "," System Information ");
return true;
Case 516:
MessageBox.Show ("right mouse click!") "," System Information ");
return true;
Default
return false;
}
}
}
}

Monitor mouse click left or right button

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.