menu.asm

来自「windows下汇编语言 学习汇编语言好助手」· 汇编 代码 · 共 248 行

ASM
248
字号
;******************************
;文件:Menu.asm               *
;功能:演示使用菜单、弹出式菜 *
;	   单和动态生成菜单       *
;******************************

.386p
.model flat,stdcall
include win32.inc
extrn GetMessageA:proc
extrn TranslateMessage:proc
extrn DispatchMessageA:proc
extrn ShowWindow:proc
extrn UpdateWindow:proc
extrn CreateWindowExA:proc
extrn RegisterClassA:proc
extrn DefWindowProcA:proc
extrn MessageBoxA:proc
extrn ExitProcess:proc
extrn LoadIconA:proc
extrn LoadCursorA:proc
extrn GetStockObject:proc
extrn GetModuleHandleA:proc
extrn PostQuitMessage:proc
extrn CreatePopupMenu:proc
extrn InsertMenuItemA:proc
extrn GetCursorPos:proc
extrn TrackPopupMenu:proc
extrn GetMenu:proc
extrn GetSubMenu:proc
extrn LoadMenuA:proc
extrn SetMenuItemInfoA:proc
extrn AppendMenuA:proc
extrn DestroyMenu:proc
extrn _wsprintfA:proc

FALSE = 0
MB_OK = 0
NULL  = 0
IDM_HELLO			 = 200H
IDM_ABOUT			 = 201H
IDM_TOP				 = 202H
IDM_POPUP			 = 203H
IDM_EXIT			 = 204H
IDM_DYNAMIC			 = 205H
MF_STRING           = 00000000H
MF_SEPARATOR        = 00000800H
MFT_STRING          = MF_STRING
MFT_SEPARATOR       = MF_SEPARATOR
TPM_LEFTALIGN   	= 0000H

POINT struc
	x dd ?
	y dd ?
	ends
MSG struc
    hwnd 	dd ?
    message	dd ?
    wParam	dd ?
    lParam	dd ?
    time	dd ?
    pt		POINT<>
    ends

WNDCLASS struc
	style		dd ?
	lpfnWndProc	dd ?
	cbClsExtra	dd ?
	cbWndExtra	dd ?
	hInstance	dd ?
	hIcon		dd ?
	hCursor		dd ?
	hbrBackground	dd ?
	lpszMenuName	dd ?
	lpszClassName	dd ?
	ends


MENUITEMINFO struc
	cbSize	dd ?
	fMask	dd ?
	fType	dd ?
	fState	dd ?
	wID		dd ?
	hSubMenu	dd ?
	hbmpChecked	dd ?
	hbmpUnchecked	dd ?
	dwItemData	dd ?
	dwTypeData	dd ?
	cch			dd ?
	ends 

.data 
	printMsgTitle  db 'title',0
	printMsgTmp    db 'HWND:%X MSG:%X WPARAM:%X LPARAM:%X',0
	printMsgText   db 200 dup (0)

	MenuName	db 'MyFirstMenu',0
	MenuName1	db 'MySecondMenu',0
	myclassname	db 'MyClass',0
	caption		db '菜单使用例子',0
	hello		db 'Hello',0
	about		db '菜单例子 Ver 1.0',13,' 蓝琚成,2000/8',0
	top			db '顶层',0
	popup		db '弹出菜单例子',0
	exit		db '退出',0
	dynamic		db '动态生成菜单',0
	align 4
	hInst	dd ?
	hWnd	dd ?
	hmenu	dd ?
	hmenu1	dd ?
	hmenu0  dd ?
	msg			MSG<>
	wc			WNDCLASS<>
	point		POINT<>
	menuinfo	MENUITEMINFO<>

.code
main:
	call	GetModuleHandleA,0	;取模块句柄
	mov		[hInst],eax

	mov		wc.lpszClassName,offset myclassname
	mov		eax,[hInst]
	mov		wc.hInstance,eax
	mov		wc.lpfnWndProc,offset WinMsgProc
	call	LoadIconA,0,IDI_APPLICATION
	mov		wc.hIcon,eax
	call	LoadCursorA,0,IDC_IBEAM
	mov		wc.hCursor,eax
	mov		wc.lpszMenuName,offset MenuName	;菜单名
	call	GetStockObject,WHITE_BRUSH
	mov		wc.hbrBackground,eax
	mov		wc.style,CS_HREDRAW or CS_VREDRAW or CS_GLOBALCLASS
	mov		wc.cbClsExtra,0
	mov		wc.cbWndExtra,0

	call	RegisterClassA,offset wc	;注册窗口类
	;建立主窗口
	call	CreateWindowExA,0,offset myclassname,offset caption,WS_OVERLAPPEDWINDOW,100,100,450,300,0,0,[hInst],0
	mov		[hWnd],eax

	call	ShowWindow,[hWnd],1
	call	UpdateWindow,[hWnd]
MsgLoop:	;消息循环
	call	GetMessageA,offset msg,0,0,0
	or		eax,eax
	jz		Exit
	call	TranslateMessage,offset msg
	call	DispatchMessageA,offset msg
	jmp		MsgLoop
Exit:
	call	ExitProcess,0
;**************************************************************************
WinMsgProc  proc uses ebx edi esi, handle:DWORD, wMsg:DWORD, wParam1:DWORD, lParam1:DWORD
	cmp		wMsg,WM_CREATE
	jz		create
	cmp		wMsg,WM_DESTROY
	jz		destory
	cmp		wMsg,WM_LBUTTONDOWN
	jz		lbuttondown
	cmp		wMsg,WM_RBUTTONUP
	jz		rbuttonup
	cmp		wMsg,WM_COMMAND
	jz		command
	jmp		default
command:	;处理菜单消息
	mov		ebx,wParam1
	cmp		bx,IDM_HELLO
	jz		menu_hello
	cmp		bx,IDM_ABOUT
	jz		menu_about
	cmp		bx,IDM_TOP
	jz		menu_top
	cmp		bx,IDM_POPUP
	jz		menu_popup
	cmp		bx,IDM_EXIT
	jz		menu_exit
	cmp		bx,IDM_DYNAMIC
	jz		menu_dynamic
	call	printMsg,handle,wMsg,wParam1,lParam1
	jmp		default
menu_hello:
	call	MessageBoxA,handle,offset hello,offset caption,MB_OK
	xor		eax,eax
	ret
menu_about:
	call	MessageBoxA,handle,offset about,offset caption,MB_OK
	xor		eax,eax
	ret
menu_top:
	call	MessageBoxA,handle,offset top,offset caption,MB_OK
	xor		eax,eax
	ret
menu_popup:
	call	MessageBoxA,handle,offset popup,offset caption,MB_OK
	xor		eax,eax
	ret
menu_exit:
	jmp		destory
menu_dynamic:
	call	MessageBoxA,handle,offset dynamic,offset caption,MB_OK
	xor		eax,eax
	ret
create:		;动态建立弹出菜单
	call	CreatePopupMenu
	mov		hmenu,eax
	call	AppendMenuA,hmenu,MF_STRING,IDM_DYNAMIC,offset dynamic
	call	AppendMenuA,hmenu,MFT_SEPARATOR,47H,NULL
	call	AppendMenuA,hmenu,MF_STRING,IDM_EXIT,offset exit

			;取菜单
	call	LoadMenuA,hInst,offset MenuName1
	mov		hmenu0,eax
	call	GetSubMenu,eax,0
	mov		hmenu1,eax
	xor		eax,eax
	ret
destory:	;删除菜单资源
	call	DestroyMenu,hmenu
	call	DestroyMenu,hmenu0
	call	PostQuitMessage,0
	xor		eax,eax
	ret
rbuttonup:	;弹出程序初始化时动态生成的菜单
	call	GetCursorPos, offset point
	call	TrackPopupMenu,hmenu,TPM_LEFTALIGN,point.x,point.y,0,handle,0
	xor		eax,eax
	ret
lbuttondown:;弹出程序初始化时取出的菜单
	call	GetCursorPos, offset point
	call	TrackPopupMenu,hmenu1,TPM_LEFTALIGN,point.x,point.y,0,handle,0	
	xor		eax,eax
	ret
default:
	call	DefWindowProcA,handle,wMsg,wParam1,lParam1
	ret
WinMsgProc  endp

printMsg proc aaaa:DWORD,bbbb:DWORD,cccc:DWORD,dddd:DWORD
	call	_wsprintfA,offset printMsgText,offset printMsgTmp,aaaa,bbbb,cccc,dddd
	add		esp,4*6
	call	MessageBoxA,NULL,offset printMsgText,offset printMsgTitle,MB_OK
	ret
printMsg endp
	end main

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?