Programming Microsoft Windows Ce Net 3Rd [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming Microsoft Windows Ce Net 3Rd [Electronic resources] - نسخه متنی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Menus


Menus are a mainstay of Windows input. Although each application might have a different keyboard and stylus interface, almost all have sets of menus that are organized in a structure familiar to the Windows user.

Windows CE programs use menus a little differently from other Windows programs, the most obvious difference being that in Windows CE, menus aren't part of the standard top-level window. Instead, menus are attached to a command bar or menu bar control that has been created for the window. Other than this change, the functions of the menu and the way menu selections are processed by the application match the other versions of Windows, for the most part. Because of this general similarity, I give you only a basic introduction to Windows menu management in this section.

Creating a menu is as simple as calling

HMENU CreateMenu (void);

The function returns a handle to an empty menu. To add an item to a menu, two calls can be used. The first

BOOL AppendMenu (HMENU hMenu, UINT fuFlags, UINT idNewItem,
LPCTSTR lpszNewItem);

appends a single item to the end of a menu. The fuFlags parameter is set with a series of flags indicating the initial condition of the item. For example, the item might be initially disabled (thanks to the MF_GRAYED flag) or have a check mark next to it (courtesy of the MF_CHECKED flag). Almost all calls specify the MF_STRING flag, indicating that the lpszNewItem parameter contains a string that will be the text for the item. The idNewItem parameter contains an ID value that will be used to identify the item when it's selected by the user or to indicate that the state of the menu item needs to be changed.

Another call that can be used to add a menu item is this one:

BOOL InsertMenu (HMENU hMenu, UINT uPosition, UINT uFlags,
UINT uIDNewItem, LPCTSTR lpNewItem);

This call is similar to AppendMenu, with the added flexibility that the item can be inserted anywhere within a menu structure. For this call, the uFlags parameter can be passed one of two additional flags: MF_BYCOMMAND or MF_BYPOSITION, which specify how to locate where the menu item is to be inserted into the menu.

Menus can be nested to provide a cascading effect. To add a cascading menu, or submenu, create the menu you want to attach using

HMENU CreatePopupMenu (void);

Then use InsertMenu, or AppendMenu to construct the menu. Then insert or append the submenu to the main menu using either InsertMenu or AppendMenu with the MF_POPUP flag in the flags parameter. In this case, the uIDNewItem parameter contains the handle to the submenu, while lpNewItem contains the string that will be on the menu item.

You can query and manipulate a menu item to add or remove check marks or to enable or disable it by means of a number of functions. This function,

BOOL EnableMenuItem (HMENU hMenu, UINT uIDEnableItem, UINT uEnable);

can be used to enable or disable an item. The flags used in the uEnable parameter are similar to the flags used with other menu functions. Under Windows CE, the flag you use to disable a menu item is MF_GRAYED, not MF_DISABLED. The function

DWORD CheckMenuItem (HMENU hmenu, UINT uIDCheckItem, UINT uCheck);

can be used to check and uncheck a menu item. Many other functions are available to query and manipulate menu items. Check the SDK documentation for more details.

The following code fragment 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 has been created, it can be displayed with the TrackPopupMenu function, prototyped as

BOOL TrackPopupMenuEx (HMENU hmenu, UINT uFlags, int x, int y, 
HWND hwnd, LPTPMPARAMS lptpm);

The first parameter is the handle of the menu. The uFlags parameter sets the alignment for the menu in relation to the position parameters x and y. Another flag, TPM_RETURNCMD, causes the function to return the ID value of the selected menu item instead of generating a WM_COMMAND message. The hwnd parameter is the handle to the window that will receive all messages relating to the menu, including the resultant WM_COMMAND if the user selects a menu item. The final item, lptpm, points to a TPMPARAMS structure that contains a size value and a rectangle structure. The rectangle structure defines the rectangle on the screen that the menu shouldn't cover. This parameter can be null if no exclusion rectangle needs to be specified.


Handling Menu Commands


When a user selects a menu item, Windows sends a WM_COMMAND message to the window that owns the menu. The low word of the wParam parameter contains the ID of the menu item that was selected. The high word of wParam contains the notification code. For a menu selection, this value is always 0. The lParam parameter is 0 for WM_COMMAND messages sent due to a menu selection. So to act on a menu selection, a window needs to field the WM_COMMAND message, decode the ID passed, and act according to the menu item that was selected.

Now that I've covered the basics of menu creation, you might wonder where all this menu creation code sits in a Windows program. The answer is, it doesn't. Instead of dynamically creating menus on the fly, most Windows programs simply load a menu template from a resource. To learn more about this, let's spend the remainder of this chapter looking at resources.

/ 169