Windows message Filtering

Source: Internet
Author: User

In C # programming, you will often encounter scenarios such as preventing the mouse from dragging the form, enabling certain shortcut keys, prohibiting mouse movement, and so on. These requirements can be met through the form of MouseMove events, Ondragdrop,onmove and other events to solve the problem,

However, the disadvantage of this approach is that it can only work on the current form or control, and it will not work if the form or control is overwritten. We often encounter a situation where there are many controls on the form, and this section describes how to do this by capturing Windows messages.

Generally speaking, there are two ways to implement this function,

1. Implemented by overriding WndProc (ref Message m), the method signature is as follows:

protected override void WndProc (ref Message m)
{
Base. WndProc (ref m);
}

The message contains the following fields, which are related to Windows messages,

Public IntPtr LParam {get; set;}

public int MSG {get; set;} Gets or sets the ID number of the message.

Public IntPtr WParam {get; set;}

Such as:

If we want to disable the drag and drop of the form, the code is as follows:

const int wm_nclbuttondown = 0x00A1;      const int htcaption = 2; protected override void WndProc (ref Message m) {if (m.msg = = Wm_nclbuttondown && m.wparam.toint               () = = htcaption) return; Base.      WndProc (ref m); }

2. Implement the System.Windows.Forms.IMessageFilter interface implementation through the inheritance interface, the interface declaration is as follows:

public interface IMessageFilter
{
Summary:
Filter the messages before they are dispatched.
//
Parameters:
M:
The message to dispatch. This message cannot be modified.
//
return Result:
True if the message is filtered and the message is prevented from being dispatched, or false if the message is allowed to continue to reach the next filter or control.
BOOL Prefiltermessage (ref Message m);
}

Also, for example, to disable dragging a form, implement the MessageFilter class as follows:

public class MessageFilter:System.Windows.Forms.IMessageFilter {const int wm_nclbuttondown = 0x00a1;//When the cursor is in the window's         This message is sent when the non-client area is pressed with the left mouse button const int htcaption = 2; public bool Prefiltermessage (ref System.Windows.Forms.Message m) {if (m.msg = = Wm_nclbuttondown &&A mp            M.wparam.toint32 () = = htcaption) return true;        return false; }    }

  After creating this class, create an object and add the object to the application, such as the following code, the following code is the entry method in the program file   static class program     {        private static MessageFilter filter = new MessageFilter ();         ///<summary>         ///The main entry point of the application.         ///</summary>         [stathread]        static void Main ()          {             Application.addmessagefilter (filter);             Application.enablevisualstyles ();             Application.setcompatibletextrenderingdefault (False);           &nbsP; application.run (New MainFrm ());        }     }  

Note: When implementing the IMessageFilter interface, be sure to pay attention to its return value, which is true if the message is filtered and the message is prevented from being dispatched, or false if the message continues to reach the next filter or control. ] , after the interception of the message processing, it is important to pay attention to the return value of processing, if the message is not processed, be sure to return to false, let other controls to process the message.

For Windows messages, please refer to the article http://www.cnblogs.com/lenmom/p/3730179.html

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.