汇编语言学习笔记-使用窗体控件
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
.386
.model flat,stdcall;内存平坦,参数传递约定
option casemap:none;大小写敏感
;;;;;;引用一些必要的数据
include D:\masm32\include\windows.inc
include D:\masm32\include\user32.inc
include \masm32\include\gdi32.inc
includelib D:\masm32\lib\user32.lib
include D:\masm32\include\kernel32.inc
includelib D:\masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD;函数声明,将在后面进行函数的定义
;;;;;;已定义数据的数据段
.DATA
ClassName db "SimpleWinClass",0
AppName db "Our First Window",0
ButtonClassName db "button",0
ButtonText db "My First Button",0
EditClassName db "edit",0
TestString db "I'm in an edit box now"
;;;;;;未定义数据的数据段
.DATA?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hwndButton HWND ?
hwndEdit HWND ?
buffer db 512 dup(?);缓存文本框的数据
.const
ButtonID equ 1
EditID equ 2
IDM_GETTEXT equ 3
IDM_HELLO equ 1
IDM_CLEAR equ 2
IDM_EXIT equ 3
;;;;;;代码段
.CODE
start: ;程序的入口与end start对应
invoke GetModuleHandle,NULL;调用API函数获取应用程序句柄并把句柄存入eax寄存器
mov hInstance,eax
invoke GetCommandLine;获取命令行的字符串指针
mov CommandLine,eax
invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT;调用子函数,也就是前面我们定义的函数
invoke ExitProcess,eax;退出程序
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD;子函数的定义,包括函数名称,参数类型,参数名称
;子程序的开始必须先定义该子程序用到的变量
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
;初始化窗口类,这个窗口类描述我们的窗口长的什么样子
mov wc.cbSize,SIZEOF WNDCLASSEX;窗口类结构将占用多少个字节
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,NULL
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;注册窗体
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;显示窗口
invoke UpdateWindow,hwnd;刷新窗口
;开始消息循环
.WHILE TRUE
invoke GetMessage,ADDR msg,NULL,0,0;获取消息队列中的消息
.BREAK .IF (!eax);如果获取的消息为WM_QUIT就退出消息循环
invoke TranslateMessage,ADDR msg;按键消息的转义
invoke DispatchMessage,ADDR msg;把消息发送到负责消息处理的函数,回调方法
.ENDW
mov eax,msg.wParam;把退出的消息代码码放到EAX里然后回到主函数
ret
WinMain endp
WndProc proc hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM;消息处理函数,此函数可以随便命名
.IF uMsg == WM_DESTROY;销毁窗口的消息
invoke PostQuitMessage,NULL
.ELSEIF uMsg==WM_CREATE
invoke CreateWindowEx,WS_EX_CLIENTEDGE,ADDR EditClassName,NULL,\
WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT or ES_AUTOHSCROLL\
,50,35,200,25,hWnd,EditID,hInstance,NULL
mov hwndEdit,eax
invoke SetFocus,hwndEdit
invoke CreateWindowEx,NULL,ADDR ButtonClassName,\
ADDR ButtonText,WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,\
75,70,140,25,hWnd,ButtonID,hInstance,NULL
mov hwndButton,eax
.ELSEIF uMsg==WM_COMMAND
mov eax,wParam
.IF ax == ButtonID
shr eax,16
.IF ax == BN_CLICKED
invoke SendMessage,hWnd,WM_COMMAND,IDM_GETTEXT,0
invoke GetWindowText,hwndEdit,ADDR buffer,512
invoke MessageBox,NULL,ADDR buffer,ADDR AppName,MB_OK
.ENDIF
.ENDIF
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam;默认的消息处理函数
ret;退出此子程序
.ENDIF
xor eax,eax;eax置零
ret
WndProc endp
end start