加速键资源在编辑中也时常用到,比如Ctrl+A,Ctrl+Z等等,现在就介绍一下在Win32汇编中如何使用加速键资源,
本例中按"Ctrl+O"会弹出一个消息框,按"Ctrl+E"则会在弹出一个消息框之后关闭窗口:
1>新键一个工程,创建一个新的窗口.
2>在资源中添加'加速键'资源
首先,用ResEdit(下载:32位,64位)打开工程中的资源文件,然后添加新的加速键资源:
然后,在.const中写上资源的ID号:
接着,对原来的消息循环进行修改,在TranslateMessage和DispatchMessage之前TranslateMeeage:
最后,在WM_COMMAND中对消息作出具体的响应:
下面给出全部源代码(完整工程下载)
.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 ? .const szClassName db 'MyClass',0 szCaption db 'My Window',0 ;------------------------------------------- IDR_ACCELERATOR equ 101 ID_ACC1 equ 40000 ID_ACC2 equ 40001 .code _WinMain proc LOCAL @stWndClass:WNDCLASSEX LOCAL @stMsg:MSG LOCAL @hAcc ;-------------------------- 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 ;-------------------------- invoke RegisterClassEx,addr @stWndClass ;-------------------------- invoke CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,\ offset szCaption,WS_OVERLAPPEDWINDOW,100,100,600,400,\ NULL,NULL,hInstance,NULL mov hWinMain,eax ;-------------------------- invoke ShowWindow,hWinMain,SW_SHOWNORMAL ;-------------------------- invoke UpdateWindow,hWinMain ;--------------------[Load accelerator]------------ invoke LoadAccelerators,hInstance,IDR_ACCELERATOR mov @hAcc,eax .while TRUE invoke GetMessage,addr @stMsg,NULL,0,0 .break .if eax==0 ;----------------[handle accelerators' message]--------- invoke TranslateAccelerator,hWinMain,@hAcc,addr @stMsg .if eax==0 ;if "TranslateAccelerator" doesn't handle the message invoke TranslateMessage,addr @stMsg invoke DispatchMessage,addr @stMsg .endif .endw ret _WinMain endp ;==================================================== _ProcWinMain proc uses ebx esi edi hWnd,uMsg,wParam,lParam mov eax,uMsg .if eax==WM_CLOSE invoke DestroyWindow,hWinMain invoke PostQuitMessage,NULL .elseif eax==WM_COMMAND mov eax,wParam .if ax==ID_ACC1 invoke MessageBox,NULL,CTEXT("Ctrl+O Pressed"),CTEXT("INFO"),MB_OK .elseif ax==ID_ACC2 invoke MessageBox,NULL,CTEXT("Ctrl+E Pressed, Program will exit now"),CTEXT("INFO"),MB_OK 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