Translation tellmenow
The basic objective of Windows CE-small personal productivity tools-is driving the needs of public controls. Frequent use of time and date in the calendar and task management application results in the control including the date and time Selection Control and calendar control. The small screen of personal productivity tools facilitates space-saving command lines. The command bar control is combined with the rebar Control for ie3.0 to generate a command Belt control. The command Belt control provides more space for menus, buttons, and other controls located at the top of the Windows CE application.
Finally, it is a menu control familiar to Pocket PC developers. In Windows CE. Net 4.2, it is added to the Windows CE public control. After discussing the command bar controls and command belt controls, I will talk about the menu controls.
Command Line
In short, the command bar control combines menus and toolbar. This combination is very valuable, because combining menus and toolbar into one line can save space for Windows CE display screen space. For programmers, the command bar is like a tool bar with many help functions, making programming of public controls easy. When you use a command bar, in addition to the command bar function, you can also use many toolbar messages. Figure 5-1 shows a window with command lines.
Figure 5-1 (Omitted): window with command lines.
CREATE command lines
You can create command lines in several steps. Each step is defined by a special function. Create a command bar, add menus, buttons, other controls, and tooltip, and add the close and help buttons to the right of the command bar.
Call the commandbar_create function to create a command bar. The function prototype is as follows:
Hwnd commandbar_create (hinstance hinst, hwnd hwndparent, int idcmdbar );
The function requires the instance handle of the program, the parent window handle, and the Control ID. If it is successfully created, the function returns the handle of the newly created command bar control. For applications, bare command lines are not very useful. You also need to add menus and buttons to make them useful.
Command bar menu
You can add menus to the command bar by calling one of the following two functions. The first function is
Bool commandbar_insertmenubar (hwnd hwndcb, hinstance hinst, word idmenu, int iButton );
The first two parameters are the command bar handle and the application instance handle. Idmenu is the resource ID of the menu to be loaded to the command bar. The last parameter is the index next to the button on the left of the menu. Because the left end of the command bar should be specified in the Windows CE specification, this parameter should be set to 0, indicating that all buttons are on the right of the menu.
The disadvantage of the commandbar_insertmenubar function is that it requires loading menus from resources. You cannot configure menus at runtime. Of course, it is possible to achieve this goal by loading a virtual menu and using different menu functions to operate the menu content, but here is a simpler method:
Bool commandbar_insertmenubarex (hwnd hwndcb, hinstance hinst, lptstr pszmenu, int iButton );
In commandbar_insertmenubarex, except for the third parameter pszmenu, other parameters are similar to those in commandbar_insertmenubar. This parameter is either the name of the menu resource or the handle of the menu previously created by the program. If pszmenu is a menu handle, the hinst parameter should be null.
Once the menu is loaded into the command bar, you can retrieve the menu handle at any time using the following function:
Hmenu commandbar_getmenu (hwnd hwndcb, int iButton );
The second parameter, iButton, is the index next to the button on the left of the menu. This mechanism provides the ability to recognize multiple menus on command lines. However, according to the Windows CE design specifications, you can only see one menu on the command bar. With the menu handle, you can have many available menu functions to manipulate the menu structure.
If the application wants to modify the menu on the command bar, the application must call
Bool commandbar_drawmenubar (hwnd hwndcb, int iButton );
It forces the menu on the command bar to be repainted. The parameters here use the handle of the command bar and the index of the buttons on the left of the menu. In Windows CE, you must use commandbar_drawmenubar instead of the standard function drawmenubar used to repaint menus in other Windows versions.
Command bar button
Adding a button to a command bar takes two steps, which is similar to adding a button to the toolbar. First, you must add bitmap images for buttons to the command bar. Add a button. Each button corresponds to an image in the previously added bitmap list.
The command bar maintains the bitmap list of buttons in an internal image list. A Bitmap can be added to the image list one by one or contain a group of images in a long and narrow bitmap. For example, for a bitmap containing four 16*16 pixel images, the size is 64*16 pixel. Figure 5-2 shows the layout of the bitmap image.
Figure 5-2: bitmap containing four 16*16 pixel images
You can load a bitmap using the following functions.
Int commandbar_addbitmap (hwnd hwndcb, hinstance hinst, int idbitmap, int inumimage, int iimagewidth, int iimageheight );
The first two parameters are the same as the previous command line functions. They are the command line handle and the instance handle of the executable program. The third parameter, idbitmap, is the resource ID of the bitmap image. The fourth parameter inumimages is the number of images in the loaded bitmap. You can call commandbar_addbitmap multiple times to load multiple bitmaps to the same command bar as needed. The last two parameters are the image size, both of which are set to 16.
There are two predefined bitmaps that provide many images that are usually used in command and toolbar. By setting hinst in commandbar_addbitmap to hinst_commctrl and idbitmap to idb_std_small_color or idb_view_small_color, you can load the two bitmaps. Figure 5-3 shows the images contained in the two bitmaps. The buttons in the first line contain images from the standard bitmap, while the buttons in the second line contain bitmaps in the standard window.
Figure 5-3: Images in two standard bitmaps provided by the public control DLL.
The index values of these images are defined in commctrl. H, so you do not need to know the exact order of them in the bitmap. These constants are defined as follows:
Constant definition of accessing standard bitmap
Std_cut edit/cut button Image
Std_copy edit/copy button Image
Std_paste edit/paste button Image
Std_undo edit/Undo button Image
Std_redow edit/Redo button Image
Std_delete edit/delete button Image
Std_filenew file/New button Image
Std_fileopen file/OPEN button Image
Std_filesave file/Save button Image
Std_printpre print preview button Image
Std_properties Properties button Image
Std_help HELP button (use commandbar_addadornments function to add a Help button to the command bar .)
Std_find find button Image
Std_replace replace button Image
Std_print print button Image
Constant definition of Bitmap in access standard window
View_largeicons view/large icons button Image
View_smallicons view/small icons button Image
View_list view/list button Image
View_details view/details button Image
View_sortname sort by name button Image
View_sortsize sort by size button Image
View_sortdate sort by date button Image
View_sorttype sort by type button Image
View_parentfolder go to parent folder button Image
View_netconnect connect network drive button Image
View_netdisconnect disconnect network drive button Image
View_newfolder create new folder button Image
Images loaded into command lines are referenced by their indexes in the image list. For example, if the loaded bitmap contains five images and you want to reference the fourth image in the bitmap, the 0-based index value is 3.
If you call commandbar_addbitmap multiple times to add multiple bitmap image sets to the command bar, add the preceding Image number to the index of the image in the current list, that is, the reference value of the current image. For example, commandbar_addbitmap is called twice to add two image sets. Five images are added for the first time, and four images are added for the second time, to reference the third image in the second image set, you need to add the index of the first Bitmap (2) to the total number of images in the first image (5). The reference is 7.
Once a bitmap is loaded, you can use one of the following two functions to load the button.
The first function is:
Bool commandbar_addbuttons (hwnd hwndcb, uint unumbuttons, lptbbutton lpbuttons );
Commandbar_addbuttons can add a series of buttons to the command bar at a time. This function imports a number of buttons and a pointer to the tbbutton structure array. Each element in the array describes a button. The tbbutton structure is defined as follows:
Typedef struct {
Int ibitmap;
Int idcommand;
Byte fsstate;
Byte fsstyle;
DWORD dwdata;
Int istring;
} Tbbutton;
Ibitmap is the bitmap image used by the button. That is, the 0-based index in the image list I just explained. The second parameter is the command ID of the button. When you click a button, the ID is sent to the parent window through the wm_command message.
The fsstate field defines the initial status of the button. The values of this field are as follows:
The tbstate_enabled button is valid. If this flag is not specified, the button becomes invalid and grayed out.
The tbstate_hidden button is invisible to the command bar.
The tbstate_pressed button is displayed as the underpressure status.
The tbstate_checked button is selected. This status is available only when the button is tbstyle_checked.
The tbstate_indeterminate button is grayed out.
The last flag tbstate_wrap, which is defined in the document, is invalid in the command bar. This flag is used when the toolbar is folded when more than one row is exceeded.
Fsstyle specifies the initial style of the button to indicate how the button responds to the action. A button can be defined as a standard lower pressure button, a check box button, a drop-down list box, and a check box similar to a single button. At this time, only one button in a group can be selected. The possible flag for fsstyle is as follows:
Tbstyle_button standard push button
Tbstyle_check check box. Each time you click this button, switch between select and unselect.
Tbstyle_group defines the start button of a group of buttons.
Tbstyle_checkgroup indicates that it is one of a group of check boxes, and the behavior is similar to that of a single button. Only one button can be selected at a time.
Tbstyle_dropdown drop-down list box.
The size of the tbstyle_autosize button depends on the text of the button.
Tbstyle_sep defines a separator bar and inserts a small space between buttons.
The dwdata field in the tbbutton structure is the value defined by the application. By using tb_setbuttoninfo and tb_getbuttoninfo messages, the application can set and query this value. Istring defines the index in the command bar character array containing the button text. This field can also be added as a pointer to the button text string.
Another function that adds a button to a command bar is as follows:
Bool commanbar_insertbutton (hwnd hwndcb, int iButton, lptbbutton lpbutton );
This function inserts a button into the left of the button corresponding to iButton in the command bar. In addition to the lpbutton pointing to a single tbbutton structure, the parameters of this function are similar to commandbar_addbuttons. IButton provides the position of the new button on the command bar.
Use the command bar button
In addition to the drop-down button, when you press a command bar button, the command bar will send a wm_command message to its parent window. Therefore, the button on the processing command bar is like processing the menu command. In fact, because many buttons on a command bar have equivalent menu commands, it is customary to use the same command ID for buttons and menus of the same function, this reduces the need for specific processing of the command bar button.
The command bar maintains the selection status of a single check box and a set of check box buttons. After the buttons are added to the command bar, you can use the following two messages to query or set their statuses: tb_isbuttonchecked and tb_checkbutton. (The prefix tb_implies the close relationship between the command bar and the Toolbar Control .) Put the ID of the button to be queried in the wparam parameter and send the tb_isbuttonchecked message as follows:
Fchecked = sendmessage (hwndcb, tb_isbuttonchecked, WID, 0 );
Hwndcb is the handle of the command bar containing buttons. If the returned value is non-zero, the button is selected. To set the button to the selection status, you can send a tb_checkbutton message to the command bar, as follows:
Sendmessage (hwndcb, tb_checkbutton, WID, true );
To cancel the selection, replace "true" with "false.
Invalidate button
Windows CE allows you to easily modify the appearance of invalid buttons on a command bar or toolbar. The command bar and tool bar maintain two image lists: the list of standard images previously described and a list of invalid images, which are used to store images used for failed buttons.
To use this feature, you need to create and load 2nd image lists for invalid buttons. The easiest way is to use the technology described when talking about commandbar_addbitmap to create an image list for the normal state of the button. (You can use the tb_loadimages message to load the image list in the toolbar .) Once the image list is completed, simply copy the original image list and modify the bitmap in the image list to create an invalid copy of the original bitmap. Next, load the new image list back to the command bar or toolbar. The following code snippet shows how to do this:
Hbitmap hbmp, hmask;
Himagelist hildisabled, hilenabled;
// Load the bitmap and mask to be used in the disabled image list.
Hbmp = loadbitmap (hinst, text ("discross "));
Hmask = loadbitmap (hinst, text ("dismask "));
// Get the standard image list and copy it.
Hilenabled = (himagelist) sendmessage (hwndcb, tb_getimagelist, 0, 0 );
Hildisabled = imagelist_duplicate (hilenabled );
// Replace one bitmap in the disabled list.
Imagelist_replace (hildisabled, view_list, hbmp, hmask );
// Set the disabled image list.
Sendmessage (hwndcb, tb_setdisabledimagelist, 0, (lparam) hildisabled );
The code first loads a bitmap and a mask bitmap, which will be used to replace the bitmap in the invalid image list. By sending the tb_getimagelist message to the command bar, you can obtain the current image list, and then use imagelist_duplicate to copy an image list. Finally, replace the corresponding image in the image list with the previously loaded bitmap.
In this example, only one bitmap is replaced, but many bitmaps can be replaced in actual applications. If some bitmaps are replaced, creating an invalid image list from the beginning may be easier than copying a standard image list and replacing some bitmaps. Once the new image list is created, you can load the List into the command bar by sending the tb_setdisabledimagelist message. The above code is equally valid for command lines under Windows CE.
Drop-down button
The drop-down button is more complex than the standard button on the command bar. For the user, it is a button. When it is pressed, a list item is displayed for the user to select. For programmers, the drop-down button is actually a combination of buttons and menus. When a user clicks a button, a menu is displayed. Unfortunately, the command bar has limited support for the drop-down button. It only provides the function of modifying the button appearance to indicate a drop-down button and the function of sending a specific notification when a user clicks the button. In addition, the application determines how to display the menu.
By sending a wm_notify message with the tbn_dropdown notification code, you can click the drop-down button to send the notification to the parent window of the command bar. After receiving the tbn_dropdown notification, the parent window must immediately create a pop-up menu under the drop-down button marked in the notification message. The menu is filled with appropriate options in the parent window. When a menu item is selected, the wm_command message is sent, indicating that the menu item is selected and the menu is about to disappear. To better understand how to handle the drop-down notification, you can view the following process for handling the tbn_dropdown notification:
Lresult donotifymain (hwnd, uint wmsg, wparam, lparam)
{
Lpnmhdr ppolicyheader;
Lpnmtoolbar ppolicytoolbar;
Rect;
Tpmparams TPM;
Hmenu;
// Get pointer to handle Y message header.
Ppolicyheader = (lpnmhdr) lparam;
If (ppolicyheader-> code = tbn_dropdown ){
// Get pointer to toolbar using Y structure.
Ppolicytoolbar = (lpnmtoolbar) lparam;
// Get the rectangle of the drop-down button.
Sendmessage (ppolicyheader-> hwndfrom, tb_getrect,
Ppolicytoolbar-> iItem, (lparam) & rect );
// Convert rect to screen coordinates. The rect is
// Considered here to be an array of 2 point structures.
Mapwindowpoints (pnotifyheader-> hwndfrom, hwnd_desktop,
(Lppoint) & rect, 2 );
// Prevent the menu from covering the button.
TPM. cbsize = sizeof (TPM );
Copyrect (& TPM. rcexclude, & rect );
// Load the menu resource to display under the button.
Hmenu = getsubmenu (loadmenu (hinst, text ("popmenu"), 0 );
// Display the menu. This function returns after
// User makes a selection or dismisses the menu.
Trackpopupmenuex (hmenu, tpm_leftalign | tpm_vertical,
Rect. Left, rect. Bottom, hwnd, & TPM );
}
Return 0;
}
After the Code determines that it is a tbn_dropdown notification, the first thing to do is to get the rectangle of the drop-down button. This rectangle is obtained so that the drop-down menu can be located immediately below the button. To achieve this purpose, the program sends a tb_getrect message to the command bar. The wparam parameter is the ID of the drop-down button, and the lparam parameter is a pointer to the rectangular structure.
Because the returned rectangle is based on the coordinates of the parent window, And the pop-up menu is the screen coordinate, the coordinates must be converted. This can be done using the following functions:
Mapwindowpoints (hwnd hwndfrom, hwnd hwndto, lppoint lppoints, uint cpoints );
The first parameter is the window handle of the original coordinate. The second parameter is the window handle where the coordinates to be converted are located. The third parameter is the pointer to the coordinate array to be converted. The last parameter is the number of dots in the array. In the program I show, the window handle is the command bar handle and the desktop window handle.
Once the rectangle is converted to the desktop coordinate, you can create a pop-up menu or context-related menu. To do this, you first need to load the menu from the resource and call trackpopupmenuex to display the menu. If you can recall the discussion of trackpopupmenuex in the previous chapter, you will know that the tpmparams structure contains a rectangle that will not be overwritten by the menu display. Right time, the rectangle is set to the size of the drop-down button, so that the button will not be overwritten by the pop-up menu. Fuflags contains many values to define the menu position. For the drop-down button, only the tpm_vertical flag is required. If tpm_vertical is set, the menu tries its best to keep more horizontal areas of the non-overwrite rectangle. The trackpopupmenuex function will not return until the menu item is selected or when the user clicks elsewhere on the screen and the menu disappears. (To be continued)