/// <summary>
 /// The required implementation of CommonDialog that shows the Task Dialog.
 /// </summary>
 /// <param name="hwndOwner">Owner window. This can be null.</param>
 /// <returns>If this method returns true, then ShowDialog will return DialogResult.OK.
 /// If this method returns false, then ShowDialog will return DialogResult.Cancel. The
 /// user of this class must use the TaskDialogResult member to get more information.
 /// </returns>
 protected override bool RunDialog(IntPtr hwndOwner)
 {
     _taskDialogResult = _taskDialog.Show(hwndOwner, TaskConfig, out _verificationFlagCheckedResult);
     return(_taskDialogResult != (int)DialogResult.Cancel);
 }
Пример #2
0
        private static TaskDialogResult ShowTaskDialog(TaskDialogOptions options)
        {
            var td = new NativeTaskDialog();

            td.WindowTitle         = options.Title;
            td.MainInstruction     = options.MainInstruction;
            td.Content             = options.Content;
            td.ExpandedInformation = options.ExpandedInfo;
            td.Footer = options.FooterText;

            bool hasCustomCancel = false;

            // Use of Command Links overrides any custom defined buttons
            if (options.CommandLinks != null && options.CommandLinks.Length > 0)
            {
                List <TaskDialogButton> lst = new List <TaskDialogButton>();
                for (int i = 0; i < options.CommandLinks.Length; i++)
                {
                    try
                    {
                        TaskDialogButton button = new TaskDialogButton();
                        button.ButtonId   = GetButtonIdForCommandButton(i);
                        button.ButtonText = options.CommandLinks[i];
                        lst.Add(button);
                    }
                    catch (FormatException)
                    {
                    }
                }
                td.Buttons = lst.ToArray();
                if (options.DefaultButtonIndex.HasValue &&
                    options.DefaultButtonIndex >= 0 &&
                    options.DefaultButtonIndex.Value < td.Buttons.Length)
                {
                    td.DefaultButton = td.Buttons[options.DefaultButtonIndex.Value].ButtonId;
                }
            }
            else if (options.CustomButtons != null && options.CustomButtons.Length > 0)
            {
                List <TaskDialogButton> lst = new List <TaskDialogButton>();
                for (int i = 0; i < options.CustomButtons.Length; i++)
                {
                    try
                    {
                        TaskDialogButton button = new TaskDialogButton();
                        button.ButtonId   = GetButtonIdForCustomButton(i);
                        button.ButtonText = options.CustomButtons[i];

                        if (!hasCustomCancel)
                        {
                            hasCustomCancel =
                                (button.ButtonText == TaskDialogOptions.LocalizedStrings.CommonButton_Close ||
                                 button.ButtonText == TaskDialogOptions.LocalizedStrings.CommonButton_Cancel);
                        }

                        lst.Add(button);
                    }
                    catch (FormatException)
                    {
                    }
                }

                td.Buttons = lst.ToArray();
                if (options.DefaultButtonIndex.HasValue &&
                    options.DefaultButtonIndex.Value >= 0 &&
                    options.DefaultButtonIndex.Value < td.Buttons.Length)
                {
                    td.DefaultButton = td.Buttons[options.DefaultButtonIndex.Value].ButtonId;
                }
                td.CommonButtons = TaskDialogCommonButtons.None;
            }

            if (options.RadioButtons != null && options.RadioButtons.Length > 0)
            {
                List <TaskDialogButton> lst = new List <TaskDialogButton>();
                for (int i = 0; i < options.RadioButtons.Length; i++)
                {
                    try
                    {
                        TaskDialogButton button = new TaskDialogButton();
                        button.ButtonId   = GetButtonIdForRadioButton(i);
                        button.ButtonText = options.RadioButtons[i];
                        lst.Add(button);
                    }
                    catch (FormatException)
                    {
                    }
                }
                td.RadioButtons         = lst.ToArray();
                td.NoDefaultRadioButton = (!options.DefaultButtonIndex.HasValue || options.DefaultButtonIndex.Value == -1);
                if (options.DefaultButtonIndex.HasValue &&
                    options.DefaultButtonIndex >= 0 &&
                    options.DefaultButtonIndex.Value < td.RadioButtons.Length)
                {
                    td.DefaultButton = td.RadioButtons[options.DefaultButtonIndex.Value].ButtonId;
                }
            }

            if (options.CommonButtons != TaskDialogCommonButtons.None)
            {
                td.CommonButtons = options.CommonButtons;

                if (options.DefaultButtonIndex.HasValue &&
                    options.DefaultButtonIndex >= 0)
                {
                    td.DefaultButton = GetButtonIdForCommonButton(options.CommonButtons, options.DefaultButtonIndex.Value);
                }
            }

            td.MainIcon                = options.MainIcon;
            td.CustomMainIcon          = options.CustomMainIcon;
            td.FooterIcon              = options.FooterIcon;
            td.CustomFooterIcon        = options.CustomFooterIcon;
            td.EnableHyperlinks        = DetectHyperlinks(options.Content, options.ExpandedInfo, options.FooterText);
            td.AllowDialogCancellation =
                (options.AllowDialogCancellation ||
                 hasCustomCancel ||
                 options.CommonButtons.HasFlag(TaskDialogCommonButtons.Close) ||
                 options.CommonButtons.HasFlag(TaskDialogCommonButtons.Cancel));
            td.CallbackTimer            = options.EnableCallbackTimer;
            td.ExpandedByDefault        = options.ExpandedByDefault;
            td.ExpandFooterArea         = options.ExpandToFooter;
            td.PositionRelativeToWindow = true;
            td.RightToLeftLayout        = false;
            td.NoDefaultRadioButton     = false;
            td.CanBeMinimized           = false;
            td.ShowProgressBar          = options.ShowProgressBar;
            td.ShowMarqueeProgressBar   = options.ShowMarqueeProgressBar;
            td.UseCommandLinks          = (options.CommandLinks != null && options.CommandLinks.Length > 0);
            td.UseCommandLinksNoIcon    = false;
            td.VerificationText         = options.VerificationText;
            td.VerificationFlagChecked  = options.VerificationByDefault;
            td.ExpandedControlText      = "Hide details";
            td.CollapsedControlText     = "Show details";
            td.Callback     = options.Callback;
            td.CallbackData = options.CallbackData;
            td.Config       = options;

            TaskDialogResult result;
            int diagResult = 0;
            TaskDialogSimpleResult simpResult = TaskDialogSimpleResult.None;
            bool verificationChecked          = false;
            int  radioButtonResult            = -1;
            int? commandButtonResult          = null;
            int? customButtonResult           = null;

            diagResult = td.Show(options.Owner, out verificationChecked, out radioButtonResult);

            if (radioButtonResult >= RadioButtonIDOffset)
            {
                simpResult         = (TaskDialogSimpleResult)diagResult;
                radioButtonResult -= RadioButtonIDOffset;
            }

            if (diagResult >= CommandButtonIDOffset)
            {
                simpResult          = TaskDialogSimpleResult.Command;
                commandButtonResult = diagResult - CommandButtonIDOffset;
            }
            else if (diagResult >= CustomButtonIDOffset)
            {
                simpResult         = TaskDialogSimpleResult.Custom;
                customButtonResult = diagResult - CustomButtonIDOffset;
            }
            else
            {
                simpResult = (TaskDialogSimpleResult)diagResult;
            }

            result = new TaskDialogResult(
                simpResult,
                (String.IsNullOrEmpty(options.VerificationText) ? null : (bool?)verificationChecked),
                ((options.RadioButtons == null || options.RadioButtons.Length == 0) ? null : (int?)radioButtonResult),
                ((options.CommandLinks == null || options.CommandLinks.Length == 0) ? null : commandButtonResult),
                ((options.CustomButtons == null || options.CustomButtons.Length == 0) ? null : customButtonResult));

            return(result);
        }