menuitem "say &goodbye", idm_goodbye
menuitem separator
menuitem "e&xit",idm_exit
}
menuitem "&test", idm_test
}
分析:
我们先来分析资源文件:- #define idm_test 1 /* equal to idm_test equ 1*/
#define idm_hello 2
#define idm_goodbye 3
#define idm_exit 4
firstmenu menu
用关键字menu定义菜单。
popup "&popup"
{
menuitem "&say hello",idm_hello
menuitem "say &goodbye", idm_goodbye
menuitem separator
menuitem "e&xit",idm_exit
}
定义一个有四个菜单项的子菜单,其中第三个菜单项是一个分隔线。
menuitem "&test", idm_test
定义主菜单中的一项。下面我们来看看源代码。
- 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
- idm_test equ 1 ; menu ids
idm_hello equ 2
idm_goodbye equ 3
idm_exit equ 4
.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
在本窗口过程中我们处理wm_command消息。当用户选择了一个菜单项时,该菜单项的id放入参数wparam中被同时送到windows的窗口过程,我们把它保存到eax寄存器中以便和预定义的菜单项id比较用。前三种情况下,当我们选中test、say hello、say goodbye菜单项时,会弹出一个对话框其中显示一个相关的字符串,选择exit菜单项时,我们就调用函数destroywindow,其中的参数是我们窗口的句柄,这样就销毁了窗口。就像您所看到的,通过在一个窗口类中指定菜单名的方法来给一个应用程序生成一个菜单是简单而直观的。除此方法外您还可以用另一种方法,其中资源文件是一样的,源文件中也只有少数的改动,这些改动如下:
- .data?
hinstance hinstance ?
commandline lpstr ?
hmenu hmenu ? ; handle of our menu
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,\