In this lesson, we'll add a menu to our application.
Theory:
Menus can be said to be one of the most important elements of Windows. With it, users can easily select an action command. The user can simply read all the menu items to understand the approximate functionality provided by the application, and can immediately operate without having to read the manual. Because the menu gives the user a convenient way, So when you add a menu to your application, you should follow the general criteria. For example, the first two menu items are "File" and "Edit", and finally "help," where you can insert the menu item you want to define. If the menu command that you run pops up a dialog box, Then you should add the ellipsis (...) after the menu item. menu is a resource, in addition to the menu there are other like dialog boxes, strings, icons, bitmap resources and so on. The linker will add resources to the executable program at the time of the link, and in the end our execution program includes both machine instructions and resources. You can write script files in any text editor, in a file, you can specify the appearance of the resource and other properties. Of course, a more intuitive approach is to use resource editors, which are usually packaged in a compilation environment, like Visual C + +, Borland C + +, and so on, with resource editors. We can define a menu resource in the following ways:
MyMenu MENU
{
[menu list here]
}
This is very similar to the definition of a struct in C language. MyMenu is similar to a defined variable, and the menu is similar to a keyword. Of course you can use a different approach, which is to replace the curly braces with the begin and end, which is the same as the Pascal language style.
In the list of menu items is a large string of MenuItem and Popup statements. MenuItem defines a menu item that does not activate the dialog box when selected. Its syntax is as follows:
MENUITEM "&text", ID [, Options]
It starts with the keyword MenuItem, immediately after MenuItem is the name string of the menu item, the first character after the symbol "&" will be underlined, and it is also the shortcut key for the menu item. The role of IDs when the menu is selected, the Windows message processing process is used to differentiate between menu items. There is no doubt that the ID number must be unique. Options have the following properties to choose from:
Grayed means that the menu item is inactive, that is, the WM_COMMAND message is not generated when it is selected. The menu is displayed in gray.
INACTIVE means that the menu item is inactive, that is, the WM_COMMAND message is not generated when it is selected. The menu appears in normal color.
Menubreak the menu item and the following items appear in the new column. (Translator Note: More difficult to describe, please do the experiment.) )
Help the menu item and the subsequent menus are right-aligned. (Translator Note: I compile the menu item with this flag under WINDOWS2000, it doesn't seem to work)
You can use the above logo bits individually, or you can put them together. Of course inactive and grayed cannot be used at the same time. The syntax for popup is as follows:
POPUP "&text" [,options]
{
[menu list]
}
Popup defines a menu item that pops up a submenu when it is selected. In addition, there is a special type of MenuItem statement MenuItem SEPARATOR, which indicates that a divider line is drawn in the menu item position. Once you have defined the menu, you can use the menu resources defined in the script in your program. You can use them in two places in the program, or in two ways:
In the member lpszmenuname of the WNDCLASSEX structure. For example, you have a menu "firstmenu" that you can contact to your window as follows:
. DATA
MenuName db "Firstmenu", 0
....... ...........
.................. ...
. CODE
... ........... ........
mov wc.lpszmenuname, OFFSET MenuName
...... .............. The
indicates the handle of the menu in the CreateWindowEx function:
. DATA
MenuName db "Firstmenu", 0
hmenu hmenu?
...........................
.................. ...
. CODE
... ........... ........
Invoke LoadMenu, hinst, offset MenuName
mov hmenu, eax
Invoke Createwindowex,null,offset clsname,\
Offset Caption, ws_overlappedwindow,\
cw_usedefault,cw_usedefault,\
cw_usedefault,cw_usedefault,\
NULL,\
Hmenu,\ the
hinst,\
null\
...
., ............ ...
You might ask, what's the difference between the two? When you use the first method, all windows that derive from the window class will have the same menu, because they are specified in the window class. If you want to have a different menu from a window that derives from the same class, use the second method, in which the menu specified by the function CreateWindowEx "overrides" the menu specified in the WNDCLASSEX structure. Next, let's look at how it notifies Windows window procedures when a user selects a menu item: When a user selects a menu item, the Windows window process receives a WM_COMMAND message, and the incoming parameter wparam's bottom byte contains the ID number of the menu item. Well, it's all about the menu item, so let's practice.