Monday, July 26, 2010

Menu


This lesson we will add our application a menu.

Theory:

WINDOWS menu can be said one of the most important element. With it, users can easily select the action command. Users just read it all the menu items you can understand about the application provides function, but also immediate action, not to read the manual. Precisely because of the menu gives the user a kind of convenient way, so you join in the application menu should comply with general standards. For example: the first two menu items are usually "File" and "Edit", the last is the "Help", you can This is inserted in the middle you want to define the menu item. If you are running the menu command will pop up a dialog box, then the menu item should omit the symbol (...). menu after adding a resource, in addition to the menu but also have Others, like the dialog box, string, icon, bitmap resources. In the linker will link resources to the executable program to the end of our implementation of the program on both machines, including the resources they command. You can any text editor to write the script file, the file you can specify the resources shown by the appearance and some other properties. of course, more intuitive method is to use the resource editor, resource editor are usually packaged in compiling environment, such as Visual C + +, Borland C + + and so on with the resource editor. we can define as follows a menu of resources:

MyMenu MENU
(
[Menu list here]
)
This and the C language is very similar to the definition of structure. MyMenu similar variable 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, this and the same style PASCAL language.
List of items in the menu is long list of MENU99vEM and POPUP statement. MENU99vEM defines a menu item, when selected, will not activate the dialog box. Its syntax is as follows:
MENU99vEM "& text", ID [, options]
It begins by the keyword MENU99vEM, followed by the MENU99vEM menu item after the name refers to the string, the symbol "&" character after the first draw will be taken off line, it is the menu item shortcuts. ID's role when the menu is selected, WINDOWS message processing used to distinguish between menu items. There is no doubt, ID number must be unique. options have the following properties available:
GRAYED on behalf of the menu item in the non-active state, that is when it is selected will not generate WM_COMMAND messages. The menu displayed in gray.
INACTIVE The menu item is on behalf of non-activated state, that is when it is selected will not generate WM_COMMAND messages. The menu to the normal colors.
MENUBREAK the menu item and the following menu items will be displayed in a new column. (Translator's Note: more difficult to describe, please do experiment.)
HELP in the menu item and then right-aligned menu items. (Translator's Note: I compiled WINDOWS2000 menu items have the logo, the sign did not seem to work)
You can use these flags alone, or together, can put them. INACTIVE and GRAYED course can not be used. POPUP the syntax is as follows:
POPUP "& text" [, options]
(
[Menu list]
)
POPUP defines a menu item when the menu item is selected a sub-menu will pop up. In addition there is a special type of MENU99vEM statement MENU99vEM SEPARATOR, it said the location of the menu item draw a dividing line. End the definition of the menu, you can use the script in the program resources in the definition of the menu. You can program the two places (or called in two ways) to use them:
Structure in members of lpszMenuName WNDCLASSEX in. For example, you have a menu "FirstMenu", you can press the following ways to contact it to your window:
. DATA
MenuName db "FirstMenu", 0
...........................
...........................
. CODE
...........................
mov wc.lpszMenuName, OFFSET MenuName
...........................
Menu specified in the CreateWindowEx function 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 the difference between the two with it? When you use the first method, because the class specified in the window, so all classes derived by the window, the window will have the same menu. If you want to derive from the same class have different menu window to use it in the second method, the method specified by the menu CreateWindowEx function will "cover" WNDCLASSEX specified in the menu structure. Next we look at when the user selects a menu item, how it is to inform WINDOWS windows process: When the user selects a menu item, WINDOWS window procedure will receive a WM_COMMAND message, pass the parameters come in the bottom byte of wParam contains a menu item ID. Well, that is on the menu item above all, here we come to practice.
Examples:
The first example shows a menu item specifies the first method:
.386
. Model flat, stdcall
option casemap: none

WinMain proto: DWORD,: DWORD,: DWORD,: DWORD

include masm32includewindows.inc
include masm32includeuser32.inc
include masm32includekernel32.inc
includelib masm32libuser32.lib
includelib masm32libkernel32.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_EX99v 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
backup bin bin_old conf config crawler.tar.gz crawler_bin.tar.gz data eshow eshow_sitemap.html generate.sh google.html google.html.md5 log maint news: 10 news: 11 news: 12 news: 13 news: 14 news: 15 news: 16 news: 17 news: 18 news: 2 news: 3 news: 4 news: 5 news: 6 news: 7 news: 8 news: 9 outboundLinksMgr.sql seeds sitemap.html svn tasks tmp xml2dict-2008.6-tar. gz xml2dict-read-only

Menu.rc
backup bin bin_old conf config crawler.tar.gz crawler_bin.tar.gz data eshow eshow_sitemap.html generate.sh google.html google.html.md5 log maint news: 10 news: 11 news: 12 news: 13 news: 14 news: 15 news: 16 news: 17 news: 18 news: 2 news: 3 news: 4 news: 5 news: 6 news: 7 news: 8 news: 9 outboundLinksMgr.sql seeds sitemap.html svn tasks tmp xml2dict-2008.6-tar. gz xml2dict-read-only
# Define IDM_TEST 1
# Define IDM_HELLO 2
# Define IDM_GOODBYE 3
# Define IDM_EX99v 4

FirstMenu MENU
(
POPUP "& PopUp"
(
MENU99vEM "& Say Hello", IDM_HELLO
MENU99vEM "Say & GoodBye", IDM_GOODBYE
MENU99vEM SEPARATOR
MENU99vEM "E & xit", IDM_EX99v
)
MENU99vEM "& Test", IDM_TEST
)


Analysis:
Let's analyze the resource file:

# Define IDM_TEST 1 / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var / vmware equal to IDM_TEST equ 1 * /
# Define IDM_HELLO 2
# Define IDM_GOODBYE 3
# Define IDM_EX99v 4

The above lines define the menu item ID. As long as attention to the menu item ID number must be unique, you can ID number to any value.
FirstMenu MENU

The definition of the menu with MENU keyword.

POPUP "& PopUp"
(
MENU99vEM "& Say Hello", IDM_HELLO
MENU99vEM "Say & GoodBye", IDM_GOODBYE
MENU99vEM SEPARATOR
MENU99vEM "E & xit", IDM_EX99v
)

There are four menu items to define a sub-menu, in which the third menu item is a separator.

MENU99vEM "& Test", IDM_TEST

A definition of the 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 as many menus, so before use must be specified that you want to use one, the next line is displayed in the selected menu item strings in the relevant dialog box.

IDM_TEST equ 1; Menu IDs
IDM_HELLO equ 2
IDM_GOODBYE equ 3
IDM_EX99v equ 4

WINDOWS define the window used 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 process, we deal with WM_COMMAND window message. When the user selects a menu item, the menu item's ID into the parameters were also sent to the WINDOWS wParam window procedure, we save it to the eax register in order to pre-defined menu items and compare with ID. 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, we will call the function DestroyWindow, where the parameters are We handle of the window, thus destroying the window. As you can see, through a window class name is specified in the menu to an application method to generate a menu is simple and intuitive. Outside in this method you can use another method, which is the same resource file, source file, only a few changes, these changes are as follows:


. Data?
hInstance HINSTANCE?
CommandLine LPSTR?
hMenu HMENU?; handle of our menu

Define a variable to hold the handle of our menu, 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

Call LoadMenu function, which requires an instance handle and the menu name string, calls the results back to point to the menu handle, and then passed to the function just returns the menu handle CreateWindowEx on it.






Recommended links:



Ophone To Support The TD-based Chips Is Still The Bottleneck



Unicom frankly difficult to govern without legal constraint SP is critical violations



converting avi to Wmv



MTS to vob



Dealer how to identify the manufacturer with the money trap?



dvd audio ripper



Picked Games And Entertainment



Rising gods: a small pinch frame, more than busy for!



DreamWeaver, classic 50-Q (9)



If Tomorrow Is The Last Night Of IPv4



Mkv To Vob



Review Dictionaries EDUCATION



Photoshop retouching images (8) to adjust brightness



No comments:

Post a Comment