Current Position :
Acebyte >
Support >
Knowledgebase >
How to Make Full Use of Context Menu for Computer Control

Acebyte Knowledge Base

How to Make Full Use of Context Menu for Computer Control

January 27th, 2012 128 views |0 Comments
Share

Each control on computer has its own and clear feature for PC users. For example, a button is usually used to be double clicked to open or start an action. So is the context menu. That is to say, if you right click, not left click, you will enter a special menu. To make full use of these context menus, you can control your computer better to make PC running faster.

 

Guide on how to activate context menu on computer:

Step 1. Activate ControlContext, which is a Microsoft Visual C++ MFC application

Step 2. Make it as Dialog Box without the AboutBox

Step 3. In Context-Sensitive Menu on Controls, key in Dialog Title

Step 4. Add a button to Dialog Box, and do changes: ID to IDC_SUBMIT_BTN and Caption to Submit

Step 5. Add a check box to the Dialog Box, and do changes: ID to IDC_APPROVED_CHK and Caption to Approved

Step 6. Show the Add Resource window and double click on Menu:

Step 7. Do the changes: IDR_MENU1 to IDR_SUBMIT in ID and create it as below:

Step 8. Changes the IDs to ID_POPUP_GREEN, ID_POPUP_RED, ID_POPUP_YELLOW, ID_POPUP_BLUE, ID_POPUP_WHITE, and ID_POPUP_FUCHSIA

Step 9. Turn to the Dialog Box and click its body. In the appeared Properties window, press the messages button .

Step 10. Implement the even as below by clicking the arrow of WM_CONTEXTMENU combo box:

void CControlContextDlg::OnContextMenu(CWnd* /*pWnd*/, CPoint point)

{

// TODO: Add your message handler code here

// Load the desired menu

CMenu mnuPopupSubmit;

mnuPopupSubmit.LoadMenu(IDR_SUBMIT);

// Get a pointer to the button

CButton *pButton;

pButton = reinterpret_cast<CButton *>(GetDlgItem(IDC_SUBMIT_BTN));

// Find the rectangle around the button

CRect rectSubmitButton;

pButton->GetWindowRect(&rectSubmitButton);

// Get a pointer to the first item of the menu

CMenu *mnuPopupMenu = mnuPopupSubmit.GetSubMenu(0);

ASSERT(mnuPopupMenu);

// Find out if the user right-clicked the button

// because we are interested only in the button

if( rectSubmitButton.PtInRect(point) ) // Since the user right-clicked the button, display the context menu

mnuPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);

}

 

Step 11. Implement its Click even shown as below by double clicking the Submit button in the dialog box:

void CControlContextDlg::OnBnClickedSubmitBtn()
{
               // TODO: Add your control notification handler code here
               AfxMessageBox("You clicked the Submit button but ain't nothin' to do right now!!!");
}

Step 12. Show the IDR_SUBMIT menu and right click the Green menu and then click Add Even Handler, finally set the Class List to CControlContextDlg

Step 13. Then turn to the IDR_SUBMIT menu, right click the Red mune and click Add Event Handler, finally set the Class List to CControlContextDlg

Step 14. Now you will implement the events as following:

void CControlContextDlg::OnPopupGreen()
{
               // TODO: Add your command handler code here
               AfxMessageBox("You selected the Green menu item");
}

void CControlContextDlg::OnPopupRed()
{
               // TODO: Add your command handler code here
               AfxMessageBox("You selected the Red menu item");
}

Step 15. Excute the application and right click on the Submit button as below:

Step 16. Change the OnContextMenu() event as below to display the other menu:

void CControlContextDlg::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
               // TODO: Add your message handler code here
               // Load the desired menu
               CMenu mnuPopupSubmit;
               CMenu mnuPopupApproval;
 
               mnuPopupSubmit.LoadMenu(IDR_SUBMIT);
               mnuPopupApproval.LoadMenu(IDR_APPROVAL);
 
               // Get a pointer to the button
               CButton *pButton, *chkApproval;
 
               pButton = reinterpret_cast<CButton *>(GetDlgItem(IDC_SUBMIT_BTN));
               chkApproval = reinterpret_cast<CButton *>(GetDlgItem(IDC_APPROVED_CHK));
 
               // Find the rectangle around the control
               CRect rectSubmitButton, rectApprovalButton;
 
               pButton->GetWindowRect(&rectSubmitButton);
               chkApproval->GetWindowRect(&rectApprovalButton);
 
               // Get a pointer to the first item of the menu
               CMenu *mnuPopupMenu = mnuPopupSubmit.GetSubMenu(0);
               CMenu *mnuPopupMenu2 = mnuPopupApproval.GetSubMenu(0);
               ASSERT(mnuPopupMenu);
               ASSERT(mnuPopupApproval);
               
               // Find out if the user right-clicked a button
               if( rectSubmitButton.PtInRect(point) ) // Since the user right-clicked a button, display its context menu
                               mnuPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
               else if( rectApprovalButton.PtInRect(point) )
                               mnuPopupMenu2->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}

Step 17. Show the IDR_APPROVAL menu

Step 18. Right click every menu and then click Add Event Handler, set the Class List to CContextMenuDlg, finally you will be able to implement the events as below:

void CControlContextDlg::OnPopupUnderreview()
{
               // TODO: Add your command handler code here
               CheckDlgButton(IDC_APPROVED_CHK, BST_INDETERMINATE);
}
 
void CControlContextDlg::OnPopupApproved()
{
               // TODO: Add your command handler code here
               CheckDlgButton(IDC_APPROVED_CHK, BST_CHECKED);
}
 
void CControlContextDlg::OnPopupRejected()
{
               // TODO: Add your command handler code here
               CheckDlgButton(IDC_APPROVED_CHK, BST_UNCHECKED);

Step 19. Now you can test the application.

Related Post :

Leave a Reply