CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Nov 2001
    Posts
    244

    Adding an item to an existing CMenu

    VS2005 C++ MFC

    When I try to add a new Menu Item to an existing Menu, the new item never shows up. All I see are the existing menu items from the Resource.

    Code:
        
    
        // Load SysTrayMenu (WORKS OK, IDR_MENU1 is a Menu Resource with five items)
        if(SysTrayMenu.LoadMenu(IDR_MENU1)==NULL) AfxMessageBox("LoadMenu Failed");
    
    
        // Add a new item to the existing menu (FAILS)
        MENUITEMINFO MenuItem;
        ZeroMemory(&MenuItem, sizeof(MENUITEMINFO));
        MenuItem.cbSize = sizeof(MENUITEMINFO);
        MenuItem.fMask = MIIM_TYPE|MIIM_ID|MIIM_STATE;  //MIIM_STRING|MIIM_ID;
        MenuItem.wID = ID_POPUP_ADMIN;
        MenuItem.fType = MFT_STRING;
        MenuItem.fState = MFS_ENABLED;
        MenuItem.cch = 6;
        MenuItem.dwTypeData = "Admin";
        SysTrayMenu.InsertMenuItem(3, &MenuItem, TRUE);
    
    	
    
    
        // Lets try again. Add a new item to the existing menu (FAILS AS WELL)
        SysTrayMenu.InsertMenu(4, MF_STRING|MF_BYPOSITION|MF_ENABLED, ID_POPUP_ADMIN, "Admin");
    
        // Has a migraine... lets try again (FAILS AS WELL)
        SysTrayMenu.AppendMenu(MF_BYPOSITION, ID_POPUP_ADMIN, "Admin");
    	
        // Apparently we need to call this to redraw the menu
        DrawMenuBar();

    Any ideas?
    Last edited by Anarchi; April 14th, 2010 at 10:50 PM.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Adding an item to an existing CMenu

    And what is SysTrayMenu?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Nov 2001
    Posts
    244

    Re: Adding an item to an existing CMenu

    Its a standard CMenu.

    CMenu SysTrayMenu;

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

    Re: Adding an item to an existing CMenu

    Quote Originally Posted by Anarchi View Post
    Its a standard CMenu.

    CMenu SysTrayMenu;
    So where is it declared and how is it used after LoadMenu/InsertMenu?
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2001
    Posts
    244

    Re: Adding an item to an existing CMenu

    The CMenu is declared as a member in the main MFC Dialog class.

    The menu is activated when right-clicking the system tray icon.

    I use the CSysTrayIcon Class made by Prateek Kaul, found here:
    http://www.codeguru.com/Cpp/misc/mis...int.php/c3891/

    To popup the menu, I handle WM_SYSTRAYMSG in PreTranslateMessage and call:
    SysTrayIcon.OnContextMenu(SysTrayMenu.GetSubMenu(0), ptMouse, this);

    The above function contains:

    Code:
    BOOL CSysTrayIcon::OnContextMenu(CMenu* pContextMenu, CPoint ptMouse, CWnd* pWndMessageHandler)
    {
        // The window is brought forward, so that it starts 
        // receving messages, rather than currently active window.
        // If we don't do this the messages of the context menu will
        // go to the currently active window.
        pWndMessageHandler->SetForegroundWindow();
    	
        BOOL bResult = pContextMenu->TrackPopupMenu(
                           TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
                           ptMouse.x, 
                           ptMouse.y, 
                           pWndMessageHandler
                       );
        
        return bResult;
    }


    Do you think that System Tray Icon class may not be handling the menu modification properly?

    Maybe I should try adding "DrawMenuBar()" to that function, rather than calling DrawMenuBar in InitDialog()

  6. #6
    Join Date
    Nov 2001
    Posts
    244

    Re: Adding an item to an existing CMenu

    I added the below line in that systemtrayicon class, but it didnt make a difference.
    :rawMenuBar(AfxGetMainWnd()->m_hWnd);

    By the way, when I call InsertMenuItem() in the main InitDialog function, its return value is successful.

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

    Re: Adding an item to an existing CMenu

    SysTrayMenu.InsertMenuItem(3, &MenuItem, TRUE);
    . . .
    To popup the menu, I handle WM_SYSTRAYMSG in PreTranslateMessage and call:
    SysTrayIcon.OnContextMenu(SysTrayMenu.GetSubMenu(0), ptMouse, this);
    Don't you see any problem with such insertion and further use?
    Best regards,
    Igor

  8. #8
    Join Date
    Nov 2001
    Posts
    244

    Re: Adding an item to an existing CMenu

    Quote Originally Posted by Igor Vartanov View Post
    Don't you see any problem with such insertion and further use?
    Interesting, why is that "GetSubItem" Thanks I'll look into it.

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

    Re: Adding an item to an existing CMenu

    Quote Originally Posted by Anarchi View Post
    Interesting, why is that "GetSubItem" Thanks I'll look into it.
    Where do you see (or have) GetSubItem?
    I don't see any...

    Besides, you don't need to call DrawMenuBar.
    Last edited by VictorN; April 21st, 2010 at 02:31 AM.
    Victor Nijegorodov

  10. #10
    Join Date
    Nov 2001
    Posts
    244

    Re: Adding an item to an existing CMenu

    Sorry I meant "GetSubMenu(0)"

    I still haven't figured it out, maybe I'll just work around the problem and add a hotkey or something.

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

    Re: Adding an item to an existing CMenu

    You definitely need do some reading on menus in Windows.

    What they used to call a popup menu is a submenu in fact.
    Now, you show submenu 0 but add new menu item to the parent menu. So once again, don't you see any problem with this?
    Last edited by Igor Vartanov; April 21st, 2010 at 02:11 AM.
    Best regards,
    Igor

  12. #12
    Join Date
    Nov 2001
    Posts
    244

    Re: Adding an item to an existing CMenu

    I'm confused, so do I need to add the new item to the submenu? (instead of the parent menu)

    Why do Microsoft make things so complicated lol

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

    Re: Adding an item to an existing CMenu

    Quote Originally Posted by Anarchi View Post
    ,,, Why do Microsoft make things so complicated lol
    Well where did you see less "complicated" menus?
    Victor Nijegorodov

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

    Re: Adding an item to an existing CMenu

    Quote Originally Posted by Anarchi View Post
    I'm confused, so do I need to add the new item to the submenu? (instead of the parent menu)
    If I were you, I would already be re-reading MSDN and have made twenty tries instead of a doubtful asking here.

    Why do Microsoft make things so complicated lol
    Sorry, but complicated it looks only because of your lack of experience. Again, if I were you, I would withhold from judging something I do not completely understand.
    Best regards,
    Igor

  15. #15
    Join Date
    Nov 2001
    Posts
    244

    Re: Adding an item to an existing CMenu

    Thanks for the help Igor.

    I haven't had time to try again (very busy at work ) but I'll have another read of MSDN when I get a chance.

    With a lot of Microsoft coding, you need to spend a huge amount of time understand it properly
    They should KISS (keep it simple silly) or at least provide wrappers to simplify common tasks like they do in Visual Basic.

Page 1 of 2 12 LastLast

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