Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Delphi standard confirmation dialog but with checkbox "Don't ask me again"?

In many confirmation dialogs it is usefull to have such option (quick wayt to disable confirmation). But i can't find how to do that. I don't want to design it myself because i need this dialog to be standard-like and don't wont to redesign with every update of Delphi. Is there simple way to use Delphi standard confirmation dialog with such checkbox ?

UPDATE2. Suggested SynTaskDialog library from Synopse project does great job (all i need and even more), i will use it in my projects. Thanks!

UPDATE. So, thank you guys for ideas. System function MessageBoxCheck is nice solution but seem to be not so stable as it should be. In general i agree that it is good idea to use latest API functions to provide users with best UI experience of modern os and use old-fashioned design for older systems. At moment i stay on simple solution (code is following), but if someone share the code with support of UI for modern OS, it will be nice.

function MsgDlgWithCB(const Msg,Title,CBMsg: string; DlgType: TMsgDlgType;
  Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn;
  var cbDontAskAnymore: TCheckBox): TForm;
var
  i: integer;
  b: TButton;
  y: integer;
begin
  Result := CreateMessageDialog(Msg, DlgType, Buttons, DefaultButton) ;
  Result.Position := poScreenCenter;
  cbDontAskAnymore := TCheckBox.Create(Result);
  cbDontAskAnymore.Caption := CBMsg;
  cbDontAskAnymore.Width := 130;
  y := -1;
  for i := 0 to result.ComponentCount-1 do
    if result.Components[i] is TButton then
    begin
      b := TButton(result.Components[i]);
      b.Left := b.Left + cbDontAskAnymore.Width + 16;
      Result.ClientWidth := Max(Result.ClientWidth, b.Left+b.Width+16);
      y := b.Top+b.Height-cbDontAskAnymore.Height;
    end;
  if y<0 then
    y := Result.ClientHeight - cbDontAskAnymore.height - 16;
  Result.Caption := Title;
  cbDontAskAnymore.Parent := Result;
  cbDontAskAnymore.Top := y;
  cbDontAskAnymore.Left := 8;
end;

function MessageDlgCheckbox(const Msg: string; DlgType: TMsgDlgType;
  Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn;
  var cbDontAskAnymore: Boolean;
  const Title: string ='Confirmation';
  const CBMsg: string = 'Don''t ask anymore'): integer;
var
  f: TForm;
  c: TCheckbox;
begin
  f := MsgDlgWithCB(Msg,Title,CBMsg,DlgType,Buttons,DefaultButton,c);
  try
    result := f.ShowModal;
    cbDontAskAnymore := c.Checked;
  finally
    f.free;
  end;
end;
like image 813
Andrei Galatyn Avatar asked Aug 22 '13 11:08

Andrei Galatyn


2 Answers

You can use our Open Source SynTaskDialog unit.

Windows provides a generic task dialog available since Vista/Seven. But there is none available with previous versions of Windows, i.e. Windows XP or 2K.

This unit (licensed under a MPL/GPL/LGPL tri-license) will use the new TaskDialog API under Vista/Seven, and emulate it with pure Delphi code and standard themed VCL components under XP or 2K. It supports Delphi 6 up to XE4, and is Win32/Win64 Unicode ready.

Here is the result under a Windows Seven 64 bit computer:

enter image description here

And here is the same dialog created from our emulated pure Delphi code:

enter image description here

Since this screenshot was made on a Win 7 machine, the styling is native for that OS. When the emulated version of the dialog runs on XP it displays in a style native to that OS.

You have your "Do not ask for this setting next time" checkbox... and potentially much more!

like image 97
Arnaud Bouchez Avatar answered Nov 01 '22 16:11

Arnaud Bouchez


The system native functionality that offers such facilities is the task dialog API introduced in Vista. This provides means for you to show much more capable dialogs than the older MessageBox API.

Should you need to support XP then you will have to create your own dialog. For example by deriving from TForm and calling ShowModal. If you do this, make the form capable of building itself dynamically. Don't make one form per message that you show!

In my codebase, I have my own wrapper of the task dialog API. This detects at runtime versions of Windows that do not support task dialog and falls back on a custom built Delphi dialog.

Regarding SHMessageBoxCheck I'd be a little wary of taking a dependency on that. According to its documentation it's not supported beyond XP, and you have to import it by ordinal. I'd personally be worried that it might be dropped from a future version of Windows. That said, MS has a strong track record of doing whatever it takes to keep legacy apps working with new OS releases.

like image 32
David Heffernan Avatar answered Nov 01 '22 16:11

David Heffernan