Author: Jonas Bilinkevicius
I was just wondering if there is a way, either with Windows API or Delphi's VCL, to
get the 'Don't ask again' checkbox in a dialog box, other than creating one from
scratch.
Answer:
You have to create a form, this is not a stock windows dialog. Take a look at the
following unit, especially the MessageDlgWithNoMorebox function.
1 {
2 MyDialogs: Collects modified dialog functions
3 Author: Dr. Peter Below
4 Description: Version 1.01 created 4 Juli 2001, added translation of button captions.
5 Last modified: 4 Juli 2001
6 }
7
8 unit MyDialogs;
9
10 interface
11
12 uses
13 Dialogs;
14
15 function DefMessageDlg(const aCaption: string; const Msg: string; DlgType:
16 TMsgDlgType;
17 Buttons: TMsgDlgButtons; DefButton: Integer; HelpCtx: Longint): Integer;
18
19 function MessageDlgWithNoMorebox(const aCaption: string; const Msg: string;
20 DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; DefButton: Integer;
21 HelpCtx: Longint; var askNoMore: Boolean): Integer;
22
23 implementation
24
25 uses
26 Windows, Classes, Controls, stdctrls, sysutils, forms;
27
28 const {Copied from Dialogs}
29 ModalResults: array[TMsgDlgBtn] of Integer = (mrYes, mrNo, mrOk, mrCancel,
30 mrAbort,
31 mrRetry, mrIgnore, mrAll, mrNoToAll, mrYesToAll, 0);
32
33 var {Filled during unit initialization}
34 ButtonCaptions: array[TMsgDlgBtn] of string;
35
36 {Convert a modal result to a TMsgDlgBtn code}
37
38 function ModalResultToBtn(res: TModalResult): TMsgDlgBtn;
39 begin
40 for Result := Low(Result) to High(Result) do
41 begin
42 if ModalResults[Result] = res then
43 exit;
44 end;
45 Result := mbHelp; {to remove warning only }
46 Assert(False, 'ModalResultToBtn: unknown modalresult ' + IntToStr(res));
47 end;
48
49 {When the button captions on the message form are translated the button size and as
50 a
51 consequence the button positions need to be adjusted.}
52
53 procedure AdjustButtons(aForm: TForm);
54 var
55 buttons: TList;
56 btnWidth: Integer;
57 btnGap: Integer;
58
59 procedure CollectButtons;
60 var
61 i: Integer;
62 begin
63 for i := 0 to aForm.Controlcount - 1 do
64 if aForm.Controls[i] is TButton then
65 buttons.Add(aForm.Controls[i]);
66 end;
67
68 procedure MeasureButtons;
69 var
70 i: Integer;
71 textrect: TRect;
72 w: Integer;
73 begin
74 btnWidth := TButton(buttons[0]).Width;
75 aForm.Canvas.Font := aForm.Font;
76 for i := 0 to buttons.count - 1 do
77 begin
78 TextRect := Rect(0, 0, 0, 0);
79 Windows.DrawText(aform.canvas.handle, PChar(TButton(buttons[i]).Caption), -1,
80 TextRect,
81 DT_CALCRECT or DT_LEFT or DT_SINGLELINE);
82 with TextRect do
83 w := Right - Left + 16;
84 if w > btnWidth then
85 btnWidth := w;
86 end;
87 if buttons.count > 1 then
88 btnGap := TButton(buttons[1]).Left - TButton(buttons[0]).Left -
89 TButton(buttons[0]).Width
90 else
91 btnGap := 0;
92 end;
93
94 procedure SizeButtons;
95 var
96 i: Integer;
97 begin
98 for i := 0 to buttons.count - 1 do
99 TButton(buttons[i]).Width := btnWidth;
100 end;
101
102 procedure ArrangeButtons;
103 var
104 i: Integer;
105 total, left: Integer;
106 begin
107 total := (buttons.count - 1) * btnGap;
108 for i := 0 to buttons.count - 1 do
109 Inc(total, TButton(buttons[i]).Width);
110 left := (aForm.ClientWidth - total) div 2;
111 if left < 0 then
112 begin
113 aForm.Width := aForm.Width + 2 * Abs(left) + 16;
114 left := 8;
115 end;
116 for i := 0 to buttons.count - 1 do
117 begin
118 TButton(buttons[i]).Left := left;
119 Inc(left, btnWidth + btnGap);
120 end;
121 end;
122
123 begin
124 buttons := TList.Create;
125 try
126 CollectButtons;
127 if buttons.Count = 0 then
128 exit;
129 MeasureButtons;
130 SizeButtons;
131 ArrangeButtons;
132 finally
133 buttons.Free;
134 end;
135 end;
136
137 procedure InitMsgForm(aForm: TForm; const aCaption: string;
138 helpCtx: LongInt; DefButton: Integer);
139 var
140 i: Integer;
141 btn: TButton;
142 begin
143 with aForm do
144 begin
145 if Length(aCaption) > 0 then
146 Caption := aCaption;
147 HelpContext := HelpCtx;
148 for i := 0 to ComponentCount - 1 do
149 begin
150 if Components[i] is TButton then
151 begin
152 btn := TButton(Components[i]);
153 btn.default := btn.ModalResult = DefButton;
154 if btn.default then
155 ActiveControl := Btn;
156 {$IFNDEF STANDARDCAPTIONS}
157 btn.Caption := ButtonCaptions[ModalResultToBtn(btn.Modalresult)];
158 {$ENDIF}
159 end;
160 end;
161 {$IFNDEF STANDARDCAPTIONS}
162 AdjustButtons(aForm);
163 {$ENDIF}
164 end;
165 end;
166
167 {
168 DefMessageDlg:
169 Creates a MessageDlg with translated button captions and configurable default
170 button and caption
171
172 Parameters:
173 aCaption: Caption to use for the dialog. If empty the default is used.
174 Msg: Message to display
175 DlgType: Type of dialog, see MessageDlg online help
176 Buttons: Buttons to display, see MessageDlg online help
177 DefButton: ModalResult of the button that should be the default.
178 HelpCtx: Help context (optional)
179
180 Returns the ModalResult of the dialog
181
182 Created 07.06.1998 by P. Below, modified 04.07.2001
183 }
184
185 function DefMessageDlg(const aCaption: string; const Msg: string; DlgType:
186 TMsgDlgType;
187 Buttons: TMsgDlgButtons; DefButton: Integer; HelpCtx: Longint): Integer;
188 var
189 aForm: TForm;
190 begin
191 aForm := CreateMessageDialog(Msg, DlgType, Buttons);
192 try
193 InitMsgForm(aForm, aCaption, helpCtx, DefButton);
194 Result := aForm.ShowModal;
195 finally
196 aForm.Free;
197 end;
198 end;
199
200 resourcestring
201 {$IFDEF GERMAN}
202 AskNoMoreCaption = 'Diesen Dialog nicht mehr anzeigen';
203 {$ELSE}
204 AskNoMoreCaption = 'Don''t show this dialog again';
205 {$ENDIF}
206
207 {
208 MessageDlgWithNoMorebox:
209 Creates a MessageDlg with translated button captions and configurable
210 default button and caption
211
212 Parameters:
213 aCaption: Caption to use for the dialog. If empty the default is used.
214 Msg: Message to display
215 DlgType: Type of dialog, see MessageDlg online help
216 Buttons: Buttons to display, see MessageDlg online help
217 DefButton: ModalResult of the button that should be the default.
218 HelpCtx: Help context (optional)
219 askNoMore: If this is passed in as True the function will directly return
220 the DefButton result.
221 Otherwise a checkbox is shown beneath the buttons which the user can check to
222 not have this dialog show up in the future. Its checked state is returned in
223 the parameter.
224
225 Returns the ModalResult of the dialog
226
227 Created 4.7.2001 by P. Below
228 }
229
230 function MessageDlgWithNoMorebox(const aCaption: string; const Msg: string; DlgType:
231 TMsgDlgType;
232 Buttons: TMsgDlgButtons; DefButton: Integer; HelpCtx: Longint; var askNoMore:
233 Boolean): Integer;
234 var
235 aForm: TForm;
236 chk: TCheckbox;
237 begin
238 if askNoMore then
239 Result := DefButton
240 else
241 begin
242 aForm := CreateMessageDialog(Msg, DlgType, Buttons);
243 try
244 InitMsgForm(aForm, aCaption, helpCtx, DefButton);
245 chk := TCheckbox.Create(aForm);
246 chk.Parent := aForm;
247 chk.SetBounds(16, aForm.ClientHeight, aForm.Clientwidth - 32, chk.Height);
248 chk.Checked := False;
249 chk.Caption := AskNoMoreCaption;
250 AForm.Height := aForm.Height + chk.Height + 8;
251 Result := aForm.ShowModal;
252 askNoMore := chk.Checked;
253 finally
254 aForm.Free;
255 end;
256 end;
257 end;
258
259 resourcestring
260 {$IFDEF GERMAN}
261 cmbYes = '&Ja';
262 cmbNo = '&Nein';
263 cmbOK = 'OK';
264 cmbCancel = 'Abbrechen';
265 cmbHelp = '&Hilfe';
266 cmbAbort = '&Abbrechen';
267 cmbRetry = '&Wiederholen';
268 cmbIgnore = '&Ignorieren';
269 cmbAll = '&Alle';
270 cmbNoToAll = 'N&ein für alle';
271 cmbYesToAll = 'Ja für &alle';
272 {$ELSE}
273 cmbYes = '&Yes';
274 cmbNo = '&No';
275 cmbOK = 'OK';
276 cmbCancel = 'Cancel';
277 cmbHelp = '&Help';
278 cmbAbort = '&Abort';
279 cmbRetry = '&Retry';
280 cmbIgnore = '&Ignore';
281 cmbAll = '&All';
282 cmbNoToAll = 'N&o to All';
283 cmbYesToAll = 'Yes to &All';
284 {$ENDIF}
285
286 procedure InitButtonCaptions;
287 begin
288 ButtonCaptions[mbYes] := cmbYes;
289 ButtonCaptions[mbNo] := cmbNo;
290 ButtonCaptions[mbOK] := cmbOK;
291 ButtonCaptions[mbCancel] := cmbCancel;
292 ButtonCaptions[mbAbort] := cmbAbort;
293 ButtonCaptions[mbRetry] := cmbRetry;
294 ButtonCaptions[mbIgnore] := cmbIgnore;
295 ButtonCaptions[mbAll] := cmbAll;
296 ButtonCaptions[mbNoToAll] := cmbNoToAll;
297 ButtonCaptions[mbYesToAll] := cmbYesToAll;
298 ButtonCaptions[mbHelp] := cmbHelp;
299 end;
300
301 initialization
302 InitButtonCaptions;
303 end.
|