#1 2013-10-23 19:04:08

MarkElder
Member
Registered: 2013-10-23
Posts: 1

Using SynTaskDialog

Hello,

I just started using SynTaskDialog.  Thank you very much for making this component available.  I'm posting here some of the items I discovered when trying to use it in my project. 

The other main thread on this topic (http://synopse.info/forum/viewtopic.php?id=253) was a little long and it was tough to tell what was old information and what was current. If I have some things wrong hopefully someone will correct me, otherwise I hope this is useful for others getting started with the object.

I wanted to create a simple Save Changes dialog with three buttons
[Save]  [Don't Save]  [Cancel]

Here is the code I ended up using, with comments on its use.

var 
  // There are two options to use, either TTaskDialog or TTaskDialogEx
  // TTaskDialog will set some options and pass others into the Execute call
  // TTaskDialog gives you access to all the options in the record so you can
  // call Execute with no params.
  // The object type switches between a record or object depending on your version of Delphi
  // but everything is static so there is no need to call Create when it is an object.
  // I am using the TTaskDialogEx because it was easier to see what I was setting
  // when playing with the different options.

  Task: TTaskDialogEx;
  TaskResult: integer;
begin
    // The code has comments that indicate the record will be initialized empty.
    // However, that only seems to be true for the string values in the record.
    // I am using Default(TTaskDialogEx) Per this StackOverflow question on
    // how to initialize records with defaults:
    // http://stackoverflow.com/questions/1106 … 5#11066205
    // Without this my records was starting with a width set to a very large number and 
    // the dialog ended up much wider than the screen.

    Task := Default(TTaskDialogEx);

    Task.Base.Title := 'Save Changes';
    Task.Base.Inst := 'Save Report Format Changes';
    Task.Base.Content := 'Save changes to ' + ExtractFileName(FileName) + ' before closing?';

    // Custom buttons are added separated by Line Breaks, The first button will be given an ID of 100,
    // the second of 101, etc.  These values are used when setting the defaults as well as checking
    // the return value to see what was selected.

    Task.Base.Buttons := 'Save' + sLineBreak + 'Don''t Save' + sLineBreak + 'Cancel';
    Task.DialogIcon := tiQuestion;

    // Set the Default button to "Save"

    Task.ButtonDef := 100;

    // I first tried adding the Common Cancel button like this:
    // Task.CommonButtons := [cbCancel];
    // However on the replacement dialog that is created when
    // running on Windows XP the Cancel button is added first before the "Save" and "Don't Save" buttons
    // buttons. On the Vista/Win7 native dialog it appears third.  Adding this as a custom button ensures
    // it always appears last.
    // To still let the user press escape when the dialog is up set the tdfAllowDialogCancellation flag.
    // This will only work on Vista or Higher.  The replacement dialog does not adjust itself for this flag.
    // For me that is OK since most users will not be pressing escape anyway and it will working
    // on new OSes.
    // If the user presses escape the return value will be a "2" indicating the common Cancel button,
    // but pressing the Cancel button in this example will return 103.

    Task.CommonButtons := [];
    Task.Flags := [tdfPositionRelativeToWindow, tdfAllowDialogCancellation];

    // There were some changes made to the replacement dialog.  From looking at the images in this thread
    // http://synopse.info/forum/viewtopic.php?id=253
    // I decided I like the Classic Style better.   Only makes a difference when running on Windows XP.

    Task.EmulateClassicStyle := true;

    TaskResult := Task.Execute(Self.Handle);
    if TaskResult = 100 then
    begin
      // Save
    end
    else if TaskResult = 101 then
    begin
      // Do not save, discard changes
    end
    else
    begin
      // nothing to do here.  User canceled so just leave everything as is.
    end;


If you are wondering what some of the options look like this page has some good examples showing the different options in the system dialogs. 
  http://specials.rejbrand.se/TTaskDialog/
The code there is specific to the built in Delphi TTaskDialog but you can easily see how they map here.  The advantage to using TSynTaskDialog is the fallback options for Windows XP are built-in, no extra coding required here.

Last edited by MarkElder (2013-10-23 19:22:49)

Offline

#2 2013-10-24 09:35:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Using SynTaskDialog

Instead of Task := Default(TTaskDialogEx), you can also use Fillchar(Task,sizeof(Task),0) will work with any version of Delphi.

Thanks for sharing your experiment!

Offline

Board footer

Powered by FluxBB