unit TDMessageBox;

interface

uses
  Windows, SysUtils, Types, Classes, UITypes, Dialogs, Forms;

type
  TTaskDialogHelper = class helper for TTaskDialog
    function ExecuteModally: Boolean; overload;
    function ExecuteModally(ParentWnd: HWND): Boolean; overload;
  end;

  TTaskDialogMsgBox = record
    class function MessageBox(const ACaption, ATitle, AText: string;
      AIcon: TTaskDialogIcon; AButtons: TTaskDialogCommonButtons): TModalResult; overload; static;
    class function MessageBox(const ACaption, ATitle, AText: string;
      AIcon: TTaskDialogIcon; AButtons: TTaskDialogCommonButtons;
      const AConfirmationText: string; var AConfirmationChecked: Boolean): TModalResult; overload; static;
    class procedure ShowMessage(const ATitle: string); static;
    class procedure ShowMessageFmt(const ATitle: string;
      const Args: array of const); static;
  end;

var
  TD: TTaskDialogMsgBox;

const
  tdiApplication = 130;

implementation

class function TTaskDialogMsgBox.MessageBox(const ACaption, ATitle, AText: string;
  AIcon: TTaskDialogIcon; AButtons: TTaskDialogCommonButtons): TModalResult;
var
  TD: TTaskDialog;
begin

    TD := TTaskDialog.Create(nil);
    try
      if ACaption.IsEmpty then
        TD.Caption := Application.Title
      else
        TD.Caption := ACaption;
      TD.Title := ATitle;
      TD.Text := AText;
      TD.MainIcon := AIcon;
      TD.CommonButtons := AButtons;
      TD.Flags := TD.Flags + [tfPositionRelativeToWindow];
      if AIcon = tdiApplication then
      begin
        TD.MainIcon := tdiNone;
        TD.CustomMainIcon := Application.Icon;
        TD.Flags := TD.Flags + [tfUseHiconMain];
      end;
      if TD.ExecuteModally then
        Result := TD.ModalResult
      else
        Result := mrNone;
    finally
      TD.Free;
    end;

end;

class function TTaskDialogMsgBox.MessageBox(const ACaption, ATitle,
  AText: string; AIcon: TTaskDialogIcon; AButtons: TTaskDialogCommonButtons;
  const AConfirmationText: string;
  var AConfirmationChecked: Boolean): TModalResult;
var
  TD: TTaskDialog;
begin

    TD := TTaskDialog.Create(nil);
    try
      if ACaption.IsEmpty then
        TD.Caption := Application.Title
      else
        TD.Caption := ACaption;
      TD.Title := ATitle;
      TD.Text := AText;
      TD.MainIcon := AIcon;
      TD.VerificationText := AConfirmationText;
      TD.CommonButtons := AButtons;
      TD.Flags := TD.Flags + [tfPositionRelativeToWindow];
      if AConfirmationChecked then
        TD.Flags := TD.Flags + [tfPositionRelativeToWindow, tfVerificationFlagChecked];
      if AIcon = tdiApplication then
      begin
        TD.MainIcon := tdiNone;
        TD.CustomMainIcon := Application.Icon;
        TD.Flags := TD.Flags + [tfUseHiconMain];
      end;
      if TD.ExecuteModally then
      begin
        Result := TD.ModalResult;
        AConfirmationChecked := tfVerificationFlagChecked in TD.Flags;
      end
      else
        Result := mrNone;
    finally
      TD.Free;
    end;

end;

class procedure TTaskDialogMsgBox.ShowMessage(const ATitle: string);
begin
  TD.MessageBox('', ATitle, '', tdiInformation, [tcbOk]);
end;

class procedure TTaskDialogMsgBox.ShowMessageFmt(const ATitle: string;
  const Args: array of const);
begin
  ShowMessage(Format(ATitle, Args));
end;

{ TTaskDialogHelper }

function TTaskDialogHelper.ExecuteModally: Boolean;
begin
  Application.ModalStarted;
  try
    Result := Execute;
  finally
    Application.ModalFinished;
  end;
end;

function TTaskDialogHelper.ExecuteModally(ParentWnd: HWND): Boolean;
begin
  Application.ModalStarted;
  try
    Result := Execute(ParentWnd);
  finally
    Application.ModalFinished;
  end;
end;

end.
