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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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
HooKooDooKuCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.