This paper briefly describes the method of dynamically creating a system tray menu when the program does not support Mfc,cmenu. Because the menu item is indeterminate, you need to create a dynamic pop-up menu with the SDK.
The main implementation code is as follows:
------------------code begin----------------//variables defined in the class://The corresponding menu item ID when you double-click the tray icon.
UINT M_ndclickmenuitemid;
pop-up menu handle.
M_hmenu M_hmenu;
Dynamically creates a right-click menu item.
@param the Item_text menu item text string, separated by commas.
@param dbclick_id Double-click the menu item ID for the tray icon.
Call Example://Create_menu ("Show/Hide, exit", 40001);
BOOL Ctrayiconmenu::create_menu (char* item_text, unsigned int dbclick_id) {m_ndclickmenuitemid = dbclick_id;
Dynamically create pop-up menu M_hmenu =:: CreatePopupMenu ();
if (M_hmenu = = NULL) {return 0;
int i=0; int msgid=40001;
The message ID of the first menu item.
BOOL ret = 0;
char * Ptext =:: Strtok (Item_text, ","); while (Ptext!= NULL) {ret =:: AppendMenu (M_hmenu, mf_enabled |
mf_string, MsgId + i, ptext);
Ptext =:: Strtok (NULL, ",");
i++;
return 1;
The///response message displays the menu.
Lresult Ctrayiconmenu::on_msg (WPARAM wid, LPARAM event) {//Some other processing code ...
Point Mouse;
:: GetCursorPos (&mouse);
The HWND is the application main window handle.
if (event = = Wm_rbuttonup) {:: SetForegroundWindow (HWND); :: TrackPopupMenu (M_hmenu, 0, mouse.x, mouse.y, 0, HWnd, NULL);
return 1; //------------------Code end----------------