右键弹出菜单是很常用的功能,其实现也不能,各其相关的主要是:
一个数据结构:POINT
两个函数:GetCursorPos()&TrackPopupMenu()
这个例子中在系统菜单的最下边增加了一个菜单项,点击弹出消息框.
下面是代码(完整工程下载)
.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 _CreateMenu PROTO .data? hInstance dd ? hWinMain dd ? .const szClassName db 'MyClass',0 szCaption db 'My Window',0 .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 ;-------------------------- 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 ;-------------------------- .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 LOCAL @stPos:POINT LOCAL @hMenu mov eax,uMsg .if eax==WM_CREATE .elseif eax==WM_RBUTTONDOWN invoke _CreateMenu mov @hMenu,eax invoke GetCursorPos,addr @stPos invoke TrackPopupMenu,@hMenu,TPM_LEFTALIGN,@stPos.x,@stPos.y,NULL,hWnd,NULL .elseif eax==WM_CLOSE invoke DestroyWindow,hWinMain invoke PostQuitMessage,NULL .elseif invoke DefWindowProc,hWnd,uMsg,wParam,lParam ret .endif xor eax,eax ret _ProcWinMain endp ;==================================================== _CreateMenu proc LOCAL @hPopMenu1 LOCAL @hPopMenu2 invoke CreatePopupMenu mov @hPopMenu1,eax invoke CreatePopupMenu mov @hPopMenu2,eax invoke AppendMenu,@hPopMenu1,MF_STRING,1000,CTEXT("A") invoke AppendMenu,@hPopMenu1,MF_POPUP,@hPopMenu2,CTEXT("B") invoke AppendMenu,@hPopMenu1,MF_STRING,1001,CTEXT("C") invoke AppendMenu,@hPopMenu2,MF_STRING,1002,CTEXT("D") invoke AppendMenu,@hPopMenu2,MF_STRING,1003,CTEXT("E") push @hPopMenu1 pop eax ret _CreateMenu endp ;===================================================== start: call _WinMain invoke ExitProcess,NULL end start