win32编程实现点击一个按钮弹出对话框
刚学,需要一个最基本的模板,单击按钮后怎样触发单击事件弹出对话框?帮忙看看代码哪里错了,纠正并说明原因。
main.asm:
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
; Sample code for < Win32ASM Programming 2nd Edition>
; by 罗云彬, http://asm.yeah.net
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
; FirstWindow.asm
; 在窗口模板程序上添加一个按钮
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
; 使用 nmake 或下列命令进行编译和链接:
; ml /c /coff FirstWindow.asm
; Link /subsystem:windows FirstWindow.obj
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
.386
.model flat,stdcall
option casemap:none
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
; Include 文件定义
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
includewindows.inc
includegdi32.inc
includelibgdi32.lib
includeuser32.inc
includelibuser32.lib
includekernel32.inc
includelibkernel32.lib
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
; 数据段
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
.data?
szCaptiondb 'A MessageBox ! ',0
hInstancedd?
hWinMaindd?
.const
szClassNamedb 'MyClass ',0
szCaptionMaindb 'My first Window ! ',0
szTextdb 'Win32 Assembly, Simple and powerful ! ',0
szButtondb 'button ',0
szButtonTextdb '&OK ',0
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
; 代码段
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
.code
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
; 窗口过程
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
_ProcWinMainprocuses ebx edi esi,hWnd,uMsg,wParam,lParam
local@stPs:PAINTSTRUCT
local@stRect:RECT
local@hDc
moveax,uMsg
;********************************************************************
.ifeax ==WM_PAINT
invokeBeginPaint,hWnd,addr @stPs
mov@hDc,eax
invokeGetClientRect,hWnd,addr @stRect
invokeDrawText,@hDc,addr szText,-1,\
addr @stRect,\
DT_SINGLELINE or DT_CENTER or DT_VCENTER
invokeEndPaint,hWnd,addr @stPs
;********************************************************************
; 建立一个按钮
;********************************************************************
.elseifeax ==WM_CREATE
invokeCreateWindowEx,NULL,\
offset szButton,offset szButtonText,\
WS_CHILD or WS_VISIBLE,\
10,10,65,22,\
hWnd,1,hInstance,NULL
.ifeax ==IDOK
invoke MessageBox,NULL,offset szText,offset szCaption,MB_OK
.endif
;以后再用invoke mouse_event,MOUSEEVENTF_MOVE,10,10,0,0
;********************************************************************
.elseifeax ==WM_CLOSE
invokeDestroyWindow,hWinMain
invokePostQuitMessage,NULL
;********************************************************************
.else
invokeDefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
;********************************************************************
xoreax,eax
ret
_ProcWinMainendp
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
_WinMainproc
local@stWndClass:WNDCLASSEX
local@stMsg:MSG
invokeGetModuleHandle,NULL
movhInstance,eax
invokeRtlZeroMemory,addr @stWndClass,sizeof @stWndClass
;********************************************************************
; 注册窗口类
;********************************************************************
invokeLoadCursor,0,IDC_ARROW
mov@stWndClass.hCursor,eax
pushhInstance
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
invokeRegisterClassEx,addr @stWndClass
;********************************************************************
; 建立并显示窗口
;********************************************************************
invokeCreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,offset szCaptionMain,\
WS_OVERLAPPEDWINDOW,\
100,100,600,400,\
NULL,NULL,hInstance,NULL
movhWinMain,eax
invokeShowWindow,hWinMain,SW_SHOWNORMAL
invokeUpdateWindow,hWinMain
;********************************************************************
; 消息循环
;********************************************************************
.whileTRUE
invokeGetMessage,addr @stMsg,NULL,0,0
.break.if eax== 0
.if eax ==WM_USER+1
invoke MessageBox,NULL,offset szText,offset szCaption,MB_OK
.endif
invokeTranslateMessage,addr @stMsg
invokeDispatchMessage,addr @stMsg
.endw
ret
_WinMainendp
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
start:
call_WinMain
invokeExitProcess,NULL
;> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
endstart
Makefile:
NAME=main
OBJS = $(NAME).obj
LINK_FLAG = /subsystem:windows
ML_FLAG = /c /coff
$(NAME).exe: $(OBJS)
Link $(LINK_FLAG) $(OBJS)
.asm.obj:
ml $(ML_FLAG) $ <
clean:
del *.obj
[解决办法]
.data? 中的 szCaption db 'A MessageBox ! ',0 应该放到 .const 段中吧.
点击 OK 按钮出现一个 MessageBox() 对话框? 这应该在 WM_COMMAND 中检测 id 为 IDOK (1) 的触发事件, 而不是在 WM_CREATE 中. 即:
....
.elseif eax == WM_CREATE
invoke CreateWindowEx,NULL,\
offset szButton,offset szButtonText,\
WS_CHILD or WS_VISIBLE,\
10,10,65,22,\
hWnd,1,hInstance,NULL
;********************************************************************
.elseif eax == WM_COMMAND
mov eax, wParam
.if ax == 1
invoke MessageBox,NULL,offset szText,offset szCaption,MB_OK
.endif
.elseif eax == WM_CLOSE
....