示例#1
0
        /// <summary>
        /// Displays a task dialog with the given configuration options.
        /// </summary>
        /// <param name="options">
        /// A <see cref="T:TaskDialogInterop.TaskDialogOptions"/> that specifies the
        /// configuration options for the dialog.
        /// </param>
        /// <returns>
        /// A <see cref="T:TaskDialogInterop.TaskDialogResult"/> value that specifies
        /// which button is clicked by the user.
        /// </returns>
        public static TaskDialogResult Show(TaskDialogOptions options)
        {
            TaskDialogResult result = TaskDialogResult.Empty;

            // Make a copy since we'll let Showing event possibly modify them
            TaskDialogOptions configOptions = options;


            if (NativeTaskDialog.IsAvailableOnThisOS)
            {
                try
                {
                    OnShowing(new TaskDialogShowingEventArgs(ref configOptions));
                    result = ShowTaskDialog(configOptions);
                }
                catch (EntryPointNotFoundException)
                {
                    // HACK: Not available? Just crash. Need investigation.
                    // HACK: Removed every Emulated-Task-Dialog Thingy.
                    throw;
                    //// This can happen on some machines, usually when running Vista/7 x64
                    //// When it does, we'll work around the issue by forcing emulated mode
                    //// http://www.codeproject.com/Messages/3257715/How-to-get-it-to-work-on-Windows-7-64-bit.aspx
                    //ForceEmulationMode = true;
                    //result = ShowEmulatedTaskDialog(configOptions);
                }
                finally
                {
                    OnClosed(new TaskDialogClosedEventArgs(result));
                }
            }
            else
            {
                throw new Exception("Old System");
            }


            return(result);
        }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TaskDialogClosedEventArgs"/> class.
 /// </summary>
 /// <param name="result">The result of the TaskDialog.</param>
 internal TaskDialogClosedEventArgs(TaskDialogResult result)
 {
     Result = result;
 }
示例#3
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);
        }