How to trigger the behavior of the right-click menu

xiaoxiao2021-03-06  38

Although this is a very simple operation, it costs me a lot of time. It is mainly what I have not sufficiently understanding the news mechanism. I finally finally found similar example in CodeProject, I also slammed the plug, write The purpose of this article is a summary of the previous work, but also to share some experiences and everyone, avoid rearward. To achieve the features mentioned in the title, we must first understand the WINDOWS message mechanism. After reading an article specializing in the message mechanism, it feels good, put it out of this important part as this paper (this article is from Thunder. "Taking MFC tutorials with me" is really good. Most of this article is to quote the description of the news from the tutorial. Of course, I have added some own understanding), this article is divided into three parts, the first part tells the news Basic concept, the second part tells how to add a custom message, the third part tells how to implement the right-key menu action, if you don't know much about the implementation of this article, you can look at the part of this article, you will have reward. First, the concept message of the message is simple to say that, by entering the device to send an instruction to execute a certain action (the popular point is similar to someone wants to do a certain job, he will give the person in charge say hello, tell him Do the corresponding work, then this greetings are messages, and the corresponding work is the message processing function). In the SDK, the message is actually very easy to understand. When the window is established, there will be a function (window handler) to start executing a message loop, we can clearly see the thread of the message processing. A Switch Case statement can get it, the message is loop until the WM_QUIT message will end, and the remaining messages are intercepted after calling the corresponding processing function. But in the MFC encapsulated the API, the news seems to be a bit complex. We can't see the familiar Switch Case statement, and it is a thing that is called a message map. Why is the MFC to introduce a message mapping mechanism, you can imagine, in the current program development activity, your program has multiple forms, the main window is only one, the menu, toolbar, controls These are sub-windows , Then how many Switch Case we need to write, and also assign a message handler for each message, how complicated this is. Therefore, MFC uses a new mechanism. With an array, map the window message and the corresponding message processing function, you can understand this is a table. This mechanism is the message mapping. This table is defined in the window base class CWnd, and the message mapping table of derived class is empty, that is, if you don't manually add message processing functions, the parent class will be executed when the derived window accepts a message. Message handler. This is obviously efficient. Second, how to add your own message A standard message handler is the default handler that predefields the standard Windows message in the CWND class (WM_XXXX WM is the abbreviation of Window Message). The class library is named after the message name. For example, the handler of the WM_Paint message is declared in the CWnd as: AFX_MSG void onpaint (); AFX_MSG keywords indicate the role of the C Virtual keyword by distinguishing these handler to other CWND member functions. However, please note that these functions are actually not virtual, but through message mapping. We have explained this in the beginning of this article. All classes that can perform message processing are based on CCMDTARGET classes, that is, the CCMDTarget class is all parent classes that can be handled. The CCMDTARGET class is the basis and core of the MFC handling command message.

To rewrite the processing programs defined in the base class, just define a function with the same prototype in the derived class, and create the message mapping item for this handler. We can create most window messages or custom messages via ClassWizard. You can automatically establish message mapping through ClassWizard, and the framework of message processing function, we only need to fill out what we have to do, add what you have to do to handle functions . This is very simple, it will not say it. But maybe we need to add some ClassWizard unsupported window messages or custom messages, then we need to do the framework for establishing message mapping and message processing, usually the steps are as follows: The first step: Define the message. Microsoft recommends user-defined messages at least WM_USER 100 because many new controls also use WM_USER messages. #define WM_MYMESSAGE (WM_USER 100) Step 2: Implement Message Processing Functions. This function uses WPRAM and LPARAM parameters and returns lpeSult. LpeSult CMAINFRAME :: ONMYMESSAGE (WPARAM WPARAM, LPARAM LPARAM) {// Todo: Handle user-defined messages, fill in the blank is to fill here. Return 0; Step: In the user class message block, use the ON_MESSAGE macro to map the message to the message processing function. ON_MESSAGE (WM_MYMESSAGE, ONMYMESSAGE) In fact, the message category can be divided into multiple, and it is only one of them. There are three main message categories: (below) 1, Windows Message This type of message mainly includes messages that start with prefix WM_, except for WM_COMMAND. Windows messages are processed by windows and views. Such messages tend to have parameters for determining how to handle messages. 2, the control notifies such messages including the WM_COMMAND notification message sent from the control and other sub-windows to its parent window. For example, when the user performs operations that may change the text in Edit Control, the Edit Control will send a WM_CHANGMAND message containing the En_Change Control Notification Code to its parent. The window handler of this message responds to this notification message in a certain manner, such as retrieving the text in the control. The framework is transmitted to other WM_ messages, just like the control notification message. However, there is an exception, i.e., when the user clicks the button, the BN_Clicked control message is sent by the button. This message is specifically handled as a command message and transmits like other commands. 3, Command Message This type of message includes the WM_COMMAND notification message issued by the user interface object (menu, toolbar buttons, and shortcut). The frame processing command is different from the processing other messages, and more types of object processing commands can be used. Windows Messages and Control Notification Messages are processed by the window (the window is an object that is derived from the CWND class). Including CFrameWnd, CmdiframeWnd, CmdichildWnd, CView, CDIALOG, and your own class derived from these base classes. These objects encapsulate the handle of the HWnd - Windows window. Command messages can be processed by a broader object (document, document template, and application object itself), not just windows and views. When a certain command directly affects a particular object, the object should be processed by this command.

For example, the "Open" command in the File menu is logically associated with the application: the specified document is opened when the application receives this command. Therefore, the handler of the "Open" command is a member function of the application class. Command messages We are more common with menu items and toolbars. You can see that his message mapping macro and window message is not the same. The general form is such an on_command (ID, Memberfxn) The first parameter is the command ID, One ID number corresponds to a message processing, of course, you can make multiple ids share a process function. Common applications, for example: menu items Open the ID and toolbar buttons of the document Open the ID of the document simultaneously using a process function, or directly set them directly into the same. There is also a message called notification message. For example, some of the complex controls such as tree controls are available after clicking, such as a structure of the cursor, the location of the cursor, so the MFC is a macro for each notification message for the control, which is long. This looks: on_control (en_change, id, memberfxn) There are many messages existing in MFC, macro definitions are different, you can touch bypass. Third, the implementation of the trigger the right-click menu is quoted, starting with your own work, to trigger the action of the right-click menu, as long as a function of the corresponding custom menu is defined, and add the corresponding message processing function in the message processing loop. Yes, the following methods are proclaimed in the VC6.0 Windows2000 environment. There are two implementations, the first, is my own: First, a custom menu is defined in the resource editor, add the right-click pop-up menu function to the VC project, and the specific implementation method can see the first article of my blog. Article "Method for adding a right-click menu in the list box; add a function to the class (such as MyControl) to trigger the menu, and add the function as follows :: {MessageBox (" test "); // This is the test statement} final That is to add the following macro in the message processing loop: ON_COMMAND (ID_ mymessage, onmymessage) // id_ myMessage is the menu item name to be processed everything OK. Now talking about the second way, is seen in CodeProject, the specific link is in (http://www.codeProject.com/UserItems/SideBarmenu.asp): His way he creates a menu is different from me, is in the program The motion creation can be used as a reference to the dynamically creation menu. First, add the following statement in the initialization function of the class (note that csidebarmenumnusidebar is defined in the class's header file, csidebarmenu is a class of authors) mnusidebar.createpopUpMenu (); // Create pop-up menu mnusidebar.Appendmenu (mf_string | mf_ownerdraw, 10, "Item 1"); // Create the first menu item mnusidebar.Appendmenu (MF_SEPArator | MF_OWNERDRAW, 0, ""); // Create a split bar mnusidebar.Appendmenu (MF_STRING | MF_OWNERDRAW, 11, "ITEM 2") ; // Creating the parameters 10, 0, 11 of the second menu item refers to the flag corresponding to the menu item, that is, when the user selects the corresponding flag, it triggers the corresponding processing function.

When setting the right-click pop-up function, written in MyControl :: OnRButtondown (Uint Nflags, Cpoint Point): ClientToscreen (& Point); mnusidebar.trackpopupmenu (TPM_LEFTALIGN, POINT.X, POINT.Y, THIS, NULL); then Add a custom message handler MenuHandler as follows, seeing this is an SDK program that uses a comparison of the original message processing loop. Void myControl :: MenuHandler (UINT) {switch (id) // Judgment the current selection {case 10: // According to the user's selection, the corresponding processing MessageBox ("Item 1", "Sidebarmenu Demo"); Break Case 11: MessageBox ("Item 2", "Sidebarmenu Demo"); Break;}} Finally, add the following macro to the message processing loop: ON_COMMAND_RANGE (10, 11, menuhandler noticed, above Macro and I have different macros that I added in the first method, which can handle some messages within a certain range, the first two parameters are the scope of the message to be processed, which is also easy to understand.

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

New Post(0)