[Translation]-wince programming (3rd edition)-4.4 menu

Source: Internet
Author: User

TranslationTellmenow

Menus are important in Windows input. Although each application may have different keyboard and pen interfaces, almost all applications organize menus according to the structure familiar to Windows users.

Using menus in Windows CE programs is somewhat different from other Windows programs. The most significant difference is that menus are not part of standard top-level windows in Windows CE. Instead, the menu is bound to the command bar or menu bar Control created for the window. In addition to this change, the menu functions and menu selection methods are the same for most Windows versions. In view of this general similarity, I will only give a basic introduction to Windows menu management in this section.

To create a menu, you just need to call hmenu createmenu (void). This function returns an empty menu handle. To add an item to a menu, you can use two call methods. The first is to call the bool appendmenu (hmenu, uint fuflags, uint idnewitem, lpctstr lpsznewitem). A separate menu item will be added at the end of the menu. You can use a series of flags to set the fuflages parameter to indicate the initial situation of the menu item. For example, a menu item may start with invalid (using the mf_grayed flag) or an option marked next to it (using the mf_checked flag ). The mf_string flag is specified for almost all calls, indicating that the lpsznewitem parameter is a string containing the menu item text. Idnewitem indicates the selected menu item ID or the status of the menu item to be changed.

Another way to add menu items is to call bool insertmenu (hmenu, uint uposition, uint uflags, uint uidnewitem, lptstr lpnewitem );
This function is similar to appendmenu, But it increases flexibility. A menu item can be inserted to any position of the menu. For this function, one of the two additional flags can be passed to uflags: mf_bycommand or mf_byposition, which indicates how to determine the position of the menu item in the menu.

Menus can be nested to achieve cascading effect. To add a cascading menu or sub-menu, you can use hmenu createpopmenu (void) to create the menu you want to bind, and then use insertmenu or appendmenu to construct the menu, then, you can set the flag to mf_popup and call insertmenu or appendmenu to insert or attach the sub-menu to the main menu. In this case, uidnewitem contains the sub-menu handle, while lpnewitem contains the string displayed in the menu item.

There are many functions that allow you to query or manipulate menu items to Add/Remove selection tags, valid/invalid menu items, and so on.
Enablemenuitem is used to make the menu item valid/invalid.
The function prototype is as follows: bool enablemenuitem (hmenu, uint uidenableitem, uint uenable );
The identifier used in uenable is similar to that used in other menu functions. In Windows CE, The mf_grayed instead of the mf_disabled flag is used to invalidate the menu item.

Use the checkmenuitem function to select menu items/remove selections.
The function prototype is as follows: DWORD checkmenuitem (hmenu, uint uidcheckitem, uint ucheck );
Many other functions can be used to query and manipulate menu items. For more details, see the SDK documentation.

The following code snippet creates a simple menu structure:
Hmainmenu = createmenu ();

Hmenu = createpopupmenu ();
Appendmenu (hmenu, mf_string | mf_enabled, 100, text ("& New "));
Appendmenu (hmenu, mf_string | mf_enabled, 101, text ("& Open "));
Appendmenu (hmenu, mf_string | mf_enabled, 101, text ("& Save "));
Appendmenu (hmenu, mf_string | mf_enabled, 101, text ("E & xit "));

Appendmenu (hmainmenu, mf_string | mf_enabled | mf_popup, (uint) hmenu, text ("& file "));

Hmenu = createpopupmenu ();
Appendmenu (hmenu, mf_string | mf_enabled, 100, text ("C & ut "));
Appendmenu (hmenu, mf_string | mf_enabled, 101, text ("& copy "));
Appendmenu (hmenu, mf_string | mf_enabled, 101, text ("& Paste "));

Appendmenu (hmainmenu, mf_string | mf_enabled | mf_popup, (uint) hmenu, text ("& edit "));

Hmenu = createpopupmenu ();
Appendmenu (hmenu, mf_string | mf_enabled, 100, text ("& about "));

Appendmenu (hmainmenu, mf_string | mf_enabled | mf_popup, (uint) hmenu, text ("& Help "));

Once a menu is created, you can use trackpopupmenu to display it. The function prototype is as follows:
Bool trackpopupmenu (hmenu, uint uflags, int X, int y, hwnd, lptpmparams lptpm );

The first parameter is the menu handle. Based on the location parameters X and Y, the flag uflags is used to set the menu alignment. One flag is tpm_returncmd, which allows the function to return the ID of the selected menu item instead of sending a wm_command message. Hwnd is the window handle for receiving all menu-related messages. It also includes the wm_command message generated when the menu item is selected. The last parameter, lptpm, is a pointer to the tpmparams structure, which contains a size value and a rectangular structure. The rectangle structure defines the screen rectangle that the menu cannot overwrite. If there is no rectangle to be excluded, this parameter can be null.

Process menu commands
When you select a menu item, Windows will send a wm_command message to the window that owns the menu. Wparam's low bitwise contains the ID of the selected menu item. Its high-bitwise value contains the notification Code. For the "menu selection" action, this value is always 0. The wm_command message triggered by the selection menu. Its lparam is 0. Therefore, in response to a menu selection action, the window needs to respond to the wm_command message, parse the input ID, and respond according to the selected menu item.

Now that I have involved in the basic menu creation process, you may want to know where the menu creation code is in windows. My answer is, it does not exist. In addition to dynamically creating menus during running, most Windows programs simply load a menu template from resources. To learn more about resources, let's take a look at resources with the rest of this chapter.

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.