Adrian031197
asked on
Dynamic Menus
MSVC5 Enterprise
How do I dynamically add items to a menu?
How do I dynamically add items to a menu?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How do I add items to a menu on a tooltray icon?
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_hwndMenuOwne r, WM_NULL, 0, 0);
TrackPopupMenu(Ex), passing previously loaded or created menu. The only one trick here is you must call SetForegroundWindow() before TrackPopupMenu and
PostMessage(m_hwndMenuOwne
ASKER
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.
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.
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(AddReplac eDelete, &tnid);
if (hicon)
DestroyIcon(hicon);
2.Add message handler for WM_YOURMESSAGE
LONG CYourView::OnOpenEnableMen u(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_T RAYMENU));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
SetForegroundWindow();
pPopup->TrackPopupMenu(TPM _RIGHTALIG N | 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;
}
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(AddReplac
if (hicon)
DestroyIcon(hicon);
2.Add message handler for WM_YOURMESSAGE
LONG CYourView::OnOpenEnableMen
{
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_T
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
SetForegroundWindow();
pPopup->TrackPopupMenu(TPM
// 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;
}
ASKER
Is the Loadmenu command what adds the command to the tray icon?
And what was wrong with my code?
And what was wrong with my code?
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).
ASKER
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?
And what was wrong with my code?
ASKER
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)