例如:showmessage('显示结果')。
1. Text:要显示的讯息
2. Caption:讯息视窗的标题列文字
3. Flags:讯息旗标
3.1. 可指定讯息视窗上的图示
3.2. 可指定讯息视窗出现的按钮
3.3. 可指定预设Focus在哪一个按钮
3.4. 可指定是否 Modal
3.5. 其他
引数说明:
Text、Caption 引数为 PCahr 型态,字串型态的变数可用 PChar()
转换,若直接传入一个字串的话,就不用转型。
例如:
var s: string;
....
s := '存档失败';
Application.MessageBox(PChar(s), '错误', MB_ICONERROR);
Flag 引数为 Longint 型态,可用的传入值有:
1. 指定讯息视窗上的图示
1.1 MB_ICONEXCLAMATION 或 MB_ICONWARNING
出现「黄三角形内有一个惊叹号」图示
1.2 MB_ICONINFORMATION 或 MB_ICONASTERISK
出现「白色椭圆内有一个蓝色小写 i」图示
1.3 MB_ICONQUESTION
出现「问号」图示
1.4 MB_ICONSTOP 或 MB_ICONERROR 或 MB_ICONHAND
出现「红色X」图示
2. 指定讯息视窗出现的按钮
2.1 MB_ABORTRETRYIGNORE
出现「Abort」「Retry」「Ignore」三个按钮
2.2 MB_OK
出现「Ok」按钮,为预设值。
2.3 MB_OKCANCEL
出现「Ok」「Cancel」两个按钮
2.4 MB_RETRYCANCEL
出现「Retry」「Cancel」两个按钮
2.5 MB_YESNO
出现「Yes」「No」两个按钮
2.6 MB_YESNOCANCEL
出现「Yes」「No」「Cancel」三个按钮
3. 可指定预设Focus在哪一个按钮
3.1 MB_DEFBUTTON1
指定focus在左边第一个按钮,这是预设值。
3.2 MB_DEFBUTTON2、MB_DEFBUTTON3、MB_DEFBUTTON4
以此类推,指定focus在左边第二、三、四个按钮
4. 可指定是否 Modal
4.1 MB_APPLMODAL
对于应用程式而言,是 modal form
4.2 MB_SYSTEMMODAL
对于作业系统而言,是 modal form
4.3 MB_TASKMODAL
对于 task 而言,是 modal form
一般来说,这跟 MB_APPLMODAL 是等效的,但是当应用程式中的所有
form 都隐藏的时候,就必须使用这个,才能达到 modal 的目的
5. 其他
5.1 MB_TOPMOST
将讯息视窗提至最前面
5.2 MB_RIGHT
将讯息文字向右对齐
在一个 Flag 要指定这么多的东西,要如何使用呢?
只要把要用的引数组合 or 起来就可以了。例如:
Application.MessageBox(PCahr(sMsg), PChar(sCap), MB_YESNO or MB_ICONQUESTION or MB_DEFBUTTON2)
这样会出现「问号」图示,「Yes」「No」两个按钮,而且 focus 在 No 按钮上
回传值:
1. IDABORT:使用者按了「Abort」按钮
2. IDCANCEL:使用者按了「Cancel」按钮
3. IDIGNORE:使用者按了「Ignore」按钮
4. IDNO:使用者按了「No」按钮
5. IDOK:使用者按了「Ok」按钮
6. IDRETRY:使用者按了「Retry」按钮
7. IDYES:使用者按了「Yes」按钮
3. MessageDlg()用法
1.最简单用法,不带图形
MessageBox(0,'不同意','提示',MB_OK);
MessageBox(0,'不同意','提示',MB_OKCANCEL );
2.警告对话框:
MessageBox(0,'密码不能为空,请务必输入密码','警告',MB_ICONEXCLAMATION);
3.提示信息对话框:
MessageBox(0,'密码不能为空,请务必输入密码','提示',MB_ICONASTERISK and MB_ICONINFORMATION);
4.复杂用法:
MessageBox(Handle, PChar('进程' + Listview1.Selected.caption +'不能被结束!'), PChar('提示'), MB_ICONEXCLAMATION);
附常用属性
系统默认图标,可在消息框上显示
X错误 MB_ICONHAND, MB_ICONSTOP, and MB_ICONERROR
?询问 MB_ICONQUESTION
!警告 MB_ICONEXCLAMATION and MB_ICONWARNING
i信息 MB_ICONASTERISK and MB_ICONINFORMATION
按钮的形式
MB_OK 默认
MB_OKCANCEL 确定取消
MB_YESNO 是否
MB_YESNOCANCEL 是否取消
返回值
IDCANCEL 取消被选
IDNO 否被选
IDOK 确定被选
IDYES 是被选
补充:
以上消息框的用法是在Delphi中的应用,如果不是,则要MessageBox(NULL,"ddd","ddd",MB_OK); 或MessageBox(hWnd,"ddd","ddd",MB_OK); hWnd为某窗口的句柄,或者直接用AfxMessageBox。
MessageDlgPos用法
窗口类型为一下几种模式:
mtWarning显示一个惊叹号
mtError显示一个红色的“X”
mtInformation显示在一个泡沫“我”
mtConfirmation显示一个问号
mtCustom显示消息
按钮值::
mbYes显示了“是”按钮
mbNo显示一个“否”按钮
mbOK显示“OK”按钮
mbCancel显示一个“取消”按钮
mbAbort显示“中止”按钮
mbRetry显示了“重试”按钮
mbIgnore显示一个“忽略”按钮
mbAll显示“全部”按钮
mbNoToAll显示“无所有”按钮
mbYesToAll Displys一个“是所有”按钮
mbHelp显示“帮助”按钮
返回值:
mrYes = 6
mrNo = 7
mrOK = 1
mrCancel = 2
mrAbort = 3
mrRetry = 4
mrIgnore = 5
mrAll = 8
mrNoToAll = 9
mrYesToAll = 10
例如:
1\
var
buttonSelected : Integer;
begin
// 对话框在坐标为 20,100处显示
buttonSelected :=MessageDlgPos('Confirmation',mtError, mbOKCancel, 0, 20, 100);
// 判断哪个键被按下
if buttonSelected = mrOK then ShowMessage('OK ');
if buttonSelected = mrCancel then ShowMessage('Cancel');
2\
var
buttonSelected : Integer;
begin
buttonSelected :=MessageDlgPos('Custom dialog',mtCustom, [mbYes,mbAll,mbCancel], 0, 20, 100);
if buttonSelected = mrYes then ShowMessage('Yes');
if buttonSelected = mrAll then ShowMessage('All');
if buttonSelected = mrCancel then ShowMessage('Cancel');
4. 输入提示框
function InputQuery(const ACaption, APrompt: string; var Value: string): Boolean;
InputQuery返回值为是否点了OK 输入的字符串放在了变量Value中
function InputBox(const ACaption, APrompt, ADefault: string): string;
inputBox返回值是字符串,也就是输入的字符串
5. ShowMessageFmt格式化提示用法
ShowMessageFmt(const Formatting: string;const Data: array of const ) ;
formatting参数
d | = Decimal (integer) |
e | = Scientific |
f | = Fixed |
g | = General |
m | = Money |
n | = Number (floating) |
p | = Pointer |
s | = String |
u | = Unsigned decimal |
x | = Hexadecimal |
格式:
%[Index:][-][Width][.Precision]Type
例如: