edittext idc_edit, 15,17,111,13, es_autohscroll | es_left
defpushbutton "say hello", idc_button, 141,10,52,13
pushbutton "e&xit", idc_exit, 141,26,52,13
end
上面的一块定义了对话框中的子窗口控件,它们是声明在一头一尾的两个关键字begin和end之间的。
- control-type "text" ,controlid, x, y, width, height [,styles]
现在我们来看看汇编源代码。先看这部分:
- mov wc.cbwndextra,dlgwindowextra
mov wc.lpszclassname,offset classname
- invoke createdialogparam,hinstance,addr dlgname,null,null,null
- invoke getdlgitem,hdlg,idc_edit
invoke setfocus,eax
invoke isdialogmessage, hdlg, addr msg
.if eax ==false
invoke translatemessage, addr msg
invoke dispatchmessage, addr msg
.endif
现在程序进入消息循环,在我们翻译和派发消息前,该函数使得对话框内置的对话框管理程序来处理有关的键盘跳转逻辑。如果该函数返回true,则表示消息是传给对话框的已经由该函数处理了。注意和前一课不同,当我们想得到控件的文本信息时调用getdlgitemtext函数而不是getwindowtext函数,前者接受的参数是一个控件的id 号,而不是窗口的句柄,这使得在对话框中调用该函数更方便。
好我们现在使用第二种方法把一个对话框当成一个主窗口来使用。在接下来的例子中,我们将产生一个应用程序的模式对话框,您将会发现其中根本没有消息循环或窗口处理过程,因为它们根本没有必要!
.386
.model flat,stdcall
option casemap:none
dlgproc proto :dword,:dword,:dword,:dword
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
.data
dlgname db "mydialog",0
appname db "our second dialog box",0
teststring db "wow! i'm in an edit box now",0
.data?
hinstance hinstance ?
commandline lpstr ?
buffer db 512 dup(?)
.const
idc_edit equ 3000
idc_button equ 3001
idc_exit equ 3002
idm_gettext equ 32000
idm_clear equ 32001
idm_exit equ 32002
.code
start:
invoke getmodulehandle, null
mov hinstance,eax
invoke dialogboxparam, hinstance, addr dlgname,null, addr dlgproc, null
invoke exitprocess,eax
dlgproc proc hwnd:hwnd, umsg:uint, wparam:wparam, lparam:lparam
.if umsg==wm_initdialog
invoke getdlgitem, hwnd,idc_edit
invoke setfocus,eax
.elseif umsg==wm_close
invoke sendmessage,hwnd,wm_command,idm_exit,0
.elseif umsg==wm_command
mov eax,wparam
.if lparam==0
.if ax==idm_gettext
invoke getdlgitemtext,hwnd,idc_edit,addr buffer,512
invoke messagebox,null,addr buffer,addr appname,mb_ok
.elseif ax==idm_clear
invoke setdlgitemtext,hwnd,idc_edit,null