A menu bar always belongs to a window. After a user selects a menu item, the system sends a message to the parent window of the menu. If a common menu sends a wm_command message, if the system menu sends the wm_syscommand message. When the mouse is hovering over a menu item with a sub-menu, the system first sends the wm_initmenupopup message to the parent window of the menu, and then expands the sub-menu. Each menu bar is pointed by a unique handle. Its type is hmenu. Use the getmenu () function to obtain the handle of a menu. Use getsubmenu () and getmenuiteminfo () to obtain the sub-menu (). Getsystemmenu () should be used to obtain the system menu handle of the window (). Besides the handle and ID, the menu is similar to the button ID. When a menu trigger event is triggered, the menu ID is included in wm_command or wm_syscommand. You can use getmenuitemid () to obtain the ID of a menu item. When you access a menu item, it is troublesome to use the ID method. A work und is to access the menu item based on the index of the menu item with 0 as the base, and the leftmost is 0, increment progressively to the right, 0 at the top, and progressively downward.
I. Menu Creation
1. Use menu Resources
First, design the menu, load it with loadmenu (), and set it with setmenu (). For example, in the wm_create message of the window:
Hmenu; Hmenu = loadmenu (hinst, makeintresource (idr_menu1 )); Setmenu (hwnd, hmenu ); |
2. Create a menu in the memory menu Template
Some applications allow users to customize menus. This requires that the memory menu template be used to define the menus, and then the loadmenuindirect () function is used to load the memory menu template to obtain the menu handle, the memory menu template consists of a menuitemtemplateheader (or menuex_template_header) structure and several menuitemtemplates (or menuex_template_item) structures.
3. createmenu () function
The createmenu () function returns an hmenu pointer pointing to an empty menu, which is used to add the menu item insertmenuitem () to it ()
Ii. menu display
1. If the hmenu member has been specified during wndclassex window class registration or the setmenu () function is called to set the menu of the parent window for the member, the window is displayed, and the menu is automatically displayed
2. Use the trackpopmenuex () function to display the pop-up menu, for example
Hmenu hpmemu; Hpmenu = loadmenu (hinst, makeintresource (idr_popmenu ); Trackepopmenuex ( Hpmenu, // menu handle Tpm_leftalign, // alignment of menu and pop-up point coordinates X, // The X coordinate of the pop-up point Y, // The Y coordinate of the pop-up point Hwnd, // parent window handle Null ); |
3. messages in the menu display process
Click an item on the menu bar. The parent window receives the wm_inititem message (you can draw a menu in this message), and then the menu is displayed. When you point to a menu item that can pop up a sub-menu, the parent window receives wm_initmenupopup (you can capture this message to draw a sub-menu), and then a sub-menu is displayed. When you move the cursor over the menu, the parent window receives the wm_menuselect message, which contains the index number of the current menu item and can be referenced. Click a menu item to generate the wm_command message. Click a menu item in the mns_notifybypos style to generate the wm_menucommand message. In addition to providing the data provided by the wm_command message, the message also adds a menuinfo structure. Right-click the menu to send the wm_contextmenu message. You can right-click a menu item to generate the wm_menurbuttonup message. (For example, you can capture the message by right-clicking the pop-up menu on the menu item of IE's favorites menu)
3. Dynamic menu changes
1. Insert a new menu item insertmenuitem ()
First fill in the menuinfo Structure
Typedef struct tagmenuiteminfo { Uint cbsize; structure size, sizeof (menuinfo) NT fmask; the item to be obtained or set. Which of the following parameters are affected by the parameter settings. Miim_bitmap, hbmpitem will be used to set or return the bitmap of the button, miim_id, WID setting or return the ID of the menu item Uint FTYPE; set or return the type of menu item, mft_bitmap Bitmap bitmap; mft_string; mft_ownerdraw, send wm_drawitem and wm_measureitem messages to draw menu items Uint fstate; menu item status: mfs_default, default; mfs_grayed, unavailable; Uint WID; the ID of the menu item, which must contain miim_id in fmask Hmenu hsubmenu; the handle of the next menu, which must contain miim_submenu in fmask Hbitmap hbmpchecked; Hbitmap hbmpunchecked; Ulong_ptr dwitemdata; Lptstr dwtypedata; Uint CCH; Hbitmap hbmpitem; } Menuiteminfo, * lpmenuiteminfo; Then call the insertmenu () function. Insertmenu ( Hmenu; Uint nitems; Bool ispisition; // if it is set to true, nitems is the index number of the menu item, false, and nitems is the ID of the menu item Lpmenuiteminfo lpmiinfo; ) |
2. Use the setmenuiteminfo () function to modify menu item attributes.
3. Delete menu items
Deletemenu () and removemenu (). After the menu item is deleted, use drawmenubar () to re-paint the menu and display the update.
Iv. Menu Effects
1. shortcut menu
Capture the wm_contextmenu of the parent window (you can right-click the window to activate this event) and call the Function
Trackpopupmenuex (); trackpopupmenuex (HM,
0, get_x_lparam (lparam), get_y_lparam (lparam), hwnd, null); HM is the menu handle, and get_x_lparam (cannot be used) macro obtains the X coordinates of the mouse when an event occurs, hwnd is the parent window handle
2. Bitmap menu
Add the miim_bitmap flag to the fmask of the menuiteminfo structure. The hbmitem duration specifies a pointer to the bitmap.
5. Self-drawn menu
1. Specify a flag
To implement self-drawing, the style of the menu item must meet the requirements. There are two ways to do this: one is to use the insertmenuitem () function to insert, and the other is to use the setmenuiteminfo () function to modify, no matter which one is used, you must enter a menuiteminfo structure, add miim_ftype to its fmask, and add mft_ownerdraw to its FTYPE. Like the list box, the self-drawing of menu items is designed to transfer a large amount of data. It can be stored in the menuiteminfo structure, set fmask to miim_data, and assign values to the dwitemdata member. The parameters wm_drawitem (lpdrawitemstruct) and wm_measureitem (lpmeasureitemstruct) contain itemdata idioms used to retrieve pre-stored data.
For example:
Menuiteminfo Mi; Hmenu HMN; Mi. fmask = miim_ftype; Milftype = mft_ownerdraw; HMN = getmenu (hwnd ); Setmenuiteminfo (HMN, 0, true, & mi ); |
2. Process wm_measureitem messages
When you click and the menu is about to be displayed, the parent window of the menu receives the wm_measureitem message. to capture this message, you can set the size of the menu item, for example:
Lpmeasureitemstruct lpmis; Lpmis = (lpmeasureitemstruct) lparam; Lpmis-> itemheight = 48 Lpmis-> itemwidth = 144; |
3. Process wm_drawitem messages
The lparam parameter of the wm_drawitem message is an lpdrawitemstruct pointer, which contains information such as the device scenario pointer, menu ID, and status pointing to the menu item to be drawn based on the information, for example:
Lpdrawitemstruct lpdis; HDC hmem; Hbitmap HBM; Hmem = createcompatibledc (lpdis-> HDC ); HBM = loadbitmap (hinst, makeintresource (idb_menumap )); SelectObject (hmem, HBM ); Bitblt (lpdis-> HDC, 0, 0, lpids-> rcitem. Right, lpdis-> rcitem. Bottom, hmem, 0, 0, srccopy ); |