📄 lion-tutorial08.htm
字号:
<html>
<head>
<link rel="stylesheet" href="../../asm.css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Iczelion's win32 asm tutorial</title>
</head>
<body bgcolor="#FFFFFF" background="../../images/back01.jpg">
<p align="center">Tutorial 8: Menu</p>
<hr size="1">
In this tutorial, we will learn how to incorporate a menu into our window. <br>
Download the <a href="files/tut08-1.zip">example 1</a> and <a href="files/tut08-2.zip">example
2.</a>
<h3> Theory:</h3>
Menu is one of the most important component in your window. Menu presents a list
of services a program offers to the user. The user doesn't have to read the manual
included with the program to be able to use it, he can peruse the menu to get
an overview of the capability of a particular program and start playing with it
immediately. Since a menu is a tool to get the user up and running quickly, you
should follow the standard. Succintly put, the first two menu items should be
File and Edit and the last should be Help. You can insert your own menu items
between Edit and Help. If a menu item invokes a dialog box, you should append
an ellipsis (...) to the menu string. <br>
Menu is a kind of resource. There are several kinds of resources such as dialog
box, string table, icon, bitmap, menu etc. Resources are described in a separated
file called a resource file which normally has .rc extension. You then combine
the resources with the source code during the link stage. The final product is
an executable file which contains both instructions and resources. <br>
You can write resource scripts using any text editor. They're composed of phrases
which describe the appearances and other attributes of the resources used in a
particular program Although you can write resource scripts with a text editor,
it's rather cumbersome. A better alternative is to use a resource editor which
lets you visually design resources with ease. Resource editors are usually included
in compiler packages such as Visual C++, Borland C++, etc. <br>
You describe a menu resource like this: <br>
<ul>
<b>MyMenu MENU</b> <br>
<b>{</b> <br>
<b> [menu list here]</b> <br>
<b>}</b>
</ul>
C programmers may recognize that it is similar to declaring a structure. <b>MyMenu</b>
being a menu name followed by <b>MENU</b> keyword and menu list within curly brackets.
Alternatively, you can use BEGIN and END instead of the curly brackets if you
wish. This syntax is more palatable to Pascal programmers. <br>
Menu list can be either <b>MENUITEM</b> or <b>POPUP</b> statement. <br>
<b>MENUITEM</b> statement defines a menu bar which doesn't invoke a popup menu
when selected.The syntax is as follows:
<ul>
<b>MENUITEM "&text", ID [,options]</b>
</ul>
It begins by MENUITEM keyword followed by the text you want to use as menu bar
string. Note the ampersand. It causes the character that follows it to be underlined.
Following the text string is the ID of the menu item. The ID is a number that
will be used to identify the menu item in the message sent to the window procedure
when the menu item is selected. As such, each menu ID must be unique among themselves.
<br>
Options are optional. Available options are as follows:
<dl>
<ul>
<li> <b>GRAYED</b> The menu item is inactive, and it does not generate
a WM_COMMAND message. The text is grayed.</li>
<li> <b>INACTIVE</b> The menu item is inactive, and it does not generate a
WM_COMMAND message. The text is displayed normally.</li>
<li> <b>MENUBREAK</b> This item and the following items appear on a
new line of the menu.</li>
<li> <b>HELP</b> This item and the following items are right-justified.</li>
</ul>
You can use one of the above option or combine them with "or" operator. Beware
that <b>INACTIVE</b> and <b>GRAYED</b> cannot be combined together. <br>
<b>POPUP </b>statement has the following syntax: <br>
<dl><b>POPUP "&text" [,options]</b> <br>
<b>{</b> <br>
<b> [menu list]</b> <br>
<b>}</b></dl>
POPUP statement defines a menu bar that, when selected, drops down a list of
menu items in a small popup window. The menu list can be a <b>MENUTIEM</b> or
<b>POPUP</b> statement. There's a special kind of <b>MENUITEM</b> statement,
<b>MENUITEM SEPARATOR</b>, which will draw a horizontal line in the popup window.
<br>
The next step after you are finished with the menu resource script is to reference
it in your program. <br>
You can do this in two different places in your program.
<ul>
<li> In lpszMenuName member of WNDCLASSEX structure. Say, if you have a menu
named "FirstMenu", you can assigned the menu to your window like this:</li>
<ul>
<ul>
<b>.DATA</b>
<ul>
<b>MenuName db "FirstMenu",0</b>
</ul>
<b>...........................</b> <br>
<b>...........................</b> <br>
<b>.CODE</b>
<ul>
<b>...........................</b> <br>
<b>mov wc.lpszMenuName, OFFSET MenuName</b> <br>
<b>...........................</b>
</ul>
</ul>
</ul>
<li> In menu handle parameter of CreateWindowEx like this:</li>
<ul>
<ul>
<b>.DATA</b>
<ul>
<b>MenuName db "FirstMenu",0</b> <br>
<b>hMenu HMENU ?</b>
</ul>
<b>...........................</b> <br>
<b>...........................</b> <br>
<b>.CODE</b>
<ul>
<b>...........................</b> <br>
<b>invoke LoadMenu, hInst, OFFSET MenuName</b> <br>
<b>mov hMenu, eax</b> <br>
<b>invoke CreateWindowEx,NULL,OFFSET ClsName,\</b> <br>
<b>
OFFSET Caption, WS_OVERLAPPEDWINDOW,\</b> <br>
<b>
CW_USEDEFAULT,CW_USEDEFAULT,\</b> <br>
<b>
CW_USEDEFAULT,CW_USEDEFAULT,\</b> <br>
<b>
NULL,\</b> <br>
<b> hMenu,\</b>
<br>
<b>
hInst,\</b> <br>
<b>
NULL\</b> <br>
<b>...........................</b>
</ul>
</ul>
</ul>
</ul>
So you may ask, what's the difference between these two methods? <br>
When you reference the menu in the WNDCLASSEX structure, the menu becomes the
"default" menu for the window class. Every window of that class will have the
same menu. <br>
If you want each window created from the same class to have different menus,
you must choose the second form. In this case, any window that is passed a menu
handle in its CreateWindowEx function will have a menu that "overrides" the
default menu defined in the WNDCLASSEX structure. <br>
Next we will examine how a menu notifies the window procedure when the user
selects a menu item. <br>
When the user selects a menu item, the window procedure will receive a WM_COMMAND
message. The low word of wParam contains the menu ID of the selected menu item.
<br>
Now we have sufficient information to create and use a menu. Let's do it.
<h3> Example:</h3>
The first example shows how to create and use a menu by specifying the menu
name in the window class.
<p><b>.386</b> <br>
<b>.model flat,stdcall</b> <br>
<b>option casemap:none</b>
<p><b>WinMain proto :DWORD,:DWORD,:DWORD,:DWORD</b>
<p><b>include \masm32\include\windows.inc</b> <br>
<b>include \masm32\include\user32.inc</b> <br>
<b>include \masm32\include\kernel32.inc</b> <br>
<b>includelib \masm32\lib\user32.lib</b> <br>
<b>includelib \masm32\lib\kernel32.lib</b>
<p><b>.data</b> <br>
<b>ClassName db "SimpleWinClass",0</b> <br>
<b>AppName db "Our First Window",0</b> <br>
<b>MenuName db "FirstMenu",0
; The name of our menu in the resource file.</b> <br>
<b>Test_string db "You selected Test menu item",0</b> <br>
<b>Hello_string db "Hello, my friend",0</b> <br>
<b>Goodbye_string db "See you again, bye",0</b>
<p><b>.data?</b> <br>
<b>hInstance HINSTANCE ?</b> <br>
<b>CommandLine LPSTR ?</b>
<p><b>.const</b> <br>
<b>IDM_TEST equ 1
; Menu IDs</b> <br>
<b>IDM_HELLO equ 2</b> <br>
<b>IDM_GOODBYE equ 3</b> <br>
<b>IDM_EXIT equ 4</b>
<p><b>.code</b> <br>
<b>start:</b> <br>
<b> invoke GetModuleHandle, NULL</b> <br>
<b> mov hInstance,eax</b> <br>
<b> invoke GetCommandLine<br>
mov CommandLine,eax</b> <br>
<b> invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</b>
<br>
<b> invoke ExitProcess,eax</b>
<p><b>WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</b>
<br>
<b> LOCAL wc:WNDCLASSEX</b> <br>
<b> LOCAL msg:MSG</b> <br>
<b> LOCAL hwnd:HWND</b> <br>
<b> mov wc.cbSize,SIZEOF WNDCLASSEX</b> <br>
<b> mov wc.style, CS_HREDRAW or CS_VREDRAW</b>
<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -