Digitally custom message (on)

zhaozj2021-02-16  52

Abstract: Since I sent 5 articles about the news, some netizens came to the letter hoping that I would like to tell some more practical message mechanisms. Here I want to make a comprehensive discussion on the user's custom message, I hope to relieve your heart.

First, a normal custom message method. Based on the scope of the message value mentioned in the previous article, I know the scope of the user's custom message, but although it starts from WM_USER, it is generally in our project. There are still many other controls, they also take some of the WM_USER message range, so we have to leave a part of the scope for them. Here, we retain 100 messages, in general, this can meet our requirements. (1) Define the value of the message. In the place where we want to have a message (such as CMYVIEW.CPP's start part), the following work is not very simple (WM_USER 100), we have said in the previous, and there are 3 parts of the news must be coordinated. : Message declaration, message mapping, message body. We will do it in hand. (2) First, add a message declaration in the AFX_MSG block: In CMYVIEW.H, find the following section, and join the message declaration: protected: // {{AFX_MSG (CMYVIEW) ... AFX_MSG LRESULT OnMYMSG (WParam WPARAM, LPARAM lParam); file: //}} AFX_MSG (3) was added in MESSAGE_MAP ON_MESSAGE macro block: BEGIN_MESSAGE_MAP (CMyView, CView) file: // {{AFX_MSG_MAP (CMyView) ..... ON_MESSAGE (WM_MSG_1, OnMyMsg) File: //}} AFX_MSG_MAP END_MESSAGE_MAP () (4) Add Message Function: LpeSult Cmyview :: OnMymsg (WPARAM WPARAM, LPARAM LPARAM) {AFXMESSAGEBOX ("Message has been received!"); return 0;} After the definition is complete, we can activate the message, you can use the postMessage / SendMessage, all OK! Second, custom message block message block, mainly to handle multiple messages, However, there is a request for the message: The value of the message must be continuous. The advantage of this is: Simplifying classes, can complete a set of messages through a function. Generally, there are four Message Block: ON_CONTROL_RANGE, ON_NOTIFY_RANGE, ON_COMMAND_RANGE and ON_UPDATE_COMMAND_UI_RANGE, their use almost, I would find a more commonly used to talk about it: ON_COMMAND_RANGE and ON_UPDATE_COMMAND_UI_RANGE steps are as follows: 1. Use the command to receive all the macro range of macro ON_COMMAND_RANGE ID command message (WM_Command message) within a range (WM_COMMAND message). Using this macro can handle multiple commands in a function.

1) Define command processing functions after class. H file {}} so that it is not subject to CL0AssWizard. Protected: file: // {{AFX_MSG (CTestView) File: //}} AFX_MSG AFX_MSG VOID OnTestCommandRange (UINT NID); DECLARE_MESSAGE_MAP () 2) After {{}}, add an ON_COMMAND_RANGE macro to class message image. The first two parameters define the range of the command ID to be processed. These IDs have a sequence size, and the last ID is higher than the first. The last parameter is the name of the message processing function defined in the first step. BEGIN_MESSAGE_MAP (CTestView, CView) file: // {{AFX_MSG_MAP (CTestView) file: //}} AFX_MSG_MAP ON_COMMAND_RANGE (ID_TEST_1, ID_TEST_4, OnTestCommandRange) END_MESSAGE_MAP () 3) add a message handler function with the following statement. The parameter NID is the ID to handle the command. void CTestView :: OnTestCommandRange (UINT nID) {switch (nID) {case ID_TEST_1: break; case ID_TEST_2: break; case ID_TEST_3: break; case ID_TEST_4: break;}} 2. using the user interface of the macro command taken ON_UPDATE_COMMAND_UI_RANGE macro range Update the request for the user interface in the message. With this macro, you can enable all menu commands or process a set of toolbar buttons. 1) Define the user interface command processing function after {{}} of the class. So that it is not subject to ClassWizard. protected: file: // {{AFX_MSG (CTestView) file: //}} AFX_MSG afx_msg void OnUpdateTestCommandRange (CCmdUI * pCCmdUI); 2) Similarly, following {} {}, is added to the command macro image ON_UPDATE_COMMAND_UI_RANGE user class. The first two parameters define the range of messages I d to be processed. These IDs have a sequence size, and the last ID value is larger than the first. The last parameter is the name of the user interface command processing function defined in the first step.

BEGIN_MESSAGE_MAP (CTestView, CView) file: // {{AFX_MSG_MAP (CTestView) file: //}} AFX_MSG_MAP ON_UPDATE_COMMAND_UI_RANGE (ID_TEST_1, ID_TEST_4, OnUpdateTestCommandRange) END_MESSAGE_MAP () 3) command message processing interface statement follows: void CTestView :: OnUpdateTestCommandRange ( CCMDUI * PCMDUI) {Switch (PCMDUI-> M_NID) {case ID_test_1: Break; case ID_test_2: pcmdui-> setradio (); break; case ID_test_3: Break; Case ID_Test_4: Break;}} is not finished

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

New Post(0)