Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic Menus

Posted on 1997-11-29
9
Medium Priority
?
288 Views
Last Modified: 2013-11-20
MSVC5 Enterprise
How do I dynamically add items to a menu?
0
Comment
Question by:Adrian031197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 100 total points
ID: 1311082
Generally speaking you can add menu by InsertMenu or AppendMenu.
0
 

Author Comment

by:Adrian031197
ID: 1311083
I can add it, now I'm trying to make it do something.
What's wromg with this code?

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra,
                    AFX_CMDHANDLERINFO* pHandlerInfo)
{
//A
      if (pHandlerInfo == NULL)
      {
            if(nID==WM_MYMENU)
                  AfxMessageBox("You Clicked The DynMenu",
                                MB_OK | MB_ICONEXCLAMATION);
            else
            {
                  AfxMessageBox("Hmm", MB_OK);
            }
      return TRUE;
      }
//B      
      return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

The added menu item has the ID WM_MYMENU.
If I run this program and click the menu, I get 4 Hmms, one right after the other.
If I comment out everything between A and B, it works just fine (minus responding to my dynamic item)
0
 

Author Comment

by:Adrian031197
ID: 1311084
How do I add items to a menu on a tooltray icon?
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 7

Expert Comment

by:galkin
ID: 1311085
Shell tray menu doesn't differ from any other menu. When you right/left click on your application icon window associated with shell tray icon is send call back message, you set when you supplied data for icon. LPARAM contains actual message ID(for instance WM_L(R)BUTTONDOWN(UP)). If you want to popup menu in response to this message you do this as usual by
TrackPopupMenu(Ex), passing previously loaded or created menu. The only one trick here is you must call SetForegroundWindow() before TrackPopupMenu and
PostMessage(m_hwndMenuOwner, WM_NULL, 0, 0);
0
 

Author Comment

by:Adrian031197
ID: 1311086
1st, what's wrong with the code that I posted?
2nd, when I add an item to the menu, it shows up in the main window, but not in the tooltray icon. It's menu never changes.
0
 
LVL 7

Expert Comment

by:galkin
ID: 1311087
To add menu to shell tray do the following:

1.Add icon and call back message to the shell tray
     
    BOOL res;
    NOTIFYICONDATA tnid;
 
    tnid.cbSize = sizeof(NOTIFYICONDATA);
    tnid.hWnd = hwnd;
    tnid.uID = uID;
    tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    tnid.uCallbackMessage = NotifyIcon;
    tnid.hIcon = hicon;
    if (lpszTip)
        lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip));
    else
        tnid.szTip[0] = '\0';
 
      // the ADD in the line below ADDS a new icon
      // if it were MODIFY it would change it for the new icon
    res = Shell_NotifyIcon(AddReplaceDelete, &tnid);
 
    if (hicon)
        DestroyIcon(hicon);
 
   
2.Add message handler for WM_YOURMESSAGE

LONG CYourView::OnOpenEnableMenu(UINT wParam, LONG lParam)
{
            if (lParam == (LONG)WM_LBUTTONDOWN ||
                  lParam == (LONG)WM_RBUTTONDOWN){
            // connects the pop-up menu to the code
            CMenu menu;
            CPoint pt;
            GetCursorPos(&pt);
      
            VERIFY(menu.LoadMenu(IDR_TRAYMENU));
            CMenu* pPopup = menu.GetSubMenu(0);
            ASSERT(pPopup != NULL);      
            
            SetForegroundWindow();

            pPopup->TrackPopupMenu(TPM_RIGHTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, this);

            // It is necessary to force a task switch to the application that
            // called TrackPopupMenu at some time in the near future. This can
            // be accomplished by posting a benign message to the window or thread.
            PostMessage(WM_NULL);
      }
      return 0L;
}      
0
 

Author Comment

by:Adrian031197
ID: 1311088
Is the Loadmenu command what adds the command to the tray icon?
And what was wrong with my code?
0
 
LVL 7

Expert Comment

by:galkin
ID: 1311089
Loadmenu loads menu from resource. If you need to modify this menu use DeleteMenu to delete menu item(s) or InsertMenu to insert menu item(s).
0
 

Author Comment

by:Adrian031197
ID: 1311090
But why won't the menu items that I add dynamically appear on the tool tray icon menu?
And what was wrong with my code?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question