I am a software engineer. I have been come across C++,MFC, .net technologies. I like playing video games & reading books.
In this article, we will create main menu with four menu items in it. The last menu item will open a sub-menu. The menu will be displayed when the mouse is right-clicked in the client area of the window and at the location of the mouse pointer.
2. About the Example
The below screenshot shows the application sample:
The Example is an SDI Application without document and view architecture support. We marked the client area with a yellow border in the below screenshot. When the mouse pointer is inside the client area of the window, MFC will display a pop-up menu.
Here, we are creating the Menu items at run-time and displaying the Pop-up Menu as shown in the above screenshot. The video given below shows default setting overridden for MFC SDI Application.
Video 1: Creating MFC SDI Application without Document View Support (No Audio)
3. Process WM_CONTEXTMENU
When the Mouse is right clicked inside the client area of the Window, the Window will get a notification message WM_CONTEXTMENU. This message will come with the Window Handle in which the Mouse is right clicked. In addition, it also contains Mouse Pointer position in Screen Coordinate where the right click happened. We will use this notification message to display the Pop-up Menu.
The video given below shows how to provide a handler for the WM_CONTEXTMENU message. We will handle this Window message in the CChildView.
Video 2: Adding Handler for the Message WM_CONTEXTMENU (No Audio)
In the video, we saw a view class which provides the handler for the WM_CONTEXTMENU message. The handler looks like below:
Here, pWnd is the pointer to the window in which the user produces the right client. The second parameter called a point in this function supplies the mouse cursor location in Screen Coordinates.
4. Display Context Menu by Handling OnContextMenu
The menu is created in side the handler provided for the WM_CONTEXTMENU.
1) First We declare a CRect class to get the client window dimensions. Next, we create SubMenu and MainMenu instance of type CMenu.
2) After the Declarations, we get the client area of the window in client_rect structure. Then, we convert this structure into Screen Co-Ordinate which has the origin from the top left of our monitor. We do this because the point parameter given to our handler as a second argument is in Screen Co-Ordinate.
3) We will display the pop-up context menu when the mouse is right clicked only inside the client area of the window. Therefore, we should check the mouse click position lies inside the client rectangle dimension. Note that as we get the mouse position in screen co-ordinate, we converted the rectangle dimension of the client_rect into Screen Co-Ordinate. We need this for performing the right-clicked location is inside the client area of the SDI application window. We use the function PtInRect to achieve this.
4) After the point is inside the rectangle test, the Sub-Menu for the Context Menu is created by calling the CreatePopupMenu function of the CMenu object. Then, the Menu Items are added to it using the AppendMenu function call. The first parameter passed to it as MF_STRING denotes that we are adding a String Menu Item. The second parameter is ID value which we gave while creating Menu Item. We will use later this Id when we need to process Command Message (Not covered in this article). The last parameter is Display String of the Menu Item.
Once the Sub-Menu is created, we create the Main Menu. We create this Menu the same way the Sub-Menu was created. However, the last item on the Main Menu is a linked to the Sub-Menu which we already created. Note, we added the Sub-Menu to this Main Menu by sending the MF_POPUP as the first parameter to the function call AppendMenu. This will show the AppendMenu function that unlike the normal Menu Item it should create the Cascading Menu for the Menu Item named "Line Thickness". Below is the code:
5) Finally, we call TrackPopupMenu to display the Menu which we created earlier. The first parameter TPM_LEFTALIGN tells that the displayed pop-up menu should be left aligned with the cursor location. The x, y position tells where we want to display the MainMenu as a Pop-Up Menu.
Video 3: Display of Popup Menu in the SDI Application (No Audio)
Source Code: Download
© 2018 sirama