C # intercept system messages

Source: Internet
Author: User

First, let's take a look at several methods to intercept system messages:


 
// 1. Intercept System messages
// Method 1:
// Add monitoring messages
Private void form_load (Object sender, system. eventargs E)
{
Application. addmessagefilter (this );
}
// Cancel message monitoring
Private void form_closing (Object sender, system. componentmodel. canceleventargs E)
{
Application. removemessagefilter (this );
}
// Intercept the message for processing
Public bool prefiltermessage (Ref system. Windows. Forms. Message m)
{
Switch (M. msg)
{
Case 513: // intercept the left-click event
MessageBox. Show ("Haha, you have clicked the left button to be intercepted! ");
Return true; // if true is returned, the message is removed and no longer processed by the system.
Case 516: // intercept the left-click event
MessageBox. Show ("Haha, you have right-clicked to be intercepted! ");
Return true; // if true is returned, the message is removed and no longer processed by the system.
Default:
Return false; // if false is returned, the message is not removed and the system will process it.
}
}
// Method 2:
// Intercept the message for processing
Protected override void wndproc (ref message m)
{
Switch (M. msg)
{
Case 17:
MessageBox. Show ("Haha, you cannot close the computer! ");
M. Result = (intptr) 0;
Break;
Case 513:
MessageBox. Show ("Haha, you cannot click the left button! ");
M. Result = (intptr) 0;
Break;
Case 516:
MessageBox. Show ("Haha, you cannot right-click it! ");
M. Result = (intptr) 0;
Break;
Default:
Base. wndproc (ref m );
Break;
}
}
 

The second method is used for the software written last time. Because I am used to it, I will use this method this time.
Now we need to rewrite this method, and we need to know the system message of USB flash drive insertion and unplugging.
Devicechange 0x219
Devicearrvie 0x8000
Deviceremove 0x8004

We intercepted devicechange and used the tostring () method:
MSG = 0x219 (wm_devicechange) hwnd = 0x50e54 wparam = 0x8000 lparam = 0x475e89c result = 0x0
That is to say, when a drive is loaded, the message wm_devicechange is sent. However, to determine whether the drive is loaded or uninstalled, you must determine whether the wparam field is devicearrive or devicemove. Therefore, we compile the following test code.
Program description:

Protected override void wndproc (ref message m) // monitor Windows messages
{
Const int wm_devicechange = 0x219;
Const int wm_devicearrvial = 0x8000; // if the value of M. MSG is 0x8000, a USB flash disk is inserted.
Const int wm_devicemovecomplet = 0x8004;
Switch (M. msg)
{
Case wm_devicechange:
{
Showdevicechanged ("wm_devicechange ");//
If (M. wparam. toint32 () = wm_devicearrvial)
Showdevicechanged ("wm_devicearrvial ");
Else if (M. wparam. toint32 () = wm_devicemovecomplete)
Showdevicechanged ("wm_devicemovecomplete ");
}
Break;
}
Base. wndproc (ref m); // transmits the system message from the wndproc of the parent class.
}

Public void showdevicechanged (string message)
{
Switch (Message)
{
Case "wm_devicechange ":
This. textbox_message.text + = "device changed \ r \ n ";
Break;
Case "wm_devicemovecomplete ":
This. textbox_message.text + = "device moved \ r \ n ";
Break;
Case "wm_devicearrvial ":
This. textbox_message.text + = "device arrived \ r \ n ";
Break;
}
}

C # intercept system messages

Related Article

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.