⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.asm

📁 一个菜单使用的例子
💻 ASM
字号:
;-----------------------------------------
;   Menu.asm -- Menu Demonstration
;   based on Charles Petzold's MenuDemo.C
;
;   Translated into assembly 9/5/99 by
;   Ron Thomas Ron_Thom@Compuserve.com	
;-----------------------------------------

.386				; 32 bit when .386 appears before .MODEL
.MODEL FLAT,STDCALL

include windows.inc
include resource.inc

include user32.inc
include kernel32.inc
include gdi32.inc

includelib user32.lib
includelib kernel32.lib
includelib gdi32.lib

WinMain		PROTO :DWORD, :DWORD, :DWORD, :SDWORD

.data
CR	EQU	0Dh
ID_TIMER	EQU 1
MenuName	db 'MENUDEMO',0		; Name of menu in resource file

ClassName db "SimpleWinClass",0
AppName   db "Menu Demonstration",0

HLPtxt		db	'Help not implemented',0
ABTtxt		db	'Menu Demonstration Program',CR
		db	'     by Ron Thomas',0

idColour	DD	WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH, DKGRAY_BRUSH, BLACK_BRUSH  
iSelection	DD	IDM_BKGND_WHITE
szAppName	db	'Menu Demo', 0

.data?

hInstance HINSTANCE ?
CommandLine LPSTR ?

LOWORD	MACRO 	bigword	;; Retrieves the low word from double word argument

	mov	eax,bigword
	and	eax,0FFFFh	;; Set to low word 
	ENDM

HIWORD	MACRO   bigword	;; Retrieves the high word from double word argument

	mov	ebx,bigword
	shr	ebx,16		;; Shift 16 for high word to set to high word
				
	ENDM
;----------------------------------------------------------------------------
.code
start:
	invoke GetModuleHandle, NULL
	mov    hInstance,eax
	invoke GetCommandLine
        invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
	invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:SDWORD
	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  hInstance
        pop   wc.hInstance
	mov   wc.hbrBackground,COLOR_WINDOW+1
	mov   wc.lpszMenuName,offset MenuName
	mov   wc.lpszClassName,OFFSET ClassName
        invoke LoadIcon,NULL,IDI_APPLICATION
	mov   wc.hIcon,eax
        mov   wc.hIconSm,0
        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 TranslateMessage, ADDR msg
                INVOKE DispatchMessage, ADDR msg
        .ENDW
        mov     eax,msg.wParam
        ret
WinMain endp

WndProc proc uses ebx, hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

	LOCAL hMenu:HMENU
  
	mov   eax,uMsg

	.IF eax==WM_COMMAND

	  invoke  GetMenu, hWnd
	  mov	hMenu,eax

	  LOWORD  wParam
	  
	  .IF eax==IDM_FILE_NEW || eax==IDM_FILE_OPEN || eax==IDM_FILE_SAVE || eax==IDM_FILE_SAVE_AS

	    invoke MessageBeep, 0

	  .ELSEIF eax==IDM_APP_EXIT

	    invoke SendMessage, hWnd, WM_CLOSE, 0, 0
	 
	  .ELSEIF  eax==IDM_EDIT_UNDO || eax==IDM_EDIT_CUT   || \
		   eax==IDM_EDIT_COPY || eax==IDM_EDIT_PASTE || eax==IDM_EDIT_CLEAR

	    invoke MessageBeep, 0

;	    Check if a one of the 5 background brushes is requested
;	    N.B the following logic assumes IDM_WHITE through IDM_BLACK are consecutive numbers
			    
	  .ELSEIF  eax==IDM_BKGND_WHITE || eax==IDM_BKGND_LTGRAY || \
		   eax==IDM_BKGND_GRAY  || eax==IDM_BKGND_DKGRAY || eax==IDM_BKGND_BLACK

	    invoke CheckMenuItem, hMenu, iSelection, MF_UNCHECKED
	    LOWORD wParam
	    mov	   iSelection, eax
	    push   eax			; save copy	
	    invoke CheckMenuItem, hMenu, iSelection, MF_CHECKED
	    pop	   ebx
	    sub	   ebx, IDM_BKGND_WHITE
	    shl	   ebx,2		; form dword index
	    invoke GetStockObject, idColour[ebx]
	    invoke SetClassLong, hWnd, GCL_HBRBACKGROUND, eax
	    invoke InvalidateRect, hWnd, NULL, TRUE

	  .ELSEIF eax==IDM_TIMER_START

	    invoke  SetTimer, hWnd, ID_TIMER, 1000, NULL
	    .IF eax

	      invoke EnableMenuItem, hMenu, IDM_TIMER_START, MF_GRAYED
	      invoke EnableMenuItem, hMenu, IDM_TIMER_STOP,  MF_ENABLED

	    .ENDIF

	  .ELSEIF eax==IDM_TIMER_STOP

	      invoke KillTimer, hWnd, ID_TIMER
	      invoke EnableMenuItem, hMenu, IDM_TIMER_START, MF_ENABLED
              invoke EnableMenuItem, hMenu, IDM_TIMER_STOP,  MF_GRAYED
 
	  .ELSEIF eax==IDM_APP_HELP

	    invoke MessageBox, hWnd, ADDR HLPtxt, ADDR szAppName, MB_ICONEXCLAMATION or MB_OK

	  .ELSEIF eax==IDM_APP_ABOUT

	    invoke MessageBox, hWnd, ADDR ABTtxt, ADDR szAppName, MB_ICONINFORMATION or MB_OK

	  .ENDIF


	.ELSEIF eax==WM_TIMER

	  invoke MessageBeep, 0			; Just makes a beep

        .ELSEIF eax==WM_DESTROY

	      invoke PostQuitMessage,NULL 
	.ELSE
              invoke DefWindowProc,hWnd,uMsg,wParam,lParam
              ret
	.ENDIF
        xor    eax,eax
	ret

WndProc endp

        end start

⌨️ 快捷键说明

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