21,459
社区成员
发帖
与我相关
我的任务
分享
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
includelib user32.lib ; calls to functions in user32.lib and kernel32.lib
include kernel32.inc
includelib kernel32.lib
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD ;定义winmain函数原形,相当于C里面声明函数
.DATA ; initialized data
ClassName db "SimpleWinClass",0 ; the name of our window class
AppName db "Our First Window",0 ; the name of our window
dText db "1212121212121",0
MenuName db "WinMainMenu",0
EditClassName db "dialog",0
Test_string db "You selected Test menu item",0
Hello_string db "Hello, my friend",0
Goodbye_string db "See you again, bye",0
szExitApp db "你真的要退出程序吗?",0
szTitle db "对话框程序",0
;将两个字符串合并为一个的格式化字符串。
szFormat db "%s%s", 0
.DATA? ; Uninitialized data
hInstance HINSTANCE ? ; Instance handle of our program
CommandLine LPSTR ? ;接受程序运行时传入的参数,例如:dos下运行一些命令时带参数
hInstance1 HINSTANCE ?
.const
IDM_TEST equ 0001h ; Menu IDs
IDM_HELLO equ 0002h
IDM_GOODBYE equ 0003h
IDM_EXIT equ 0004h
ICO_MAIN equ 1000h
IDD_DLG_MAIN equ 2000
IDC_BTN_OUTPUT equ 2001
IDC_EDT_FIRST equ 2002
IDC_EDT_SECOND equ 2003
IDC_EDT_Third equ 2004
IDC_EDT_Four equ 2005
IDC_BTN_EXITAPP equ 2006
.CODE ; Here begins our code
DlgProc proc uses ebx esi edi hwnd, uMsg, wParam, lParam
;定义了三个局部数组,类型均为字节型
LOCAL @szBuffer1[256]:byte
LOCAL @szBuffer2[256]:byte
LOCAL @szOutputBuf[512]:byte
mov eax, uMsg
.if eax == WM_INITDIALOG
;这里编写窗口初始化的代码,下面是将三个局部数组清零
invoke RtlZeroMemory, addr @szBuffer1, sizeof @szBuffer1
invoke RtlZeroMemory, addr @szBuffer2, sizeof @szBuffer2
invoke RtlZeroMemory, addr @szOutputBuf, sizeof @szOutputBuf
.elseif eax == WM_COMMAND
mov eax, wParam
.if ax == IDC_EDT_FIRST
;这里编写处理第一个文本框的代码
.elseif ax == IDC_EDT_SECOND
;这里编写处理第二个文本框的代码
.elseif ax == IDC_BTN_OUTPUT
;先获取两个文本框的文本,分别保存在@szBuffer1和@szBuffer2中
;然后通过格式化函数wsprintf将两个文本合并,最后输出到第三个文本框中。
invoke GetDlgItemText, hwnd, IDC_EDT_FIRST, addr @szBuffer1, sizeof @szBuffer1
invoke GetDlgItemText, hwnd, IDC_EDT_SECOND, addr @szBuffer2, sizeof @szBuffer2
invoke wsprintf, addr @szOutputBuf, addr szFormat, addr @szBuffer1, addr @szBuffer2
invoke SetDlgItemText, hwnd, IDC_EDT_Four, addr @szOutputBuf
.elseif ax == IDC_BTN_EXITAPP
invoke SendMessage, hwnd, WM_CLOSE, 0, 0
.endif
.elseif eax == WM_CLOSE
;这里处理程序退出代码
invoke MessageBox, hwnd, addr szExitApp, addr szTitle, MB_ICONQUESTION or MB_YESNO
.if al == IDYES
invoke EndDialog, hwnd, NULL
.endif
.else
mov eax, FALSE
ret
.endif
mov eax, TRUE
ret
DlgProc endp
start:
invoke GetModuleHandle, NULL ; 得到应用程序的句柄
; Under Win32, hmodule==hinstance mov hInstance,eax
mov hInstance,eax
invoke GetCommandLine ; get the command line. You don't have to call this function IF
; your program doesn't process the command line.
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT ; call the main function
invoke ExitProcess, eax ; quit our program. The exit code is returned in eax from WinMain.
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX ; create local variables on stack
LOCAL msg:MSG
LOCAL hwnd:HWND
mov wc.cbSize,SIZEOF WNDCLASSEX ; fill values in members of wc 结构的字节数
mov wc.style, CS_HREDRAW or CS_VREDRAW ;类风格
mov wc.lpfnWndProc, OFFSET WndProc ;窗口过程的地址
mov wc.cbClsExtra,NULL ;
mov wc.cbWndExtra,NULL
push hInstance
pop wc.hInstance ;所属的实例句柄
mov wc.hbrBackground,COLOR_WINDOW+1 ;背景色
mov wc.lpszMenuName,OFFSET MenuName ;窗口菜单
mov wc.lpszClassName,OFFSET ClassName ;类名字符串的地址
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax ;图标
mov wc.hIconSm,eax ;小图标
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax ;光标
invoke RegisterClassEx, addr wc ; register our window class
invoke CreateWindowEx,NULL,\
ADDR ClassName,\
ADDR AppName,\
WS_OVERLAPPEDWINDOW,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
NULL,\
NULL,\
hInst,\
NULL
mov hwnd,eax
invoke ShowWindow, hwnd,CmdShow ; display our window on desktop
invoke UpdateWindow, hwnd ; refresh the client area
.WHILE TRUE ; Enter message loop
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax) ;eax==0,跳出循环的条件
invoke TranslateMessage, ADDR msg ;处理键盘消息
invoke DispatchMessage, ADDR msg ;分派消息,进入消息处理(回调函数)
.ENDW
mov eax,msg.wParam ; return exit code in eax
ret
WinMain endp
WndProc proc uses ebx edi esi, hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
local @stPs:PAINTSTRUCT
local @stRect:RECT
local @hDc
;WM_DESTROY 是关闭程序的 WM_CLOSE 是关闭窗口的 WM_QUIT 是关闭消息环的
;预设函数对于WM_CLOSE 的处理方式是呼叫DestroyWindow, 并因而发出WM_DESTROY
;预设之WM_DESTROY 处理方式是呼叫PostQuitMessage,因此发出WM_QUIT。
;WM_CLOSE,调用DestroyWindow()
;DestroyWindow()又发送WM_DESTROY
;响应WM_DESTROY,调用WM_QUIT
;GetMessage()发现WM_QUIT,退出程序
;当DefWindowProc处理WM_DESTROY消息时,它不自动调用PostQutMessage。
;.IF uMsg==WM_DESTROY ; if the user closes our window
; invoke PostQuitMessage,NULL ; quit our application
.IF uMsg==WM_PAINT
invoke BeginPaint,hWnd,addr @stPs
mov @hDc,eax
invoke GetClientRect,hWnd,addr @stRect
invoke DrawText,@hDc,addr dText,-1,\
addr @stRect,DT_SINGLELINE or DT_CENTER or DT_VCENTER
invoke EndPaint,hWnd,addr @stPs
.ELSEIF uMsg==WM_COMMAND
mov eax,wParam
.IF ax==IDM_TEST
.ELSEIF ax==IDM_HELLO
invoke DialogBoxParam, NULL, IDD_DLG_MAIN, hInstance, offset DlgProc, NULL
.ELSEIF ax==IDM_GOODBYE
invoke MessageBox,NULL,ADDR Goodbye_string, OFFSET AppName, MB_OK
.ELSEIF ax==IDM_EXIT
invoke SendMessage,hWnd,WM_CLOSE,eax,1
.ENDIF
.elseif uMsg == WM_CLOSE
;invoke MessageBox,NULL,ADDR Test_string,OFFSET AppName,MB_OK
invoke DestroyWindow,hWnd
invoke PostQuitMessage,NULL
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam ; Default message processing
ret
.ENDIF
xor eax,eax ;eax 置零,比mov eax,0 更高效
ret
WndProc endp
end start