MessageBox函数创建,显示并操作消息对话框。消息对话框包含应用程序定义的消息和标题,加上任何预定义的图标与按钮的组合。
int MessageBox(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
);
Parameters
hWnd
Identifies the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.
lpText
Points to a null-terminated string containing the message to be displayed.
lpCaption
Points to a null-terminated string used for the dialog box title. If this parameter is NULL, the default title Error is used.
uType
Specifies a set of bit flags that determine the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.
[--SNIP--]
在这段文字后有所有常数和标志的列表(他们定义在windows.inc中)。因为它太长了,我没有在这里列出来。通过查看参考,你就知道MessageBox函数要4个参数:父窗口(owner),指向消息串的指针,指向标题串的指针和消息框的类型。
HWnd可以是Null。因为我们的程序没有窗口。
LpText必须是指向我们文本的指针。这仅仅意为参数是文本所在内存地址的offset。
LpCaption 是标题串的offset。
UType 是参考中解释的像MB_OK,MB_OKCANCEL,MB_ICONERROR等值的组合。
让我们先定义两个用于MessageBox的字符串:
在first.asm中加入:
.data
MsgText db "Hello world!",0
MsgTitle db "This is a messagebox",0
.data 指示data部分的开始。用db,字节直接被插入,而且字符串又只是字节的集合,data部分会在包含上面的字符串,附加上结尾的0。MsgText装有第一个字符串的offset。MsgTitle有第二个字符串的offset。现在我们可以使用函数:
invoke MessageBox, NULL, offset MsgText, offset MsgTitle, Null
但因为用的是invoke,你可以使用(更安全)ADDR代替offset:
invoke MessageBox, Null, ADDR MsgText, ADDR MsgTitle, Null
我们还没有看最后一个参数,但这不会有什么问题。因为MB_OK(有一个ok按钮的消息对话框的样式)等于0(NULL)。但你也可以使用其他的任何样式。Utype(第4个参数)的定义是:
指定一系列决定对话框内容与行为的位标志。这个参数可以是下面标志组中标志的组合。
现在以我们要一个有OK按钮与“information”图标的简单消息对话框为例。MB_OK是OK按钮的样式,MB_ICONINFORMATION是information图标的样式。样式是用“or”操作符联合的。这不是or伪代码。Masm会在汇编前处理or操作。不用or,你可以用+号(加号)代替,但有时对层叠样式有问题(一个样式包含其他一些样式)。但在本例中你也可以用+号。
.code
start:
invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK + MB_ICONINFORMATION
end start
把以上的代码加入到你的first.asm文件中。
我们还加入了一个start标签。如果你现在汇编你的程序并运行它,它将显示一个消息对话框但很有可能在你点OK之后就崩溃了。这是因为程序没有结束,而处理器开始执行MessageBox代码后的任何东西。Windows中程序是用ExitProcess函数结束的:
VOID ExitProcess(
UINT uExitCode //对于所有线程的退出代码 );
我们可以把0用作退出码。
把你的代码改成这样:
.code
start:
invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK + MB_ICONINFORMATION
invoke ExitProcess, NULL
end start
12.4-第4步
因此我们最终的程序是:
.486
.model flat, stdcall
option casemap:none
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\windows.inc
.data
MsgText db "Hello world!",0
MsgTitle db "This is a messagebox",0
.code
start:
invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK or MB_ICONINFORMATION
invoke ExitProcess, NULL
end start