Tutorial 8: Menu
In this Tutorial, We Well Learn How To Incorporate a menu Into company.
Download the
EXAMPLE 1 and
EXAMPLE 2.
Theory:.. 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 does not 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 WOULD BE HELP. You can INSERT YOWN MENU ITEMS BETWEEN EDIT AND HELP. IF A Menu Item Invokes a Dialog Box, You Should Append An Ellipsis (...) to the menu string.
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.
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.
You Describe a menu Resource Like this:
Mymenu Menu
{
[Menu List Here]
}
C Programmers May Recognize That It is Similar To Declaring A Structure.
Mymenu Being a menu name Followed by
Menu Keyword and Menu List With, You Can Use Brackets if you wish. This syntax is more paatable to Pascal Programmers.
Menu List Can Be Either
MenuItem or
Popup Statement.
Menuitem Statement Defines A Menu Bar Which Doesn't Invoke A Popup Menu When SELECTED.THE SYNTAX IS FOLLOWS:
Menuitem "& Text", ID [Options]
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 ......................... ..
Options Are Optional. Available Options Are As Follows:
GRAYED The menu item is inactive, and it does not generate a WM_COMMAND message. The text is grayed. INACTIVE The menu item is inactive, and it does not generate a WM_COMMAND message. The text is displayed normally. MENUBREAK This item and the following items Appear on a new line of the menu. Help this item and the following items area right-justified. You can use one of the Above option or combine the with "or" operator. BEWARE THAT
INACTIVE AND
Grayed Cannot Be Combined Together.
Popup Statement Has The Following Syntax:
Popup "& Text" [, Options]
{
[Menu List]
}
Popup Statement Defines A Menu Bar That, WHEN SEEN SEEN SEEN SEEN SEEN SEHEN A List of Menu Items in A Small Popup Window. The Menu List Can Be A
Menutiem OR
Popup Statement. There's a special kind of
Menuitem Statement,
Menuitem Separator, Which Will Draw A Horizontal Line in the popup window.
The Next Step After you are finished with the menu resource script is to reasonen it in your program.
You can do this in two diffram.
In lpszmenuname Member of Wndclassex Structure. Say, IF you have a menu name "firstmenu", you can worth, you can do, you:
.DATA
Menuname DB "Firstmenu", 0
.........................
.........................
.Code
.........................
MOV wc.lpsz Gene, Offset Menuname
.........................
In Menu Handle Parameter of CreateWindowex Like this:
.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, /
Hinst, /
NULL /
.........................
SO you may ask, What's the Difference Between these Two Methods?
......................... ..
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.
Next we will examine how a menu notifies the window procedure when the user selects a menu item.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 .
Now We Have Sufficient Information To Create and Use A Menu. Let's do it.
EXAMPLE: The First Example Shows How To create and use a menu by specifying the menu name in the window class.
.386 .Model flat, Stdcall Option Casemap: NONE
Winmain Proto: DWORD,: DWORD,: DWORD,: DWORD
Include /masm32/include/windows.inc include /masm32/include/user32.inc include /masm32/include/kernel32.inc includelib /masm32/lib/user32.lib incrudelib /masm32/lib/kernel32.lib
.data ClassName db "SimpleWinClass", 0 AppName db "Our First Window", 0 MenuName db "FirstMenu", 0;. The name of our menu in the resource file Test_string db "You selected Test menu item", 0 Hello_string db " Hello, My Friend ", 0 goodbye_string db" see you again, bye ", 0
.DATA? Hinstance Hinstance? Commandline LPSTR?
.const idm_test EQU 1; MENU IDS IDM_HELLO EQU 2 IDM_GOODBYE EQU 3 IDM_EXIT EQU 4
. Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Getcommandline Mov Commandline, Eax Invoke Winmain, Hinstance, Null, CommandLine, SW_SHOWDEFAULT INVOKE EXITPROCESS, EAX
WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD LOCAL wc: WNDCLASSEX LOCAL msg: MSG LOCAL hwnd: HWND mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra, NULL mov wc.cbWndExtra, NULL push hInst pop wc.hInstance mov wc.hbrBackground, COLOR_WINDOW 1 mov wc.lpszMenuName, OFFSET MenuName; Put our menu name here mov wc.lpszClassName, OFFSET ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc.hIcon, eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc invoke CreateWindowEx, NULL, aDDR ClassName, aDDR AppName, / WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, / CW_USEDEFAULT , CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, / HINST, NULL MOV HWND, EAX INVOKE ShowWindow, Hwnd, SW_SHOWNORMAL INVOKE UPDATEWINDOW, HWND .WH Ile True Invoke GetMessage, Addr MSG, NULL, 0, 0.Break .if (! EAX) Invoke DispatchMessage, Addr Msg .Endw Mov EAX, Msg.wParam Ret Winmain Endp
WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM .IF uMsg == WM_DESTROY invoke PostQuitMessage, NULL .ELSEIF uMsg == WM_COMMAND mov eax, wParam .IF ax == IDM_TEST invoke MessageBox, NULL, ADDR Test_string , OFFSET AppName, MB_OK .ELSEIF ax == IDM_HELLO invoke MessageBox, NULL, ADDR Hello_string, OFFSET AppName, MB_OK .ELSEIF ax == IDM_GOODBYE invoke MessageBox, NULL, ADDR Goodbye_string, OFFSET AppName, MB_OK .ELSE invoke DestroyWindow, hWnd .ENDIF. Else Invoke DefWindowProc, HWND, UMSG, WPARAM, LPARAM RET .Endif xor EAX, EAX RET WNDPROC ENDP End Start ************************************ *********************************************************** **************************************************** MENU.RC
*********************************************************** *********************************************************** *********************
#define idm_test 1 #define idm_hello 2 #define idm_goodbye 3 #define idm_exit 4
FirstMenu Menu {Menuitem "& Say Hello", IDM_Hello Menuitem "Say & Goodbye", IDM_GOODBYE MENUITEM SEPARATOR MENUITEM "E & XIT", IDM_EXIT} MenuItem "& Test", IDM_Test}
Analysis: let's analyze the resource file first.
#define idm_test 1 / * equal to idm_test EQU 1 * /
#define idm_hello 2
#define IDM_GODBYE 3
#define idm_exit 4
In the menu.
FirstMenu Menu
Declare Your Menu with Menu Keyword.
POPUP "& PopUp" {MENUITEM "& Say Hello", IDM_HELLO MENUITEM "Say & GoodBye", IDM_GOODBYE MENUITEM SEPARATOR MENUITEM "E & xit", IDM_EXIT} Define a popup menu with four menu items, the third one is a menu separator.
Menuitem "& Test", IDM_Test
DEFINE A MENU BAR IN THE MAINU. Next We will Examine The Source Code.
Menuname DB "firstmenu", 0; the name of our menu in the resource file.
Test_String DB "You SELECTED TEST MENU ITEM", 0
Hello_String DB "Hello, My Friend", 0
Goodbye_string db "see you again, bye", 0
MenuName is the name of the menu in the resource file. Note that you can define more than one menu in the resource file so you must specify which menu you want to use. The remaining three lines define the text strings to be displayed in message boxes That is used by the user.
IDM_TEST EQU 1; Menu IDS
IDM_HELLO EQU 2
IDM_GOODBYE EQU 3
IDM_EXIT EQU 4
DEFINE MENU IDS for USE IN THESE DEFINED IN The Resource File.
.ELSEIF uMsg == WM_COMMAND mov eax, wParam .IF ax == IDM_TEST invoke MessageBox, NULL, ADDR Test_string, OFFSET AppName, MB_OK .ELSEIF ax == IDM_HELLO invoke MessageBox, NULL, ADDR Hello_string, OFFSET AppName, MB_OK .ELSEIF ax = = IDM_GODBYE INVOKE MessageBox, Null, Addr Goodbye_String, Offset Appname, MB_OK .ELSE INVOKE DESTROYWINDOW, HWND .Endif
In the window procedure, we process WM_COMMAND messages. When the user selects a menu item, the menu ID of that menu item is sended to the window procedure in the low word of wParam along with the WM_COMMAND message. So when we store the value of wParam in eax, we compare the value in ax to the menu IDs we defined previously and act accordingly. in the first three cases, when the user selects Test, Say Hello, and Say GoodBye menu items, we just display a text string in a message box. If the user selects Exit menu item, we call DestroyWindow with the handle of our window as its parameter which will close our window. As you can see, specifying menu name in a window class is quite easy and straightforward. However you can also use an alternate method to load a menu in your window. I will not show the entire source code here. The resource file is the same in both methods. There are some minor changes in the source file which I 'll show below. .DATA?
Hinstance Hinstance?
COMMANDLINE LPSTR?
HMENU HMENU?; HANDLE OF Our MENU
Define a variable of type hmenu to store our menu handle.
invoke LoadMenu, hInst, OFFSET MenuName mov hMenu, eax INVOKE CreateWindowEx, NULL, ADDR ClassName, ADDR AppName, / WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, / CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, / hInst, NULL
Before Calling CreateWindowex, WE CALLLOGLE AND A Pointer to the name of our menu. Loadmenu Returns The Handle of Our Menu in The Resource File Which WE Pass To CreateWindowex
This Article Come from ICzelion's ASM Page, Welcom To
http://asm.yeah.net