Salut

Windows fournit une API appelée TaskDialogIndirect qui permet de creer des boites de dialogues style Vista personnalisables a souhait. (voir http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx et http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx). Malheureusement, la fonction demande beaucoup de parametres.

L'API permet de creer des boites de dialogues pouvant contenir :
- des CommandLink ou des boutons personnalisables,
- des RadioButtons,
- une CheckBox (genre "Ne plus afficher ce message"),
- un bouton Expand,
- une barre de progression
- des hyperliens ...

De plus, elle permet de gerer des évenements qui sont appelés automatiquement par la boite de dialogue. (propriété OnCallBack du TTaskDialog)

J'ai donc créé une unité qui permet de faciliter grandement la création de ces boites de dialogues Vista. Voila son code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
unit VistaTaskDialog;
 
interface
 
uses Windows;
 
const
  TD_ICON_BLANK = 0;
  TD_ICON_WARNING = 84;
  TD_ICON_QUESTION = 99;
  TD_ICON_ERROR = 98;
  TD_ICON_INFORMATION = 81;
 
  TD_ICON_SHIELD = 65532;
  TD_ICON_SHIELD_QUESTION = 104;
  TD_ICON_SHIELD_ERROR = 105;
  TD_ICON_SHIELD_OK = 106;
  TD_ICON_SHIELD_WARNING = 107;
  TD_ICON_SHIELD_HEADB = 65531;
  TD_ICON_SHIELD_HEAD_WARNING = 65530;
  TD_ICON_SHIELD_HEAD_ERROR = 65529;
  TD_ICON_SHIELD_HEAD_OK = 65528;
  TD_ICON_SHIELD_HEADG = 65527;    
 
  TD_ICON_SOUND_WARNING = 65535;
  TD_ICON_SOUND_ERROR   = 65534;
  TD_ICON_SOUND_INFORMATION = 65533;
 
  TD_BUTTON_OK     = $01;
  TD_BUTTON_YES    = $02;
  TD_BUTTON_NO     = $04;
  TD_BUTTON_CANCEL = $08;
  TD_BUTTON_RETRY  = $10;
  TD_BUTTON_CLOSE  = $20;
 
  TD_RESULT_OK     = 1;
  TD_RESULT_CANCEL = 2;
  TD_RESULT_RETRY  = 4;
  TD_RESULT_YES    = 6;
  TD_RESULT_NO     = 7;
  TD_RESULT_CLOSE  = 8;
 
  TD_RESULT_ERROR = -1;
 
  TDF_ENABLE_HYPERLINKS           =  $0001;
  TDF_USE_HICON_MAIN              =  $0002;
  TDF_USE_HICON_FOOTER            =  $0004;
  TDF_ALLOW_DIALOG_CANCELLATION   =  $0008;
  TDF_USE_COMMAND_LINKS           =  $0010;
  TDF_USE_COMMAND_LINKS_NO_ICON   =  $0020;
  TDF_EXPAND_FOOTER_AREA          =  $0040;
  TDF_EXPANDED_BY_DEFAULT         =  $0080;
  TDF_VERIFICATION_FLAG_CHECKED   =  $0100;
  TDF_SHOW_PROGRESS_BAR           =  $0200;
  TDF_SHOW_MARQUEE_PROGRESS_BAR   =  $0400;
  TDF_CALLBACK_TIMER              =  $0800;
  TDF_POSITION_RELATIVE_TO_WINDOW =  $1000;
  TDF_RTL_LAYOUT                  =  $2000;
  TDF_NO_DEFAULT_RADIO_BUTTON     =  $4000;
  TDF_CAN_BE_MINIMIZED            =  $8000;
 
type
  TTaskDialogCallBackProc = function (AHWND : HWND; Notification : Cardinal; WParam : wParam; LParam : lParam; dwRefData : Pointer) : integer;
 
  TTaskIcon = record
    case boolean of
      false : (hIcon : HICON);
      true  : (pszIcon : PWideChar);
    end;
 
  TTaskDialogButton = record
    nButtonID     : Integer;
    pszButtonText : PWideChar;
  end;
 
  TArrayTaskDialogButton = array of TTaskDialogButton;
 
  TTaskButtons = record
    NbButtons : Cardinal;
    CustomButtons : TArrayTaskDialogButton;
    DefaultButton : integer;
  end;        
 
  TTaskDialogConfig = record
    cbSize : Cardinal;
    HwndParent : HWND;
    hInstance : THandle;
    dwFlags : Cardinal;
    dwCommonButtons : Cardinal;
 
    pszWindowTitle : PWideChar;
    MainIcon : TTaskIcon;
    pszMainInstruction : PWideChar;
    pszContent : PWideChar;
 
    Buttons : TTaskButtons;
 
    RadioButtons : TTaskButtons;
 
    pszVerificationText : PWideChar;
 
    pszExpandedInformation : PWideChar;
    pszExpandedControlText : PWideChar;
    pszCollapsedControlText : PWideChar;
 
    FooterIcon : TTaskIcon;
    pszFooter : PWideChar;
 
    pfCallback : ^TTaskDialogCallBackProc;
    lpCallBackData : Pointer;
 
    cxWidth : Cardinal;
  end;
 
  PTaskDialogConfig = ^TTaskDialogConfig;
 
 
  TButtonTextID = record
    nButtonID     : Integer;
    pszButtonText : WideString;
  end;
 
  TTaskDialog = class
  private
    FArrayButtons, FArrayRadioButtons : array of TButtonTextID;
    FDefaultButton, FDefaultRadioButton : integer;
 
    FWindowTitle : WideString;
    FMainInstruction : WideString;
    FContent : WideString;
 
    FVerificationText : WideString;
    FExpandedInformation : WideString;
    FExpandedControlText : WideString;
    FCollapsedControlText : WideString;
    FFooterText : WideString;
 
    FMainIcon, FFooterIcon : TTaskIcon;
 
    FHandleParent : HWND;
    FInstance : THandle;
 
    FFlags : Cardinal;
    FCommonButtons : Cardinal;
 
    FCallback : TTaskDialogCallBackProc;
    FCallBackData : Pointer;
 
    FWidth : Cardinal;
  public
    constructor Create;
    destructor Destroy; override;
 
    function Execute (out Button, RadioButton : integer; out VerifChecked : boolean) : integer;
    procedure AddCustomButton (ButtonID : integer; ButtonText : WideString);
    procedure AddRadioButton (ButtonID : integer; ButtonText : WideString);
    procedure InitializeConfig;
    function HyperLink(ExecutableName, Text : Widestring) : WideString;
 
    property HandleParent : HWND read FHandleParent write FHandleParent;
    property hInstance : THandle read FInstance write FInstance;
 
    property WindowTitle     : WideString read FWindowTitle     write FWindowTitle;
    property MainInstruction : WideString read FMainInstruction write FMainInstruction;
    property Content         : WideString read FContent         write FContent;
 
    property VerificationText     : WideString read FVerificationText     write FVerificationText;
    property ExpandedInformation  : WideString read FExpandedInformation  write FExpandedInformation;
    property ExpandedControlText  : WideString read FExpandedControlText  write FExpandedControlText;
    property CollapsedControlText : WideString read FCollapsedControlText write FCollapsedControlText;
    property FooterText           : WideString read FFooterText           write FFooterText;
 
    property DefaultButton      : integer read FDefaultButton      write FDefaultButton;
    property DefaultRadioButton : integer read FDefaultRadioButton write FDefaultRadioButton;
 
    property MainIcon   : TTaskIcon read FMainIcon   write FMainIcon;
    property FooterIcon : TTaskIcon read FFooterIcon write FFooterIcon;
 
    property Flags         : Cardinal read FFlags         write FFlags;
    property CommonButtons : Cardinal read FCommonButtons write FCommonButtons;
    property Width         : Cardinal read FWidth         write FWidth;
 
    property CallBackData : Pointer read FCallBackData write FCallBackData;
    property OnCallBack : TTaskDialogCallBackProc read FCallBack write FCallBack;
  end;
 
 
implementation
 
const
  TaskDialogIndirectSig = 'TaskDialogIndirect';
 
function IsWindowsVista: Boolean;
var
  VerInfo: TOSVersioninfo;
begin
  VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  GetVersionEx(VerInfo);
  Result := VerInfo.dwMajorVersion >= 6;
end;
 
constructor TTaskDialog.Create;
begin
  inherited;
 
  InitializeConfig;
end;
 
 
destructor TTaskDialog.Destroy;
begin       
  InitializeConfig;
 
  inherited;
end;
 
procedure TTaskDialog.InitializeConfig;
var i : integer;
begin
  for i:=0 to Length(FArrayButtons)-1 do
    FArrayButtons[i].pszButtonText:='';
 
  for i:=0 to Length(FArrayRadioButtons)-1 do
    FArrayRadioButtons[i].pszButtonText:='';
 
  SetLength(FArrayButtons,0);
  SetLength(FArrayRadioButtons,0);
  FDefaultButton:=0;
  FDefaultRadioButton:=0;
 
  FWindowTitle:= '';;
  FMainInstruction := '';;
  FContent :='';
 
  FVerificationText:='';
  FExpandedInformation:='';
  FExpandedControlText:='';
  FCollapsedControlText:='';
 
  FMainIcon.hIcon:=0;
  FFooterIcon.hIcon:=0;
 
  FHandleParent := 0;
  FInstance := 0;
 
  FFlags := 0;
  FCommonButtons := 0;
  FWidth :=0;
end;
 
 
 
procedure TTaskDialog.AddCustomButton (ButtonID : integer; ButtonText : WideString);
var L : integer;
begin
  L:=Length(FArrayButtons);
  SetLength(FArrayButtons,L+1);
  FArrayButtons[L].pszButtonText:=ButtonText;
  FArrayButtons[L].nButtonID:=ButtonID;
end;
 
procedure TTaskDialog.AddRadioButton (ButtonID : integer; ButtonText : WideString);
var L : integer;
begin
  L:=Length(FArrayRadioButtons);
  SetLength(FArrayRadioButtons,L+1);
  FArrayRadioButtons[L].pszButtonText:=ButtonText;
  FArrayRadioButtons[L].nButtonID:=ButtonID;
end;
 
function TTaskDialog.HyperLink(ExecutableName, Text : Widestring) : WideString;
begin
  Result:='<A HREF="'+ExecutableName+'">'+Text+'</A>';
end;
 
function TTaskDialog.Execute (out Button, RadioButton : integer; out VerifChecked : boolean) : integer;
var AButtons, ARadioButtons : TTaskButtons;
    i : integer;
    FConfig : TTaskDialogConfig;
    FDLLHandle : THandle;
    FTaskDialogIndirect : function(Config: PTaskDialogConfig; pnButton, pnRadioButton : PInteger;
                                   VerificationFlagChecked : PBoolean): integer; cdecl stdcall;
begin
  Result:=TD_RESULT_ERROR;
  if IsWindowsVista then
  begin
    FDLLHandle := LoadLibrary(comctl32);
    if FDLLHandle >= 32 then
    begin
      @FTaskDialogIndirect := GetProcAddress(FDLLHandle, TaskDialogIndirectSig);
 
      if Assigned(FTaskDialogIndirect) then
      begin
        FConfig.HwndParent:=FHandleParent;
        FConfig.hInstance:=FInstance;
        FConfig.dwFlags:=FFlags;
        FConfig.dwCommonButtons:=FCommonButtons;
        FConfig.pszWindowTitle:=@FWindowTitle[1];
        FConfig.MainIcon:=FMainIcon;
        FConfig.pszMainInstruction:=@FMainInstruction[1];
        FConfig.pszContent:=@FContent[1];
 
        //création du tableau de boutons
        AButtons.DefaultButton:=FDefaultButton;
        AButtons.NbButtons:=Length(FArrayButtons);
        SetLength(AButtons.CustomButtons,AButtons.NbButtons);
        for i:=0 to AButtons.NbButtons-1 do
        begin
          AButtons.CustomButtons[i].pszButtonText:=@FArrayButtons[i].pszbuttonText[1];
          AButtons.CustomButtons[i].nButtonID:=FArrayButtons[i].nButtonID;
        end;
        FConfig.Buttons:=AButtons;
 
        //création du tableau de radiobuttons
        ARadioButtons.DefaultButton:=FDefaultRadioButton;
        ARadioButtons.NbButtons:=Length(FArrayRadioButtons);
        SetLength(ARadioButtons.CustomButtons,AButtons.NbButtons);
        for i:=0 to ARadioButtons.NbButtons-1 do
        begin
          ARadioButtons.CustomButtons[i].pszButtonText:=@FArrayRadioButtons[i].pszbuttonText[1];
          ARadioButtons.CustomButtons[i].nButtonID:=FArrayRadioButtons[i].nButtonID;
        end;
        FConfig.RadioButtons:=ARadioButtons;
 
        FConfig.pszVerificationText:=@FVerificationText[1];
        FConfig.pszExpandedInformation:=@FExpandedInformation[1];
        FConfig.pszExpandedControlText:=@FExpandedControlText[1];
        FConfig.pszCollapsedControlText:=@FCollapsedControlText[1];
        FConfig.FooterIcon:=FFooterIcon;
        FConfig.pszFooter:=@FFooterText[1];
        FConfig.pfCallback:=@FCallBack;
        FConfig.lpCallBackData:=FCallBackData;
        FConfig.cxWidth:=FWidth;
        FConfig.cbSize:=SizeOf(FConfig);
 
        Result:=FTaskDialogIndirect(@FConfig, @Button, @RadioButton, @VerifChecked);
 
        SetLength(AButtons.CustomButtons,0);
        SetLength(ARadioButtons.CustomButtons,0);
      end;
    end;
    FreeLibrary(FDLLHandle);
  end;
end;
 
end.
Quelques exemples d'utilisation :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var TaskD : TTaskDialog;
    Resultat, Bouton, RadioBouton : integer;
    Checked : boolean;
    MainIc, FootIc : TTaskIcon;
 
...
 
  TaskD:=TTaskDialog.Create;
  TaskD.HandleParent:=Handle;
  TaskD.WindowTitle:='Boite de dialogue complexe';
  TaskD.MainInstruction:='Instruction principale';
  TaskD.Content:='Contenu';
  TaskD.FooterText:='FooterText';
  TaskD.CommonButtons:=TD_BUTTON_OK or TD_BUTTON_RETRY;
  TaskD.CollapsedControlText:='CollapsedControlText';
  TaskD.ExpandedControlText:='ExpandedControlText';
  TaskD.ExpandedInformation:='ExpandedInformation';
  TaskD.AddCustomButton(8,'Custom Button 1');
  TaskD.AddCustomButton(9,'Custom Button 2');
  TaskD.AddRadioButton(1,'RadioButton 1');
  TaskD.AddRadioButton(2,'RadioButton 2');
 
  TaskD.Flags:=TDF_USE_COMMAND_LINKS or TDF_SHOW_PROGRESS_BAR or TDF_EXPAND_FOOTER_AREA or TDF_EXPANDED_BY_DEFAULT;
 
  MainIc.hIcon:=TD_ICON_SHIELD_QUESTION;
  TaskD.MainIcon:=MainIc;
  FootIc.hIcon:=TD_ICON_WARNING;
  TaskD.FooterIcon:=FootIc;
 
  Resultat:=TaskD.Execute(Bouton,RadioBouton,Checked);
 
  TaskD.Free;
donne :



---------------------------------------------------------------

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var TaskD : TTaskDialog;
    Resultat, Bouton, RadioBouton : integer;
    Checked : boolean;
    MainIc : TTaskIcon;
 
...
 
  TaskD:=TTaskDialog.Create;
  TaskD.HandleParent:=Handle;
  TaskD.WindowTitle:='Modification du document';
  TaskD.MainInstruction:='Voulez vous sauvegarder les changements ?';
  TaskD.Content:='Le contenu du document a été modifié. Quitter sans sauvegarder annulera les modifications apportées a ce document depuis la derniere sauvegarde';
  TaskD.AddCustomButton(1,'Enregistrer les changements'#10'Enregistre les changements et quitte le logiciel');
  TaskD.AddCustomButton(3,'Ne pas enregistrer'#10'Quitte le logiciel sans enregistrer');
  TaskD.AddCustomButton(TD_BUTTON_CANCEL,'Annuler'#10'Retourner sur le logiciel');
  TaskD.DefaultButton:=TD_BUTTON_CANCEL;
  MainIc.hIcon:=TD_ICON_QUESTION;
 
  TaskD.MainIcon:=MainIc;
  TaskD.Flags:=TDF_USE_COMMAND_LINKS or TDF_ALLOW_DIALOG_CANCELLATION;
 
  Resultat:=TaskD.Execute(Bouton,RadioBouton,Checked);
 
  case Resultat of
    S_OK             : begin //Execution OK
                         case Bouton of
                           1                : ShowMessage('Enregistre et quitte');
                           3                : ShowMessage('Quitte sans enregistrer');
                           TD_BUTTON_CANCEL : ShowMessage('Annule');
                         end;
                       end;
    TD_RESULT_ERROR  : begin   //erreur si pas Vista
                         ShowMessage('Vous n''avez pas Vista !');
                       end;
    E_OUTOFMEMORY    : ShowMessage('Erreur mémoire');
    E_INVALIDARG     : ShowMessage('Arguments invalides');
    E_FAIL           : ShowMessage('Erreur inconnue');
  end;
  TaskD.Free;
donne :






Liste des constantes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//Icones classiques
  TD_ICON_BLANK = 0;
  TD_ICON_WARNING = 84;
  TD_ICON_QUESTION = 99;
  TD_ICON_ERROR = 98;
  TD_ICON_INFORMATION = 81;
 
//Icones avec un bouclier
  TD_ICON_SHIELD = 65532;
  TD_ICON_SHIELD_QUESTION = 104;
  TD_ICON_SHIELD_ERROR = 105;
  TD_ICON_SHIELD_OK = 106;
  TD_ICON_SHIELD_WARNING = 107;
 
//Icones avec un bouclier et un bandeau de couleur (le bandeau ne marche pas pour le FooterIcon)
  TD_ICON_SHIELD_HEADB = 65531;
  TD_ICON_SHIELD_HEAD_WARNING = 65530;
  TD_ICON_SHIELD_HEAD_ERROR = 65529;
  TD_ICON_SHIELD_HEAD_OK = 65528;
  TD_ICON_SHIELD_HEADG = 65527;    
 
//Icones qui permettent a la fenetre de produire un bruit lors de l'apparition
  TD_ICON_SOUND_WARNING = 65535;
  TD_ICON_SOUND_ERROR   = 65534;
  TD_ICON_SOUND_INFORMATION = 65533;
 
//Boutons classiques (CommonButtons)
  TD_BUTTON_OK     = $01;
  TD_BUTTON_YES    = $02;
  TD_BUTTON_NO     = $04;
  TD_BUTTON_CANCEL = $08;
  TD_BUTTON_RETRY  = $10;
  TD_BUTTON_CLOSE  = $20;
 
//Valeur de retour des boutons classiques
  TD_RESULT_OK     = 1;
  TD_RESULT_CANCEL = 2;
  TD_RESULT_RETRY  = 4;
  TD_RESULT_YES    = 6;
  TD_RESULT_NO     = 7;
  TD_RESULT_CLOSE  = 8;
 
  TD_RESULT_ERROR = -1;
 
//Flags permettant de personnaliser la boite de dialogue
  TDF_ENABLE_HYPERLINKS           =  $0001;
  TDF_USE_HICON_MAIN              =  $0002;
  TDF_USE_HICON_FOOTER            =  $0004;
  TDF_ALLOW_DIALOG_CANCELLATION   =  $0008;
  TDF_USE_COMMAND_LINKS           =  $0010;
  TDF_USE_COMMAND_LINKS_NO_ICON   =  $0020;
  TDF_EXPAND_FOOTER_AREA          =  $0040;
  TDF_EXPANDED_BY_DEFAULT         =  $0080;
  TDF_VERIFICATION_FLAG_CHECKED   =  $0100;
  TDF_SHOW_PROGRESS_BAR           =  $0200;
  TDF_SHOW_MARQUEE_PROGRESS_BAR   =  $0400;
  TDF_CALLBACK_TIMER              =  $0800;
  TDF_POSITION_RELATIVE_TO_WINDOW =  $1000;
  TDF_RTL_LAYOUT                  =  $2000;
  TDF_NO_DEFAULT_RADIO_BUTTON     =  $4000;
  TDF_CAN_BE_MINIMIZED            =  $8000;
Note :
  • Ajoutez obligatoirement un XPManifest sur votre form
  • Si vous ne spécifiez pas de HandleParent, votre boite de dialogue se comportera comme si elle n'était pas modale.
  • Pour écrire en petit dans un bouton de type CommandLink, ajoutez un #10. (le #13 permet de mettre le caption du bouton sur plusieurs lignes)


Minimum supported client : Windows Vista
Minimum supported server : Windows Server 2008

Voila, j'espere que j'ai été clair ^^

Si vous avez des questions ou des modifications a apporter, faites moi signe !

A+

Mick605