delphi 消息提示窗口总结

来源:互联网 发布:图像对比分析软件 编辑:程序博客网 时间:2024/05/19 02:05
1. Showmessage方法。
Showmessage过程显示的对话框以应用程序的执行文件名作为标题,对话框只含有一个OK按钮,单击该按钮对话框即关闭并返回。
例如:showmessage('显示结果')。
一般为调试时期使用。

 
2.   Application.MessageBox详解
引数:
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参数

= Decimal (integer)= Scientific= Fixed= General= Money= Number (floating)= Pointer= String= Unsigned decimal

= Hexadecimal

格式:

%[Index:][-][Width][.Precision]Type
例如:

var
  text : string;
begin
    ShowMessageFmt('%s', ['Hello']);

  ShowMessageFmt('String = %s', ['Hello']);
  ShowMessage('');

    ShowMessageFmt('Decimal          = %d', [-123]);
  ShowMessageFmt('Exponent         = %e', [12345.678]);
  ShowMessageFmt('Fixed            = %f', [12345.678]);
  ShowMessageFmt('General          = %g', [12345.678]);
  ShowMessageFmt('Number           = %n', [12345.678]);
  ShowMessageFmt('Money            = %m', [12345.678]);
  ShowMessageFmt('Pointer          = %p', [addr(text)]);
  ShowMessageFmt('String           = %s', ['Hello']);
  ShowMessageFmt('Unsigned decimal = %u', [123]);
  ShowMessageFmt('Hexadecimal      = %x', [140]);
end;

结果如下:

 

   Hello
   String = Hello
  
   Decimal          = -123
   Exponent         = 1.23456780000000E+004
   Fixed            = 12345.68
   General          = 12345.678
   Number           = 12,345,68
   Money            = ?12,345.68
   Pointer          = 0069FC90
   String           = Hello
   Unsigned decimal = 123
   Hexadecimal      = 8C
  使用宽度显示

begin
   ShowMessageFmt('Padded decimal    = <}>', [1234]);

    ShowMessageFmt('Justified decimal = <%-7d>', [1234]);

    ShowMessageFmt('0 padded decimal  = <%.6d>', [1234]);

      ShowMessageFmt('Width + precision = <%8.6d>', [1234]);

      ShowMessageFmt('Reposition after 3 strings = %s %s %s %1:s %s',
                     ['Zero', 'One', 'Two', 'Three']);

    ShowMessageFmt('In line           = <.4d>', [1234]);
  ShowMessageFmt('Part data driven  = <%*.4d>', [10, 1234]);
  ShowMessageFmt('Data driven       = <%*.*d>', [10, 4, 1234]);
end;

结果如下:

   Padded decimal    = <   1234>
   Justified decimal = <1234   >
   0 padded decimal  = <001234>
   Width + precision = <  001234>
   Reposition after 3 strings = Zero One Two One Two
   In line           = <      1234>
   Part data driven  = <      1234>
   Data driven       = <      1234>
原创粉丝点击