当前位置: 代码迷 >> 汇编语言 >> win32汇编小疑点
  详细解决方案

win32汇编小疑点

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



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

.data?
hInstance dd ?
hWinMain dd ?
hWinMainb1 dd ?

.const

szClassName db 'my',0
szCaptionMain db 'my window!',0
szButton db 'button',0
szButtonText db 'exit',0

.code
_myproc proc uses ebx edi esi ebp,hWnd,uMsg,wParam,lParam
mov eax,uMsg
.if eax == hWinMainb1
mov eax,uMsg
.elseif eax == WM_LBUTTONDOWN
mov eax,wParam
.elseif eax == MK_LBUTTON
mov eax,uMsg
.elseif eax == WM_PARENTNOTIFY
invoke DestroyWindow,hWinMain
invoke PostQuitMessage,NULL
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret

_myproc endp

_WinMain proc
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
invoke DispatchMessage,addr @stMsg
.endw
ret
_WinMain endp
start:
call _WinMain
invoke ExitProcess,NULL
end start

------解决方案--------------------------------------------------------
响应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
  相关解决方案