C # message processing method

zhaozj2021-02-11  175

In C #, the driver adopted by the program uses event drivers instead of the original message driver, although the .NET framework provides the events provided by the .NET framework, it is very rich in the previous system. Implementation method, so use messages in C # sometimes greatly improved programming efficiency.

1 define a message

Messages in C # need to be defined as the original 16 credit number in the Windows system, such as

Const int WM_LBUTTON = 0x201; // Defines the left button of the mouse click message

Public const int user = 0x0400 // is a user message defined by the Windows system

2 messages

Message Send is defined by the API function sendMessage provided by Windows.

[DllImport ( "User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage (int hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter int lParam // second Message parameter;

3 messages

In C #, all windows have also messages reception processing functions, that is, DEFPROC functions

You can overload this function in the Form to process messages

Protected Override Void DefWndProc (Ref System.winForms.Message M)

{

Switch (m.msg)

{

Case WM_LButton:

/// String is different from the method of usage of cstring in the MFC.

String message = string.format ("Received Message! Parameters: {0}, {1}", M.WParam, M.LParam;

MessageBox.show (Message); // / Display a message box

Break;

DEFAULT:

Base.defwndproc (REF M); // Call the base class function to handle non-custom messages.

Break;

}

}

In fact, the events in the C # are also implemented by packaging system messages. If you do not process it in the DEFWndProc function

Then, he will give the system to process the message, the system will pass the agent to implement the processing function of the mouse click, so you can pass

DEFPROC function to block messages, such as you want to intercept a button click message

4 other messaging methods in C #

In C #, you need to preprocess the message's message, such as you use the OWC's Spreedsheet control to process the Excel file, you don't want users to choose casually

The data is edited, you can block the mouse event, this time you must intercept the system's pre-defined event (this is called subcatenification in the MFC), you can provide one of the interfaces provided via C #

IMessageFilter to implement the filter filter

Public Class Form1: System.Windows.Forms.form, iMessageFilter

{

Const int WM_MOUSEMOVE = 0x200

Public Bool PrefilterMessage (Ref Message M) {Keys Keycode = (int) m.wparam & keys.keycode; if (m.msg == m.msg == wm_mousemove) //||m.msg == wm_lbuttondown {//Messagebox.show("ignoring escape ... "); return true;}}}}}

转载请注明原文地址:https://www.9cbs.com/read-5684.html

New Post(0)