基本上的窗口都会有一个菜单,现在就来看看Win32汇编中是如何加载菜单的:
1>在工程中添加新的菜单资源
2>双击新添加的菜单资源进行编辑
3>菜单栏:Make->Compile RC来编译资源文件
4>导出资源中的ID号并写到数据段的.const中
5>下面是完整的源代码供参考:(工程下载地址)
.386 .model flat,stdcall option casemap:none include windows.inc include kernel32.inc include user32.inc include debug.inc includelib kernel32.lib includelib user32.lib includelib debug.lib .data? hInstance dd ? hWinMain dd ? hMenu dd ? .const szClassName db 'MyClass',0 szCaption db 'My Window',0 ;---------------------------------------- IDR_MENU equ 10000 IDM_FILE equ 10001 IDM_OPEN equ 10003 IDM_EXIT equ 10002 IDM_EDIT equ 10004 IDM_COPY equ 10005 .code _WinMain proc LOCAL @stWndClass:WNDCLASSEX LOCAL @stMsg:MSG ;-------------------------- invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass ;-------------------------- invoke GetModuleHandle,NULL mov hInstance,eax ;-------------------------- push hInstance pop @stWndClass.hInstance mov @stWndClass.cbSize,sizeof WNDCLASSEX mov @stWndClass.style,CS_HREDRAW OR CS_VREDRAW mov @stWndClass.lpfnWndProc,offset _ProcWinMain mov @stWndClass.hbrBackground,COLOR_WINDOW+1 mov @stWndClass.lpszClassName,offset szClassName invoke LoadCursor,0,IDC_ARROW mov @stWndClass.hCursor,eax ;-------------[Regist Window]------------- invoke RegisterClassEx,addr @stWndClass ;------------[Load Menu]------------------ invoke LoadMenu,hInstance,IDR_MENU mov hMenu,eax ;-------------[Create Window]------------- invoke CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,\ offset szCaption,WS_OVERLAPPEDWINDOW,100,100,600,400,\ NULL,hMenu,hInstance,NULL mov hWinMain,eax ;-------------------------- invoke ShowWindow,hWinMain,SW_SHOWNORMAL ;-------------------------- invoke UpdateWindow,hWinMain ;-------------------------- .while TRUE invoke GetMessage,addr @stMsg,NULL,0,0 .break .if eax==0 invoke TranslateMessage,addr @stMsg invoke DispatchMessage,addr @stMsg .endw ret _WinMain endp ;==================================================== _ProcWinMain proc uses ebx esi edi hWnd,uMsg,wParam,lParam mov eax,uMsg .if eax==WM_CREATE .elseif eax==WM_CLOSE invoke DestroyWindow,hWinMain invoke PostQuitMessage,NULL .elseif eax==WM_COMMAND mov eax,wParam .if ax==IDM_EXIT invoke DestroyWindow,hWinMain invoke PostQuitMessage,NULL .endif .elseif invoke DefWindowProc,hWnd,uMsg,wParam,lParam ret .endif xor eax,eax ret _ProcWinMain endp ;==================================================== start: call _WinMain invoke ExitProcess,NULL end start