Add checkbox to menu item

How do I add a checkbox next to a menu item in MFC? I tried to use the Checked field but it doesn't work quite like expected.
steenpatAsked:
Who is Participating?
 
HooKooDooKuConnect With a Mentor Commented:
The CMenu functions like CheckMenuItem() allows you to specify which item in the menu by ID or position.

pMenu->CheckMenuItem( 3, MF_BYPOSITION | MF_CHECKED   )
0
 
HooKooDooKuCommented:
You have to get a CMenu object and use CMenu::CheckMenuItem.  

While I don't have any code handy showing the check mark, here's a code snippet you might can learn from that creates a pop-up menu and disables some of the menu items rather than check or uncheck the menu items.  Note that IDR_DATALISTCTRL_MENU is the name of the menu in the resource editor (and the other ID_XXX are the names of the menu items).
void CDataListCtrl::OnContextMenu( CWnd* pWnd, CPoint point )
{
	//Cache Location (for OnPopupRelabelColumn)
	m_ContextMenuPoint = point;
 
	//Load the menu
	CMenu Menu;
	Menu.LoadMenu( IDR_DATALISTCTRL_MENU );
	CMenu* pPopup = Menu.GetSubMenu(0);
 
	int SelectCount = CListCtrl::GetSelectedCount();
	ItemPtr pSelectedItem = this->GetSelectedItem();
 
	//Disable "Transaction" & "Remittance" info if More or Less than one item is selected
	Menu.EnableMenuItem( ID_POPUP_TRANSACTIONINFO, SelectCount == 1 ? MF_ENABLED : MF_GRAYED ); 
	Menu.EnableMenuItem( ID_POPUP_REMITTANCEINFO , SelectCount == 1 ? MF_ENABLED : MF_GRAYED ); 
 
	//Disable "Remittance" is selected Item does not have a Payee
	if( pSelectedItem )
	{
		CString Payee( pSelectedItem->strPayee );
		if( Payee.IsEmpty() ) Menu.EnableMenuItem( ID_POPUP_REMITTANCEINFO, MF_GRAYED );
	}
 
	//Disable "Print Selected" if nothing selected
	Menu.EnableMenuItem( ID_POPUP_PRINTSELECTED, SelectCount ? MF_ENABLED : MF_GRAYED );
 
	//Show Menu
	pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL );
}

Open in new window

0
 
steenpatAuthor Commented:
That's not quite what I want. I want the checkbox control beside the menu item and the user to be able to click on it, not change the state of the menu item based on some setting.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
HooKooDooKuCommented:
You don't have to add a checkbox control beside a menu item.  Every menu item already has the ability to place a check mark beside it.

In the above example, I was only attempting to show an idea of how to access the menu in general.  Of course in your case, you don't want to do things like "Menu.EnableMenuItem(...)", you instead want to call Menu.CheckMenuItem( ID_menuitem, MF_CHECKED ) to check it and Menu.CheckMenuItem( ID_menuitem, MF_UNCHECKED ) to uncheck it.  You will just need to execute these commands when the user clicks on the menu item.

//Assuming we have a class CMyWnd with a 
//Add to the window's message handler
ON_COMMAND(ID_YOUR_MENUITEM, &CYourWnd::OnMenuYourMenuItem)
 
//Add to Window Class
void OnMenuYourMenuItem()
{
  CMenu* pMenu = //Get reference to your menu ... I'll admit I'm a bit fuzzy on this part here.
                 //Note the exact code may require loading a main window, then accessing a submenu
  if( pMenu->GetMenuState( ID_menuitem, MF_BYCOMMAND ) & MF_CHECKED == MF_CHECKED )
  {
    pMenu->CheckMenuItem( ID_menuitem, MF_UNCHECKED );
  }
  else
  {
    pMenu->CheckMenuItem( ID_menuitem, MF_CHECKED );
  }

Open in new window

0
 
steenpatAuthor Commented:
How I am going to do this if I want to be able to check a menu item that is a popup? I notice that visual studio2005 will not allow me to assign it a resource ID if I do so.
0
 
steenpatAuthor Commented:
Actually, I jumped the gun again...
I meant that because it does not have a resource ID I can't seem to handle OnCmd message for it, unless I can somehow do that by menu position as well?
0
 
HooKooDooKuCommented:
You can use resource IDs with Popup menus.  Here's an example using the right click to generate a context menu.
IDR_MYWNDPOPUP_MENU MENU 
BEGIN
    POPUP "POPUP_MYWNDPOPUP"
    BEGIN
        MENUITEM "Item1", ID_POPUP_ITEM1
        MENUITEM "Item2", ID_POPUP_ITEM2
        MENUITEM "Item3", ID_POPUP_ITEM3
    END
END
 
 
 
*** C++ CODE ***
BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
 
	//Popup Menu
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_POPUP_ITEM1, &CMyWnd::OnPopupItem1)
	ON_COMMAND(ID_POPUP_ITEM2, &CMyWnd::OnPopupItem2)
	ON_COMMAND(ID_POPUP_ITEM3, &CMyWnd::OnPopupItem3)
 
END_MESSAGE_MAP()
 
void CStmtViewWnd::OnContextMenu( CWnd* pWnd, CPoint point )
{
 	//Load the menu
	CMenu Menu;
	Menu.LoadMenu( IDR_MYWNDPOPUP_MENU );
	CMenu* pPopup = Menu.GetSubMenu(0);
 
	//Show Menu
	pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL );
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.