读书人

win32汇编小疑点

发布时间: 2012-03-08 13:30:13 作者: rapoo

win32汇编小问题
小弟新手,建立了一个按钮,想点击那个按钮就退出程序,可是怎么也做不成,
下面是代码,运行时闪了一下,自己就关闭,不知道为什么,
前辈帮我看看,谢谢啦。



.386
.model flat,stdcall
option casemap:none
included:\masm32\INCLUDE\windows.inc
included:\masm32\INCLUDE\gdi32.inc
includelibd:\masm32\LIB\gdi32.lib
included:\masm32\INCLUDE\user32.inc
includelibd:\masm32\LIB\user32.lib
included:\masm32\INCLUDE\kernel32.inc
includelibd:\masm32\LIB\kernel32.lib

.data?
hInstancedd?
hWinMaindd?
hWinMainb1dd?

.const

szClassName db'my',0
szCaptionMaindb'my window!',0
szButtondb'button',0
szButtonTextdb'exit',0

.code
_myprocprocuses ebx edi esi ebp,hWnd,uMsg,wParam,lParam
mov eax,uMsg
.ifeax == hWinMainb1
mov eax,uMsg
.elseifeax == WM_LBUTTONDOWN
mov eax,wParam
.elseif eax == MK_LBUTTON
mov eax,uMsg
.elseif eax == WM_PARENTNOTIFY
invokeDestroyWindow,hWinMain
invokePostQuitMessage,NULL
.else
invokeDefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xoreax,eax
ret

_myprocendp

_WinMainproc
local @stWndClass:WNDCLASSEX
local @stMsg:MSG

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
invoke LoadCursor,0,IDC_ARROW
mov @stWndClass.hCursor,eax
push hInstance
pop @stWndClass.hInstance
mov @stWndClass.cbSize,sizeof WNDCLASSEX
mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW or CS_DBLCLKS
mov @stWndClass.lpfnWndProc,offset _myproc
mov @stWndClass.hbrBackground,COLOR_MENU+1
mov @stWndClass.lpszClassName,offset szClassName
invoke RegisterClassEx,addr @stWndClass
invoke CreateWindowEx,WS_EX_ACCEPTFILES,offset szClassName,\
offset szCaptionMain,WS_OVERLAPPED,150,150,400,400,\
NULL,NULL,hInstance,NULL
mov hWinMain,eax
invoke ShowWindow,hWinMain,SW_SHOWNORMAL
invoke CreateWindowEx,WS_EX_WINDOWEDGE,offset szButton,\
offset szButtonText,WS_CHILD or WS_VISIBLE,100,100,80,80,\
hWinMain,1,hInstance,NULL
mov hWinMainb1,eax
.while TRUE
invoke GetMessage,addr @stMsg,NULL,0,0
.break .if eax == 0
invokeDispatchMessage,addr @stMsg
.endw
ret
_WinMainendp
start:
call_WinMain
invokeExitProcess,NULL
endstart

[解决办法]
响应WM_PAINT消息,调用BeginPaint、EndPaint处理窗口绘制;
响应WM_COMMAND消息处理按钮事件;
DispatchMessage之前要TranslateMessage。
[解决办法]
Mark!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[解决办法]

C/C++ code
.386.model flat,stdcalloption casemap:noneinclude d:\masm32\INCLUDE\windows.incinclude d:\masm32\INCLUDE\gdi32.incincludelib d:\masm32\LIB\gdi32.libinclude d:\masm32\INCLUDE\user32.incincludelib d:\masm32\LIB\user32.libinclude d:\masm32\INCLUDE\kernel32.incincludelib d:\masm32\LIB\kernel32.libBUTTONID equ 1.data?hInstance dd ?hWinMain dd ?hWinMainb1 dd ?.constszClassName db 'my',0szCaptionMain db 'my window!',0szButton db 'button',0szButtonText db 'exit',0.code_myproc proc uses ebx edi esi ebp,hWnd,uMsg,wParam,lParammov eax,uMsg.if eax == hWinMainb1mov eax,uMsg.elseif eax == WM_LBUTTONDOWNmov eax,wParam.elseif eax == MK_LBUTTONmov eax,uMsg.elseif eax == WM_PARENTNOTIFYinvoke DestroyWindow,hWinMaininvoke PostQuitMessage,NULL.elseif eax==WM_CREATEinvoke CreateWindowEx,NULL,addr szButton,addr szButtonText,WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,200,200,140,20,hWnd,BUTTONID,hInstance,NULL.elseinvoke DefWindowProc,hWnd,uMsg,wParam,lParamret.endifxor eax,eaxret_myproc endp_WinMain proclocal @stWndClass:WNDCLASSEXlocal @stMsg:MSGinvoke GetModuleHandle,NULLmov hInstance,eaxinvoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClassinvoke LoadCursor,0,IDC_ARROWmov @stWndClass.hCursor,eaxpush hInstancepop @stWndClass.hInstancemov @stWndClass.cbSize,sizeof WNDCLASSEXmov @stWndClass.style,CS_HREDRAW or CS_VREDRAW or CS_DBLCLKSmov @stWndClass.lpfnWndProc,offset _myprocmov @stWndClass.hbrBackground,COLOR_MENU+1mov @stWndClass.lpszClassName,offset szClassNameinvoke RegisterClassEx,addr @stWndClassinvoke CreateWindowEx,WS_EX_ACCEPTFILES,offset szClassName,\offset szCaptionMain, WS_OVERLAPPEDWINDOW,150,150,400,400,\NULL,NULL,hInstance,NULLmov hWinMain,eaxinvoke ShowWindow,hWinMain,SW_SHOWNORMALinvoke CreateWindowEx,WS_EX_WINDOWEDGE,offset szButton,\offset szButtonText,WS_OVERLAPPEDWINDOW ,100,100,80,80,\hWinMain,1,hInstance,NULLmov hWinMainb1,eaxinvoke   ShowWindow,hWinMainb1,SW_SHOWNORMAL    ;显示窗口invoke   UpdateWindow,hWinMainb1      ;更新窗口.while TRUEinvoke GetMessage,addr @stMsg,NULL,0,0.break .if eax == 0invoke TranslateMessage,addr @stMsginvoke DispatchMessage,addr @stMsg.endwret_WinMain endpstart:call _WinMaininvoke ExitProcess,NULLend start 


[解决办法]
The WM_PARENTNOTIFY message is sent to the parent of a child window when the child window is created or destroyed, or when the user clicks a mouse button while the cursor is over the child window. When the child window is being created, the system sends WM_PARENTNOTIFY just before the CreateWindow or CreateWindowEx function that creates the window returns. When the child window is being destroyed, the system sends the message before any processing to destroy the window takes place.
你在创建按钮时系统发送WM_PARENTNOTIFY消息给窗口过程,窗口过程代码中WM_PARENTNOTIFY消息分支下又销毁了窗体,才会这样。一般DestroyWindow和PostQuitMessage的调用放在WM_CLOSE分支下处理。

读书人网 >汇编语言

热点推荐