Dialog Examples/zh CN
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
日本語 (ja) │
polski (pl) │
русский (ru) │
slovenčina (sk) │
中文(中国大陆) (zh_CN) │
一些有用的对话框
这是在组件调色板中找不到的一些有用的对话框:
procedure ShowMessage (const Msg: string);
function MessageBox (Text, Caption : PChar; Flags: Word): Integer;
function MessageDlg (const Msg: string; AType: TMsgDlgType; AButtons: TMsgDlgButtons; HelpCtx: LongInt): Word;
function InputBox (const ACaption, APrompt, ADefault: string); string;
function InputQuery (const ACaption, APrompt: string; var Value: string): Boolean;
function PasswordBox(const ACaption, APrompt : String) : String;
这些对话框组件都可以显示一个小型的弹出对话框,对话框中包含一些信息并需要用户响应:要么按下一个按钮,要么输入一些文本,再者两者都有所要求。程序员几乎不能控制这些弹出对话框的格式,大小或位置,但是可以控制它们的文本内容。
The reason why there are often several very similar alternatives, 允许不同的方法来调用组件并从过程或函数中接收回数据。
这些对话框可能是依赖于平台的。例如,它们可能显示不同。例如,字符串在Windows XP中会完全显示, 可能在Windows 7中会显示不全。
信息对话框
消息对话框显示一条消息并等待一次按键或鼠标点击响应。
ShowMessage
procedure ShowMessage(const Msg: string);
{ 在Dialogs.pp中定义 }
最简单的信息对话框: 获取一个简单的字符串作为参数,并在一个模板化的对话框中显示该字符串。并在返回调用routine|实例程序或program|程序前,等待一次鼠标单击或按下 ↵ Enter-按键。
这是一个模式化过程调用,这会是一个在单击确定或其它选择前一直会显示、接收焦点、并且不放弃焦点的对话框。
示例:
program LazMessage;
uses
Dialogs;
begin
ShowMessage('This is a message from Lazarus');
end.
你能够使用下面的行分隔符来创建多行消息,这些行分隔符都能工作:
sLineBreak
LineEnding
- 或字符代码:#13#10
多行消息示例:
program LazMessage;
uses
Dialogs;
begin
ShowMessage('This is a multilines' + sLineBreak + 'message!' );
end.
MessageBox
function Application.MessageBox(Text, Caption: PChar; Flags: LongInt): Integer;
{ 在Forms.pp中定义,是TApplication的一部分;因此必须称其为Application.Messagebox()或者使用'with
Application
do
...' 构造 }
参数包括
- Text: the string that is displayed as a prompt or instruction in the Box;
- Caption: the string label at the top of the message box;
- Flags: longint - an integer constructed by adding together various constants to define the contents and behaviour of the box, for example
MB_ABORTRETRYIGNORE + MR_ICONQUESTION
will cause the application to display a query (?) icon in a box with three buttons: ABORT RETRY IGNORE.
The function returns an integer value corresponding to the button that was pressed; its value can be determined by reference to the constants [IDOK..IDHELP]
It can be invoked like a procedure call (ie as a 'MessageBox()' statement rather than as a 'Variable := MessageBox()' function call - see example below)
示例:
uses
Forms, Dialogs, LCLType;
procedure DisplayMessageBox;
var
Reply, BoxStyle: Integer;
begin
BoxStyle := MB_ICONQUESTION + MB_YESNO;
Reply := Application.MessageBox('Press either button', 'MessageBoxDemo', BoxStyle);
if Reply = IDYES then Application.MessageBox('Yes ', 'Reply',MB_ICONINFORMATION)
else Application.MessageBox('No ', 'Reply', MB_ICONHAND);
end;
注意,在这个示例中,字符串'Yes'和'No'需要使用空格填充;否则对话框可能会没有足够的宽度来正确地显示标题
MessageDLG
function MessageDlg(const aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
function MessageDlg(const aCaption, aMsg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
这个函数有两个版本,第一个版本:'Caption'参数是可选的;如果省略掉,对话框会丢失标题
This is the most complete and elaborate of the message dialogs, and allows the programmer considerable control over the appearance of the dialog box.
The parameters defining the kind of box and its icon are types rather than integer constants, and the buttons can be specified as a set in square brackets eg [mbRetry, mbIgnore, mbAbort, mbCancel]
.
The HelpCtx parameter is not currently implemented and should be set to zero.
The return value from the function is the identity of the button pressed, expressed as an integer (see the definitions below, [mrNone..mrAll]).
示例:
uses
Forms, Dialogs, LCLType, Controls;
procedure TryMessageDlg;
begin
if MessageDlg('Question', 'Do you wish to Execute?', mtConfirmation,
[mbYes, mbNo, mbIgnore],0) = mrYes
then { Execute rest of Program };
end;
QuestionDlg
询问对话框允许各国按钮标题和设置默认按钮和取消按钮。
示例:
case QuestionDlg ('Caption','Message',mtCustom,[mrYes,'Positive', mrNo, 'Negative', 'IsDefault'],'') of
mrYes: QuestionDlg ('Caption','So you mean „Yes“',mtCustom,[mrOK,'That is right'],'');
mrNo: QuestionDlg ('Caption','Oh, you mean „No“',mtCustom,[mrOK,'Exactly'],'');
mrCancel: QuestionDlg ('Caption','You canceled the dialog with ESC or close button.',mtCustom,[mrOK,'Exactly'],'');
end;
针对按钮的高级示例(来自promptdialog.inc)
[mrOk,mrCancel,'Cancel now',mrIgnore,300,'Do it','IsDefault']
这将产生4个按钮:
- 'Ok'返回mrOk
- 'Cancel now'返回mrCancel
- 'Ignore'返回mrIgnore
- 'Do it'返回300。这将是默认的按钮(聚焦选择项)
通常按钮在Lazarus对话框中有图标。In order to prevent icons from being shown non standardize values for message results can be used.当前最高标准值是11。例如:
case QuestionDlg ('Caption','Message',mtCustom,[20,'Positive', 21, 'Negative',22,'I do not know','IsCancel'],'') of
20: QuestionDlg ('Caption','So you mean „Yes“',mtCustom,[20,'That is right'],'');
21: QuestionDlg ('Caption','Oh, you mean „No“',mtCustom,[21,'Exactly'],'');
22: QuestionDlg ('Caption','So, please find out!',mtCustom,[22,'Maybe'],'');
end;
为了便于工作,常量可以被定义,例如:
const
mrNoneNI= 20;
mrOkNI= mrNoneNI+1;
mrCancelNI= mrNoneNI+2;
mrAbortNI= mrNoneNI+3;
mrRetryNI= mrNoneNI+4;
mrIgnoreNI= mrNoneNI+5;
mrYesNI= mrNoneNI+6;
mrNoNI= mrNoneNI+7;
mrAllNI= mrNoneNI+8;
mrYesToAllNI= mrNoneNI+10;
mrCloseNI= mrNoneNI+11;
mrLastNI= mrCloseNI;
begin
case QuestionDlg ('Caption','Message',mtCustom,[mrYesNI,'Positive', mrNoNI, 'Negative',mrCancelNI,'I do not know','IsCancel'],'') of
mrYesNI: QuestionDlg ('Caption','So you mean „Yes“',mtCustom,[mrYesNI,'That is right'],'');
mrNoNI: QuestionDlg ('Caption','Oh, you mean „No“',mtCustom,[mrNoNI,'Exactly'],'');
mrCancelNI: QuestionDlg ('Caption','So, please find out!',mtCustom,[mrCancelNI,'Maybe'],'');
end; //case
end;
文本输入对话框
InputBox
文本输入对话框:显示一条信息,并等待用户输入文本
function InputBox(const ACaption, APrompt, ADefault: String): String;
显示一个带有已定义标题和提示的对话框,并要求用户在一个文本框中输入文本。A default string can optionally be displayed in the text box.用户输入的字符串或默认的字符串将作为函数的结果而返回。
示例:
uses
Forms, LCLType, Dialogs, Controls;
procedure TryInputBox;
var
UserString: string;
begin
UserString := InputBox('Get some text input',
'Please type in some information', 'Some sample text');
ShowMessage(UserString)
end;
InputQuery
function InputQuery(const ACaption, APrompt : String;
MaskInput : Boolean; var Value : String) : Boolean;
function InputQuery(const ACaption, APrompt : String;
var Value : String) : Boolean;
这个函数的第二个版本,显示一个提示并要求用户输入文本数据; 这个函数的第一个版本,包含一个MaskInput 布尔值参数,which determines whether the user input is masked out by asterisks (*) in the text-input box (like during entry of a password), while the second omits this property. The text entered by the user is returned in the variable parameter 'Value'; the function result is a boolean which returns true
if the OK button was pressed, or
false
if the box was closed by any other mechanism (such as clicking the 'Close' icon on the top title bar). Omitting the MaskInput parameter is equivalent to setting it false
.
示例:
uses
Forms, LCLType, Dialogs, Controls;
procedure TryInputQuery;
var
QueryResult: Boolean;
UserString: string;
begin
if InputQuery('Question', 'Type in some data', TRUE, UserString)
then ShowMessage(UserString)
else
begin
InputQuery('Don''t be silly', 'Please try again', UserString);
ShowMessage(UserString);
end
end;
PasswordBox
Function PasswordBox(const ACaption, APrompt : String) : String;
Behaves very similarly to the InputQuery function with MaskInput = TRUE; the difference is that the password that was typed in is returned as the result of the function (like InputBox).
Constants and Types used in message dialogs
Several constants and types relevant for use with the dialog boxes are pre-defined in the LCL library:
const { Defined in LCLType.pp }
integer constants for defining the types of buttons and the icon for display in MessageBox
MB_OK = $00000000;
MB_OKCANCEL = $00000001;
MB_ABORTRETRYIGNORE = $00000002;
MB_YESNOCANCEL = $00000003;
MB_YESNO = $00000004;
MB_RETRYCANCEL = $00000005;
MB_ICONHAND = $00000010;
MB_ICONQUESTION = $00000020;
MB_ICONEXCLAMATION = $00000030;
MB_ICONASTERICK = $00000040;
MB_ICONWARNING = MB_ICONEXCLAMATION;
MB_ICONERROR = MB_ICONHAND;
MB_ICONINFORMATION = MB_ICONASTERICK;
integer constants defining the return value from MessageBox according to which button was pressed
IDOK = 1; ID_OK = IDOK;
IDCANCEL = 2; ID_CANCEL = IDCANCEL;
IDABORT = 3; ID_ABORT = IDABORT;
IDRETRY = 4; ID_RETRY = IDRETRY;
IDIGNORE = 5; ID_IGNORE = IDIGNORE;
IDYES = 6; ID_YES = IDYES;
IDNO = 7; ID_NO = IDNO;
IDCLOSE = 8; ID_CLOSE = IDCLOSE;
IDHELP = 9; ID_HELP = IDHELP;
define whether first, second or third button is default
MB_DEFBUTTON1 = $00000000;
MB_DEFBUTTON2 = $00000100;
MB_DEFBUTTON3 = $00000200;
MB_DEFBUTTON4 = $00000300;
The Flags parameter of MessageBox is constructed by adding a button constant [MB_OK..MB_RETRYCANCEL], an optional icon constant [MB_ICONHAND..MB_ICONINFORMATION] and an optional default button constant [MB_DEFBUTTON1..MB_DEFBUTTON3]
Types for use in MessageDlg, which needs parameters AType of TMsgDlgType and AButtons of TMSgDlgButtons
{ Defined in Dialogs.pp }
type
TMsgDlgType = (mtWarning, mtError, mtInformation, mtConfirmation,
mtCustom);
TMsgDlgBtn = (mbYes, mbNo, mbOK, mbCancel, mbAbort, mbRetry, mbIgnore,
mbAll, mbNoToAll, mbYesToAll, mbHelp, mbClose);
TMsgDlgButtons = set of TMsgDlgBtn;
const
mbYesNoCancel = [mbYes, mbNo, mbCancel];
mbOKCancel = [mbOK, mbCancel];
mbAbortRetryIgnore = [mbAbort, mbRetry, mbIgnore];
MsgDlgBtnToBitBtnKind: array[TMsgDlgBtn] of TBitBtnKind = (
bkYes, bkNo, bkOK, bkCancel, bkAbort, bkRetry, bkIgnore,
bkAll, bkNoToAll, bkYesToAll, bkHelp, bkClose
);
BitBtnKindToMsgDlgBtn: array[TBitBtnKind] of TMsgDlgBtn = (
mbOk, mbOK, mbCancel, mbHelp, mbYes, mbNo,
mbClose, mbAbort, mbRetry, mbIgnore, mbAll, mbNoToALl, mbYesToAll
);
{ Defined in Controls.pp }
const
mrNone = 0;
mrOK = mrNone + 1;
mrCancel = mrNone + 2;
mrAbort = mrNone + 3;
mrRetry = mrNone + 4;
mrIgnore = mrNone + 5;
mrYes = mrNone + 6;
mrNo = mrNone + 7;
mrAll = mrNone + 8;
mrNoToAll = mrNone + 9;
mrYesToAll = mrNone + 10;
mrLast = mrYesToAll;