CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2013
    Posts
    77

    Context menu creation

    I would like to create a context menu,Its look something like this
    HTML Code:
    Delete ->Rung    ->Start 
                      ->End 
            ->Element 
    Exit
    my code is
    Code:
    case WM_RBUTTONDOWN :				//Right click option
    {
    	MENUITEMINFO mi1,mi2={0};
    	HMENU hPopupMenu = CreatePopupMenu();
    	HMENU hSubMenu1 = CreatePopupMenu();
    	HMENU hSubMenu2 = CreatePopupMenu();
    	
    	InsertMenu(hPopupMenu,0,MF_BYPOSITION|MF_STRING,ID_EDIT_EXTEND,_T("Exit"));
    	
    	mi1.cbSize=sizeof(MENUITEMINFO);
    	mi1.fMask=MIIM_SUBMENU|MIIM_STRING|MIIM_ID;
    	mi1.wID=ID_EDIT_DELETE;
    	mi1.hSubMenu=hSubMenu1;
    	mi1.dwTypeData=_T("Delete");
    	InsertMenu(hSubMenu1,0,MF_BYPOSITION|MF_STRING,ID_DELETE_ELEMENT,_T("Element"));
    	InsertMenu(hSubMenu1,0,MF_BYPOSITION|MF_STRING,ID_DELETE_RUNG,_T("Rung"));
    	
    	mi2.cbSize=sizeof(MENUITEMINFO);
    	mi2.fMask=MIIM_SUBMENU|MIIM_STRING|MIIM_ID;
    	mi2.wID=ID_DELETE_RUNG;
    	mi2.hSubMenu=hSubMenu2;
    	mi2.dwTypeData=_T("Rung");
    	InsertMenu(hSubMenu2,0,MF_BYPOSITION|MF_STRING,ID_DELETE_START,_T("Start"));
    	InsertMenu(hSubMenu2,0,MF_BYPOSITION|MF_STRING,ID_DELETE_END,_T("End"));
    	
    	
    	InsertMenuItem(hSubMenu1,0,false,&mi2);
    	InsertMenuItem(hPopupMenu,0,false,&mi1);
    
    	SetForegroundWindow(hWnd);
    	GetCursorPos(&ptClient);
    	TrackPopupMenuEx(hPopupMenu,0,ptClient.x,ptClient.y,hWnd,NULL);
    
    	
    }
    But i am getting Output Like this
    HTML Code:
    Delete ->Rung              
              ->Element 
              ->Rung ->Start 
                     ->End 
    Exit
    What modification i have to do for the desired output

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Context menu creation

    The item "Rung" is popup menu. so it needs flag MF_POPUP and a menu handle rather than a menu item ID to be passed in the InsertItem... call
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2013
    Posts
    77

    Re: Context menu creation

    I did a modification like this,& its working,But i dont know its correct or not
    Code:
    case WM_RBUTTONDOWN :				//Right click option
    {
    	MENUITEMINFO mi1,mi2={0};
    	HMENU hPopupMenu = CreatePopupMenu();
    	HMENU hSubMenu1 = CreatePopupMenu();
    	HMENU hSubMenu2 = CreatePopupMenu();
    	
    	InsertMenu(hPopupMenu,0,MF_BYPOSITION|MF_STRING,ID_EDIT_EXTEND,_T("Exit"));
    	
    	mi1.cbSize=sizeof(MENUITEMINFO);
    	mi1.fMask=MIIM_SUBMENU|MIIM_STRING|MIIM_ID;
    	mi1.wID=ID_EDIT_DELETE;
    	mi1.hSubMenu=hSubMenu1;
    	mi1.dwTypeData=_T("Delete");
    	InsertMenu(hSubMenu1,0,MF_BYPOSITION|MF_STRING,ID_DELETE_ELEMENT,_T("Element"));
    	//InsertMenu(hSubMenu1,0,MF_BYPOSITION|MF_STRING,ID_DELETE_RUNG,_T("Rung"));	
    	mi2.cbSize=sizeof(MENUITEMINFO);
    	mi2.fMask=MIIM_SUBMENU|MIIM_STRING|MIIM_ID;
    	mi2.wID=ID_DELETE_RUNG;
    	mi2.hSubMenu=hSubMenu2;
    	mi2.dwTypeData=_T("Rung");
    	InsertMenu(hSubMenu2,0,MF_BYPOSITION|MF_STRING,ID_DELETE_START,_T("Start"));
    	InsertMenu(hSubMenu2,0,MF_BYPOSITION|MF_STRING,ID_DELETE_END,_T("End"));
    	
    	
    	InsertMenuItem(hSubMenu1,0,true,&mi2);
    	InsertMenuItem(hPopupMenu,0,false,&mi1);
    
    	SetForegroundWindow(hWnd);
    	GetCursorPos(&ptClient);
    	TrackPopupMenuEx(hPopupMenu,0,ptClient.x,ptClient.y,hWnd,NULL);
    
    	
    }
    break;

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Context menu creation

    Quote Originally Posted by manjut19 View Post
    I did a modification like this,& its working,But i dont know its correct or not
    What is your doubt in and why?
    Or, at least, define "its working"
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2013
    Posts
    77

    Re: Context menu creation

    Quote Originally Posted by VictorN View Post
    What is your doubt in and why?
    Or, at least, define "its working"
    Its working as desired,But I don;t know code wise its correct

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Context menu creation

    Quote Originally Posted by manjut19 View Post
    I would like to create a context menu
    Are you aware of the fact that context menu is just a popup menu, and can be a menu resource alright?
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured