/// <summary>
        /// Shows the TaskDialog.
        /// </summary>
        /// <returns>TaskDialogResult.</returns>
        public TaskDialogResult Show()
        {
            result = TaskDialogResult.Cancel;
            TaskDialogConfiguration nativeConfig = new TaskDialogConfiguration();

            nativeConfig.size            = (uint)Marshal.SizeOf(nativeConfig);
            nativeConfig.parentHandle    = hWndOwner;
            nativeConfig.commonButtons   = TaskDialogResult.Cancel;
            nativeConfig.content         = text;
            nativeConfig.windowTitle     = caption;
            nativeConfig.mainInstruction = instructionText;
            nativeConfig.taskDialogFlags = TaskDialogOptions.AllowCancel | TaskDialogOptions.ShowProgressBar | TaskDialogOptions.PositionRelativeToWindow | TaskDialogOptions.EnableHyperlinks;
            nativeConfig.callback        = new TaskDialogCallback(DialogProc);

            // Show the dialog.
            // NOTE: this is a BLOCKING call; the dialog proc callbacks
            // will be executed by the same thread as the
            // Show() call before the thread of execution
            // continues to the end of this method.
            showState = TaskDialogShowState.Showing;
            using (new EnableThemingInScope(true)) {     // Here is the way we use "vanilla" P/Invoke to call TaskDialogIndirect().
                TaskDialogIndirect(
                    nativeConfig,
                    out selectedButtonId,
                    IntPtr.Zero,
                    IntPtr.Zero);
            }
            showState = TaskDialogShowState.Closed;

            // Build and return dialog result to public API - leaving it
            // null after an exception is thrown is fine in this case
            if ((TaskDialogCommonButtonReturnId)selectedButtonId == TaskDialogCommonButtonReturnId.Ok)
            {
                result = TaskDialogResult.Ok;
            }

            // Reset progress bar.
            ProgressBarState = TaskDialogProgressBarState.Normal;
            ProgressBarValue = progressBarMinimum;

            // Free up strings.
            if (updatedStrings != null)
            {
                for (int i = 0; i < updatedStrings.Length; i++)
                {
                    if (updatedStrings[i] != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(updatedStrings[i]);
                        updatedStrings[i] = IntPtr.Zero;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Close TaskDialog with a given TaskDialogResult
        /// </summary>
        /// <param name="result">TaskDialogResult to return from the Show() method</param>
        /// <exception cref="InvalidOperationException">if TaskDialog is not showing.</exception>
        public void Close(TaskDialogResult result)
        {
            if (!NativeDialogShowing)
            {
                throw new InvalidOperationException();
            }
            showState = TaskDialogShowState.Closing;
            int id = (int)TaskDialogCommonButtonReturnId.Cancel;

            if (result == TaskDialogResult.Ok)
            {
                id = (int)TaskDialogCommonButtonReturnId.Ok;
            }
            SendMessageHelper(TaskDialogMessage.ClickButton, id, 0);
        }
        /// <summary>
        /// Displays a TaskDialog-based error dialog with the error icon.
        /// </summary>
        /// <param name="title">The dialog's title.</param>
        /// <param name="text">The dialog's body.</param>
        /// <returns>TaskDialogResult.</returns>
        public TaskDialogResult ShowError(string title, string text)
        {
            result = TaskDialogResult.No;
            TaskDialogConfiguration nativeConfig = new TaskDialogConfiguration();

            Icon = TaskDialogStandardIcon.Error;

            nativeConfig.size            = (uint)Marshal.SizeOf(nativeConfig);
            nativeConfig.parentHandle    = hWndOwner;
            nativeConfig.commonButtons   = TaskDialogResult.Ok;
            nativeConfig.content         = text;
            nativeConfig.windowTitle     = title;
            nativeConfig.mainInstruction = instructionText;
            nativeConfig.taskDialogFlags = TaskDialogOptions.AllowCancel | TaskDialogOptions.PositionRelativeToWindow;
            nativeConfig.callback        = new TaskDialogCallback(DialogProc);

            showState = TaskDialogShowState.Showing;
            using (new EnableThemingInScope(true)) {     // Here is the way we use "vanilla" P/Invoke to call TaskDialogIndirect().
                TaskDialogIndirect(
                    nativeConfig,
                    out selectedButtonId,
                    IntPtr.Zero,
                    IntPtr.Zero);
            }
            showState = TaskDialogShowState.Closed;

            if ((TaskDialogCommonButtonReturnId)selectedButtonId == TaskDialogCommonButtonReturnId.Ok)
            {
                result = TaskDialogResult.Ok;
            }

            // Free up strings.
            if (updatedStrings != null)
            {
                for (int i = 0; i < updatedStrings.Length; i++)
                {
                    if (updatedStrings[i] != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(updatedStrings[i]);
                        updatedStrings[i] = IntPtr.Zero;
                    }
                }
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Displays a TaskDialog-based error dialog with the error icon.
        /// </summary>
        /// <param name="title">The dialog's title.</param>
        /// <param name="text">The dialog's body.</param>
        /// <returns>TaskDialogResult.</returns>
        public TaskDialogResult ShowError( string title, string text )
        {
            result = TaskDialogResult.No;
            TaskDialogConfiguration nativeConfig = new TaskDialogConfiguration();
            Icon = TaskDialogStandardIcon.Error;

            nativeConfig.size = (uint) Marshal.SizeOf( nativeConfig );
            nativeConfig.parentHandle = hWndOwner;
            nativeConfig.commonButtons = TaskDialogResult.Ok;
            nativeConfig.content = text;
            nativeConfig.windowTitle = title;
            nativeConfig.mainInstruction = instructionText;
            nativeConfig.taskDialogFlags = TaskDialogOptions.AllowCancel | TaskDialogOptions.PositionRelativeToWindow;
            nativeConfig.callback = new TaskDialogCallback( DialogProc );

            showState = TaskDialogShowState.Showing;
            using ( new EnableThemingInScope( true ) ) { // Here is the way we use "vanilla" P/Invoke to call TaskDialogIndirect().
                TaskDialogIndirect(
                    nativeConfig,
                    out selectedButtonId,
                    IntPtr.Zero,
                    IntPtr.Zero );
            }
            showState = TaskDialogShowState.Closed;

            if ( (TaskDialogCommonButtonReturnId) selectedButtonId == TaskDialogCommonButtonReturnId.Ok ) {
                result = TaskDialogResult.Ok;
            }

            // Free up strings.
            if ( updatedStrings != null ) {
                for ( int i = 0; i < updatedStrings.Length; i++ ) {
                    if ( updatedStrings[i] != IntPtr.Zero ) {
                        Marshal.FreeHGlobal( updatedStrings[i] );
                        updatedStrings[i] = IntPtr.Zero;
                    }
                }
            }

            return result;
        }
Beispiel #5
0
        /// <summary>
        /// Shows the TaskDialog.
        /// </summary>
        /// <returns>TaskDialogResult.</returns>
        public TaskDialogResult Show()
        {
            result = TaskDialogResult.Cancel;
            TaskDialogConfiguration nativeConfig = new TaskDialogConfiguration();

            nativeConfig.size = (uint) Marshal.SizeOf( nativeConfig );
            nativeConfig.parentHandle = hWndOwner;
            nativeConfig.commonButtons = TaskDialogResult.Cancel;
            nativeConfig.content = text;
            nativeConfig.windowTitle = caption;
            nativeConfig.mainInstruction = instructionText;
            nativeConfig.taskDialogFlags = TaskDialogOptions.AllowCancel | TaskDialogOptions.ShowProgressBar | TaskDialogOptions.PositionRelativeToWindow | TaskDialogOptions.EnableHyperlinks;
            nativeConfig.callback = new TaskDialogCallback( DialogProc );

            // Show the dialog.
            // NOTE: this is a BLOCKING call; the dialog proc callbacks
            // will be executed by the same thread as the
            // Show() call before the thread of execution
            // continues to the end of this method.
            showState = TaskDialogShowState.Showing;
            using ( new EnableThemingInScope( true ) ) { // Here is the way we use "vanilla" P/Invoke to call TaskDialogIndirect().
                TaskDialogIndirect(
                    nativeConfig,
                    out selectedButtonId,
                    IntPtr.Zero,
                    IntPtr.Zero );
            }
            showState = TaskDialogShowState.Closed;

            // Build and return dialog result to public API - leaving it
            // null after an exception is thrown is fine in this case
            if ( (TaskDialogCommonButtonReturnId) selectedButtonId == TaskDialogCommonButtonReturnId.Ok ) {
                result = TaskDialogResult.Ok;
            }

            // Reset progress bar.
            ProgressBarState = TaskDialogProgressBarState.Normal;
            ProgressBarValue = progressBarMinimum;

            // Free up strings.
            if ( updatedStrings != null ) {
                for ( int i = 0; i < updatedStrings.Length; i++ ) {
                    if ( updatedStrings[i] != IntPtr.Zero ) {
                        Marshal.FreeHGlobal( updatedStrings[i] );
                        updatedStrings[i] = IntPtr.Zero;
                    }
                }
            }

            return result;
        }
Beispiel #6
0
 /// <summary>
 /// Close TaskDialog with a given TaskDialogResult
 /// </summary>
 /// <param name="result">TaskDialogResult to return from the Show() method</param>
 /// <exception cref="InvalidOperationException">if TaskDialog is not showing.</exception>
 public void Close( TaskDialogResult result )
 {
     if ( !NativeDialogShowing ) throw new InvalidOperationException();
     showState = TaskDialogShowState.Closing;
     int id = (int) TaskDialogCommonButtonReturnId.Cancel;
     if ( result == TaskDialogResult.Ok ) id = (int) TaskDialogCommonButtonReturnId.Ok;
     SendMessageHelper( TaskDialogMessage.ClickButton, id, 0 );
 }