Context Menu & Dialog Box :: MFC

This is a discussion on Context Menu & Dialog Box :: MFC within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I would like to implement a context menu inside a dialog box. I created a "clear" menu (no caption) ...

  1. #1
    kuphryn
    Guest

    Context Menu & Dialog Box :: MFC

    Hi.

    I would like to implement a context menu inside a dialog box. I created a "clear" menu (no caption) using Resource Editor. Note that I create an entirely new menu item like IDR_MAINFRAME (mine is IRD_CONTEXTMENU1. Inside the dialog box, I added a message for right-click. Here is what the code looks like.

    -----
    CPoint mPointCurrent;
    ::GetCursorPos(&mPointCurrent);
    CMenu mPopupMenu;
    mPopupMenu.LoadMenu(IRD_CONTEXTMENU1);

    // Program crashes at this point.

    CMenu *pContextMenu = mPopupMenu.GetSubMenu(0);
    pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
    mPointCurrent.x, mPointCurrent.y, GetActiveWindow(), NULL);
    -----

    The code above does not work. The program crashes when I right-click inside the dialog box. Is the code above for a view class only? Is there a different and corrent way to implement a context menu inside a dialog box?

    Thanks,
    Kuphryn

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786

    Re: Context Menu & Dialog Box :: MFC

    It crashes you say.....

    Are you sure that IRD_CONTEXTMENU1 actually represents your resource as when VC++ creates a menu it would usually call this IDR_CONTEXTMENU1

    I tried the following and it worked ok

    Code:
    void CTestKuphDlg::OnRButtonUp(UINT nFlags, CPoint point) 
    {
    	CPoint mPointCurrent;	
    	CMenu mPopupMenu;
    	mPopupMenu.LoadMenu(IRD_CONTEXTMENU1);
    	ClientToScreen(&point);
    	CMenu *pContextMenu = mPopupMenu.GetSubMenu(0);
    	pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | 
    		TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD | 
    		TPM_NONOTIFY,point.x, point.y,GetActiveWindow(), NULL);	
    	pContextMenu->DestroyMenu();
    	CDialog::OnRButtonUp(nFlags, point);
    }

  3. #3
    kuphryn
    Guest
    Thanks.

    I included the wrong ID when calling mPopupMenu.LoadMenu().

    Hey, you used pContextMenu->DestroyMenu(). Is that a necessity?

    Kuphryn

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by kuphryn
    Thanks.

    I included the wrong ID when calling mPopupMenu.LoadMenu().

    Hey, you used pContextMenu->DestroyMenu(). Is that a necessity?

    Kuphryn
    LOL....no ......On looking again, I guess if I was going to free the resource I should have done mPopupMenu.DestroyMenu();....

    I was trying to free it here because with your code you were calling LoadMenu() in the message handler.....not the best way to go I guess

    A far better way to go is to load the menu once somewhere else in the code (maybe on the WM_INITDIALOG) and store the CMenu in the dialog's class as a class member variable......

    Then call DestroyMenu() when the dialog closes

  5. #5
    kuphryn
    Guest
    Interesting. Thanks.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Printer Prior To Loading Print Dialog Box
    By Beaner in forum Windows Programming
    Replies: 3
    Last Post: 10-10-2008, 01:02 PM
  2. Add a dialog box to a tab
    By axr0284 in forum Windows Programming
    Replies: 0
    Last Post: 01-10-2005, 07:38 AM
  3. Context Menu cursor problem
    By dWorkVan in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2003, 11:42 AM
  4. edit control in dialog box problems
    By Bajanine in forum Windows Programming
    Replies: 11
    Last Post: 11-11-2002, 05:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21