VC implementation of dynamic menu creation method _c language

Source: Internet
Author: User

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----------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.