Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 8. Menu

WIN32 compilation: 8. Menu

Print View , by: iSee ,Total views: 11 ,Word Count: 2662 ,Date: Sun, 10 May 2009 Time: 9:56 AM

The eighth class menu


This lesson we will be in our application to add a menu.

Theory:

WINDOWS menu can be said to be one of the most important element. With it, users can easily choose the command to operate. Users will read it as long as all the menu items can be provided to understand the application of some functions, and can be operated immediately, there is no need to read the manual. It is precisely because of the menu to the user a kinds of convenient ways, so you add in the application menu should be in general compliance with the standards. For example: the general is the first two menu items "File" and "Edit", and finally "Help", you can insert in the middle you want the definition of the menu item. If the menu command to run a dialog box will pop up, then they would have in the menu item after menu (...). omitted at a resource, in addition to other things like menu dialog box, string, icon and bitmap resources. in the linker will link the resources into the executable program, and the final implementation of our program on both machines, including the resources and instructions. You can at any a text editor to prepare a script file, in the document you can specify the resources to show the appearance and other properties. Of course, a more intuitive way is to use the resources editor, resources editor usually are packaged in the compiler environment, like Visual c + + , Borland C++ + + and so on with a resource editor. we can define as follows a menu of resources:
    MyMenu MENU
    (
    [menu list here]
    )
This is the structure of C language is very similar to the definition of the body. MyMenu similar variables is defined, and is similar to the keyword MENU. Of course you can use another approach, that is, to use BEGIN and END instead of curly braces, which PASCAL language and the same style.
Menu item in the list is a long list of MENUITEM and POPUP statement. MENUITEM defines a menu item, when the choice will not activate the dialog box. Its syntax is as follows:
    MENUITEM "& text", ID [, options]
MENUITEM by keyword at the beginning, followed in the post-MENUITEM menu item refers to the name of the string, symbol "&" character after the first draw will be taken off line, it is also the shortcut menu. The role of ID when the menu is selected when, WINDOWS news used to distinguish between the process used for menu items. There is no doubt, ID number must be unique. options to choose from the following attributes:
  • GRAYED on behalf of the menu item in the non-activated state, that is, when it would not have been selected WM_COMMAND message. The menu display in gray.
  • INACTIVE on behalf of the menu item in the non-activated state, that is, when it would not have been selected WM_COMMAND message. The menu shows a normal color.
  • MENUBREAK the menu item and then the menu item will appear in the new column. (Translator's Note: more difficult to describe, please do the experiment.)
  • HELP in the menu item and then right-align the menu items. (Translator's Note: I have compiled WINDOWS2000 the signs under the menu item, the signs did not seem to work)
You can use the above flag, you can put them or together. INACTIVE and GRAYED of course, can not be used. POPUP the syntax is as follows:
POPUP "& text" [, options]
(
[menu list]
)
POPUP the definition of a menu item when the menu item is selected when a sub-menu will pop up. In addition, a special type of MENUITEM statement MENUITEM SEPARATOR, its location in the menu item draw a dividing line. End the definition of menu, you can use the script in the procedure defined in the resources menu. You can be in two places in the process (or called in two ways) to use them:
  • WNDCLASSEX structure in members of lpszMenuName. For example, you have a menu "FirstMenu", you can click the following ways to link it to your window:
        . DATA
          MenuName db "FirstMenu", 0
        ...........................
        ...........................
        . CODE
          ...........................
          mov wc.lpszMenuName, OFFSET MenuName
          ...........................
  • CreateWindowEx function specified in the menu handle:
        . 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 \
          ...........................
You may ask, what is between the two different? When you use the first method, as is specified in the window class, so all of the window class derived from the window will have the same menu. If you'd like from the same window class derived from a different menu to use it in the second method, the method specified by the CreateWindowEx function menu will be "covered" WNDCLASSEX structure specified in the menu. Next, we look at when the user selects a menu item when it is a window to inform the process of WINDOWS: When the user selects a menu item, WINDOWS window process will receive a WM_COMMAND message, wParam parameters Chuan came at the end of the byte menu item contains the ID. Well, that is on the menu item above all, we have to practice the following.

Examples:

The first example shows the designation of a menu item of the first method:

.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
includelib \ 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
. WHILE 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
(
POPUP "& PopUp"
(
MENUITEM "& Say Hello", IDM_HELLO
MENUITEM "Say & GoodBye", IDM_GOODBYE
MENUITEM SEPARATOR
MENUITEM "E & xit", IDM_EXIT
)
MENUITEM "& Test", IDM_TEST
)

Analysis:

Us to analyze the resource file:
# define IDM_TEST 1 / * equal to IDM_TEST equ 1 * /
# define IDM_HELLO 2
# define IDM_GOODBYE 3
# define IDM_EXIT 4
Above a few lines of the definition of a menu item ID. As long as the attention of the menu item ID No. must be unique, you can be of any value to the ID number.

FirstMenu MENU

The definition of the menu with MENU keyword.

POPUP "& PopUp"
(
MENUITEM "& Say Hello", IDM_HELLO
MENUITEM "Say & GoodBye", IDM_GOODBYE
MENUITEM SEPARATOR
MENUITEM "E & xit", IDM_EXIT
)

The definition of a menu item there are four sub-menu, the third menu item is a dividing line.

MENUITEM "& Test", IDM_TEST

The definition of a main menu. Let us look at 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 resource file is specified in the name of the menu. Because the script file you can define any number of menu, it must be specified prior to the use of which you want to use the next line in the selected menu item is displayed in the relevant dialog box when the string.
IDM_TEST equ 1; Menu IDs
IDM_HELLO equ 2
IDM_GOODBYE equ 3
IDM_EXIT equ 4
WINDOWS define a window in the course of the menu item ID. These values must be the same script 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_GOODBYE
invoke MessageBox, NULL, ADDR Goodbye_string, OFFSET AppName, MB_OK
. ELSE
invoke DestroyWindow, hWnd
. ENDIF

In this window we are dealing with the process WM_COMMAND messages. When the user selects a menu item, the menu item's ID Add wParam parameter WINDOWS been sent to the window at the same time the process, we save it to eax register in order and pre-defined ID of the menu item comparison. The first three cases, when we select the Test, Say Hello, Say GoodBye menu items, it will pop up a dialog box which displays a related string, select the Exit menu item, then we call the function DestroyWindow, in which the parameters handle of our window, so that the destruction of the window. Just as you can see, through a window class name is specified in the menu approach to an application to generate a menu is simple and intuitive. In addition to this method you can use another method, which is the same resource file, source files are also only a few changes, these changes are as follows:

. data?
hInstance HINSTANCE?
CommandLine LPSTR?
hMenu HMENU?; handle of our menu
Defines a variable to hold the handle of our menu, and then:

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

LoadMenu call function, which need to handle instances of the string and the menu, call the outcome of the handler to return to the menu, and then to the CreateWindowEx function to return to the menu just handle it.



Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.