Dialog Examples/fr

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) español (es) suomi (fi) français (fr) 日本語 (ja) polski (pl) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

Quelques boîtes de dialogue pratiques

Ci-dessous quelques boîtes de dialogue pratiques non incluses dans la palette de composant :

  • 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;

Chacun de ces composants affiche une petite fenêtre contenant quelques informations et demandent une réponse à l'utilisateur : d'appuyer sur un bouton, d'entrer un texte ou les deux. Le développeur n'a que peu de contrôle sur le format, la taille ou la position de ces boîtes de dialogue, mais peut influer sur leur contenu textuel.
La raison pour laquelle il y a tant d'alternatives différentes , et de permettre différentes méthodes d'appel de composant et de recevoir des informations en retour depuis une procédure ou une fonction.

Message Dialogs

Message dialogs affiche un message et attend qu'une touche soit appuyée ou un double-clic de la souris.

ShowMessage

Procedure ShowMessage (const Msg: string);

{ Déclarée dans Dialogs.pp }

La boîte de dialogue renvoyant un message la plus simple : elle prend une simple chaîne de caractères en paramètre, l'affiche dans une fenêtre stéréotypée, et attend un clic de souris ou une touche appuyée avec de continuer le programme.
C'est un appel de procédure modal, tant que le bouton OK n'est pas cliqué ou validé par entrée la fenêtre reste en avant plan et garde le focus, interdisant tout autre manipulation dans le programme.

Exemple :

Program LazMessage;
Uses Dialogs;
begin
  ShowMessage ('Cest un message de Lazarus.')
end.

MessageBox

Function Application.MessageBox (Text, Caption: PChar; Flags: longint) : Integer;

{ Defined in Forms.pp as part of TApplication; hence must be called as Application.Messagebox () or using the 'with Application do ...' construct }

Paramètres :

  • Text : la chaîne de caractères qui est affichée dans la boîte de dialogue ;
  • Caption : la chaîne de caractère affiché dans la barre de titre de la boîte de dialogue ;
  • Flags : longint – une constante qui peut définir le contenu et le comportement de la boîte de dialogue, par exemple MB_ABORTRETRYIGNORE + MR_ICONQUESTION fera en sorte que le programme affichera l'icône d'un point d'interrogation dans la boîte de dialogue (?) accompagné de trois boutons : ABORT RETRY IGNORE.

La fonction renvoie un entier correspondant au bouton appuyé ; cette valeur peut être déterminé par en faisant référence aux constantes [IDOK..IDHELP]

Cette fonction peut être appelée en tant que procédure (ie comme 'MessageBox()' au lieu de 'Variable := MessageBox()' comme on le ferait normalement pour une appel de fonction – voir exemple ci-dessous)

Exemple :

Uses Forms, Dialogs, LCLType;

Procedure DisplayMessageBox;
var reply, boxstyle: integer;
begin
    with application do
    begin
      boxstyle :=  MB_ICONQUESTION + MB_YESNO;
      reply :=  MessageBox ('Appuyez sur un bouton', 'MessageBoxDemo', boxstyle);
      if reply = IDYES then MessageBox ('Oui', 'Répondre',MB_ICONINFORMATION)
      else MessageBox ('Non', 'Répondre', MB_ICONHAND);
    end;
end;
MessageBoxDemo.png
ReplyYes.png

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;

Il existe deux versions de cette fonction, si vous omettez de renseigner 'Caption', ce sera la première fonction qui sera appelée et 'Caption' sera vide dans la boîte de dialogue.

C'est la plus complète et la plus abouti des fonctions d'appel de boîte de dialogue, elle autorise au programmeur un contrôle maximal sur l'apparence de la boîte. Les paramètres définicent le type de boîte et son icône via une constante entière, et les boutons peuvent être spécifié via [mbRetry, mbIgnore, mbAbort, mbCancel]. Le paramètre HelpCtx n'est pas encore implémenté et doit être initialisé à zéro. La valeur de retour de cette fonction et la valeur correspondante au bouton appuyé, exprimé par un entier (voir les définitions plus bas, [mrNone..mrAll]).

Exemple :

Uses forms, dialogs, lcltype, controls;

procedure TryMessageDlg;
begin
  if MessageDlg ('Question', 'Souhaitez-vous l'exécuter ?', mtConfirmation, 
                 [mbYes, mbNo, mbIgnore],0) = mrYes
  then { Exécuter la suite du programme};
end;


Question.png

Boîtes de dialogue de texte

InputBox

Boîte de dialogue de texte : affiche un message et attend que l'utilisateur entre une information sous forme de texte.

Function InputBox(const ACaption, APrompt, ADefault : String) : String;

Displays a box with defined title and prompt, and expects user input in a text box. Une chaîne de caractère réponse, peut être rentré optionnellement. The user-entered or default string is returned as the function result.

Exemple :

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;

Two versions of this function which displays a prompt and expects user input of textual data; the first includes a MaskInput boolean parameter 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.

Exemple :

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 ('Dont be silly', 'Please try again', userstring);
    ShowMessage (userstring)
  end
end;
MessageDlgQuestion.png
DontBeSillly.png

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 { Déclaré dans 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


{ Déclaré dans 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
   );


{ Déclaré dans 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;

This page has been imported from the epikwiki version.