Beispiel #1
0
        private IUICommand ShowTaskDialog()
        {
            NativeMethods.TASKDIALOGCONFIG tdc = new NativeMethods.TASKDIALOGCONFIG();
            tdc.cbSize             = Marshal.SizeOf(tdc);
            tdc.hwndParent         = NativeMethods.GetForegroundWindow();
            tdc.pszWindowTitle     = " ";
            tdc.pszMainInstruction = Title;
            tdc.dwFlags            = NativeMethods.TASKDIALOG_FLAGS.SIZE_TO_CONTENT;
            tdc.pszContent         = Content;
            if (Commands.Count == 0)
            {
                tdc.dwCommonButtons = NativeMethods.TASKDIALOG_COMMON_BUTTON_FLAGS.CLOSE_BUTTON;
            }
            else
            {
                if (CancelCommandIndex != uint.MaxValue)
                {
                    tdc.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.ALLOW_DIALOG_CANCELLATION;
                }
            }

            IntPtr bptr = Marshal.AllocHGlobal(Commands.Count * 8);

            for (int ibut = 0; ibut < Commands.Count; ibut++)
            {
                var but = new NativeMethods.TASKDIALOG_BUTTON {
                    nButtonID = ibut + 11, pszButtonText = Commands[ibut].Label
                };
                Marshal.StructureToPtr(but, IntPtr.Add(bptr, ibut * 8), false);
            }

            if (DefaultCommandIndex != uint.MaxValue)
            {
                tdc.nDefaultButton = DefaultCommandIndex + 11;
            }

            tdc.pButtons = bptr;
            tdc.cButtons = Commands.Count;
            int ibtn;
            int hresult = NativeMethods.TaskDialogIndirect(ref tdc, out ibtn, IntPtr.Zero, IntPtr.Zero);

            Marshal.FreeHGlobal(bptr);

            if (ibtn > 10)
            {
                return(Commands[ibtn - 11]);
            }
            if (ibtn == 2 && CancelCommandIndex != uint.MaxValue)
            {
                return(Commands[(int)CancelCommandIndex]);
            }

            return(null);
        }
        private IUICommand ShowTaskDialog()
        {
            NativeMethods.TASKDIALOGCONFIG tdc = new NativeMethods.TASKDIALOGCONFIG();
            tdc.cbSize = Marshal.SizeOf(tdc);
            tdc.hwndParent = NativeMethods.GetForegroundWindow();
            tdc.pszWindowTitle = " ";
            tdc.pszMainInstruction = Title;
            tdc.dwFlags = NativeMethods.TASKDIALOG_FLAGS.SIZE_TO_CONTENT;
            tdc.pszContent = Content;
            if (this.Commands.Count == 0)
            {
                tdc.dwCommonButtons = NativeMethods.TASKDIALOG_COMMON_BUTTON_FLAGS.CLOSE_BUTTON;
            }
            else
            {
                if (CancelCommandIndex != uint.MaxValue)
                {
                    tdc.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.ALLOW_DIALOG_CANCELLATION;
                }
            }

            IntPtr bptr = Marshal.AllocHGlobal(Commands.Count * 8);
            for(int ibut = 0; ibut < Commands.Count; ibut++)
            {
                var but = new NativeMethods.TASKDIALOG_BUTTON { nButtonID = ibut+11, pszButtonText = Commands[ibut].Label };
                Marshal.StructureToPtr(but, IntPtr.Add(bptr, ibut * 8), false);
            }

            if (DefaultCommandIndex != uint.MaxValue)
            {
                tdc.nDefaultButton = DefaultCommandIndex + 11;
            }

            tdc.pButtons = bptr;
            tdc.cButtons = Commands.Count;
            int ibtn;
            int hresult = NativeMethods.TaskDialogIndirect(ref tdc, out ibtn, IntPtr.Zero, IntPtr.Zero);
            Marshal.FreeHGlobal(bptr);

            if(ibtn > 10)
            {
                return Commands[ibtn - 11];
            }
            if(ibtn == 2 && CancelCommandIndex != uint.MaxValue)
            {
                return Commands[(int)CancelCommandIndex];
            }

            return null;
        }
Beispiel #3
0
 private void ApplyTextConfiguration(NativeMethods.TASKDIALOGCONFIG dialogConfig)
 {
     // Set important text properties - note that nulls or empty strings
     // are fine here
     // TODO: Rationalize default handling - do we even need to set defaults first?
     dialogConfig.pszContent              = content;
     dialogConfig.pszWindowTitle          = caption;
     dialogConfig.pszMainInstruction      = instruction;
     dialogConfig.pszExpandedInformation  = expandedText;
     dialogConfig.pszExpandedControlText  = expandedControlText;
     dialogConfig.pszCollapsedControlText = collapsedControlText;
     dialogConfig.pszFooter           = footerText;
     dialogConfig.pszVerificationText = checkBoxText;
 }
Beispiel #4
0
        private void ApplyOptionConfiguration(NativeMethods.TASKDIALOGCONFIG dialogConfig)
        {
            // Handle options - start with no options set
            NativeMethods.TASKDIALOG_FLAGS options = NativeMethods.TASKDIALOG_FLAGS.NONE;
            if (cancelable)
            {
                options |= NativeMethods.TASKDIALOG_FLAGS.TDF_ALLOW_DIALOG_CANCELLATION;
            }
            if (checkBoxChecked.HasValue && checkBoxChecked.Value)
            {
                options |= NativeMethods.TASKDIALOG_FLAGS.TDF_VERIFICATION_FLAG_CHECKED;
            }
            if (hyperlinksEnabled)
            {
                options |= NativeMethods.TASKDIALOG_FLAGS.TDF_ENABLE_HYPERLINKS;
            }
            if (expanded)
            {
                options |= NativeMethods.TASKDIALOG_FLAGS.TDF_EXPANDED_BY_DEFAULT;
            }
            if (Tick != null)
            {
                options |= NativeMethods.TASKDIALOG_FLAGS.TDF_CALLBACK_TIMER;
            }
            if (startupLocation == TaskDialogStartupLocation.CenterOwner)
            {
                options |= NativeMethods.TASKDIALOG_FLAGS.TDF_POSITION_RELATIVE_TO_WINDOW;
            }

            // Note: no validation required, as we allow this to be set even if there
            // is no expanded information text because that could be added later.
            // Default for Win32 API is to expand into (and after) the content area.
            if (expansionMode == TaskDialogExpandedInformationLocation.ExpandFooter)
            {
                options |= NativeMethods.TASKDIALOG_FLAGS.TDF_EXPAND_FOOTER_AREA;
            }

            // Finally, apply options to config
            dialogConfig.dwFlags = options;
        }
Beispiel #5
0
        private void ApplyGeneralNativeConfiguration(NativeMethods.TASKDIALOGCONFIG dialogConfig)
        {
            // If an owner wasn't specifically specified, we'll use the app's main window
            Window currentOwner = ownerWindow;

            if (currentOwner == null)
            {
                currentOwner = Helpers.GetDefaultOwnerWindow();
            }

            if (currentOwner != null)
            {
                dialogConfig.hwndParent = (new WindowInteropHelper(currentOwner)).Handle;
            }

            // Other miscellaneous sets
            dialogConfig.MainIcon =
                new NativeMethods.TASKDIALOGCONFIG_ICON_UNION((int)mainIcon);
            dialogConfig.FooterIcon =
                new NativeMethods.TASKDIALOGCONFIG_ICON_UNION((int)footerIcon);
            dialogConfig.dwCommonButtons =
                (NativeMethods.TASKDIALOG_COMMON_BUTTON_FLAGS)standardButtons;
        }
Beispiel #6
0
        public TaskDialogButton ShowModal()
        {
            var config = new NativeMethods.TASKDIALOGCONFIG();

            config.cbSize        = (uint)Marshal.SizeOf(typeof(NativeMethods.TASKDIALOGCONFIG));
            config.pButtons      = IntPtr.Zero;
            config.pRadioButtons = IntPtr.Zero;

            var uiShell = (IVsUIShell)_provider.GetService(typeof(SVsUIShell));

            if (uiShell == null)
            {
                // We are shutting down, so return the default
                return(SelectedButton);
            }
            uiShell.GetDialogOwnerHwnd(out config.hwndParent);
            uiShell.EnableModeless(0);

            var customButtons = new List <TaskDialogButton>();

            config.dwCommonButtons = 0;

            foreach (var button in Buttons)
            {
                var flag = GetButtonFlag(button);
                if (flag != 0)
                {
                    config.dwCommonButtons |= flag;
                }
                else
                {
                    customButtons.Add(button);
                }
            }

            try {
                if (customButtons.Any())
                {
                    config.cButtons = (uint)customButtons.Count;
                    var ptr = config.pButtons = Marshal.AllocHGlobal(customButtons.Count * Marshal.SizeOf(typeof(NativeMethods.TASKDIALOG_BUTTON)));
                    for (int i = 0; i < customButtons.Count; ++i)
                    {
                        NativeMethods.TASKDIALOG_BUTTON data;
                        data.nButtonID = GetButtonId(null, null, i);
                        if (string.IsNullOrEmpty(customButtons[i].Subtext))
                        {
                            data.pszButtonText = customButtons[i].Text;
                        }
                        else
                        {
                            data.pszButtonText = string.Format("{0}\n{1}", customButtons[i].Text, customButtons[i].Subtext);
                        }
                        Marshal.StructureToPtr(data, ptr + i * Marshal.SizeOf(typeof(NativeMethods.TASKDIALOG_BUTTON)), false);
                    }
                }
                else
                {
                    config.cButtons = 0;
                    config.pButtons = IntPtr.Zero;
                }

                if (_buttons.Any() && SelectedButton != null)
                {
                    config.nDefaultButton = GetButtonId(SelectedButton, customButtons);
                }
                else
                {
                    config.nDefaultButton = 0;
                }

                if (_radioButtons.Any())
                {
                    config.cRadioButtons = (uint)_radioButtons.Count;
                    var ptr = config.pRadioButtons = Marshal.AllocHGlobal(_radioButtons.Count * Marshal.SizeOf(typeof(NativeMethods.TASKDIALOG_BUTTON)));
                    for (int i = 0; i < _radioButtons.Count; ++i)
                    {
                        NativeMethods.TASKDIALOG_BUTTON data;
                        data.nButtonID     = GetRadioId(null, null, i);
                        data.pszButtonText = _radioButtons[i].Text;
                        Marshal.StructureToPtr(data, ptr + i * Marshal.SizeOf(typeof(NativeMethods.TASKDIALOG_BUTTON)), false);
                    }

                    if (SelectedRadioButton != null)
                    {
                        config.nDefaultRadioButton = GetRadioId(SelectedRadioButton, _radioButtons);
                    }
                    else
                    {
                        config.nDefaultRadioButton = 0;
                    }
                }

                config.pszWindowTitle          = Title;
                config.pszMainInstruction      = MainInstruction;
                config.pszContent              = Content;
                config.pszExpandedInformation  = ExpandedInformation;
                config.pszExpandedControlText  = ExpandedControlText;
                config.pszCollapsedControlText = CollapsedControlText;
                config.pszFooter           = Footer;
                config.pszVerificationText = VerificationText;
                config.pfCallback          = Callback;
                config.hMainIcon           = (IntPtr)GetIconResource(MainIcon);
                config.hFooterIcon         = (IntPtr)GetIconResource(FooterIcon);

                if (Width.HasValue)
                {
                    config.cxWidth = (uint)Width.Value;
                }
                else
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_SIZE_TO_CONTENT;
                }
                if (EnableHyperlinks)
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_ENABLE_HYPERLINKS;
                }
                if (AllowCancellation)
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_ALLOW_DIALOG_CANCELLATION;
                }
                if (UseCommandLinks && config.cButtons > 0)
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_USE_COMMAND_LINKS;
                }
                if (!ShowExpandedInformationInContent)
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_EXPAND_FOOTER_AREA;
                }
                if (ExpandedByDefault)
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_EXPANDED_BY_DEFAULT;
                }
                if (SelectedVerified)
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_VERIFICATION_FLAG_CHECKED;
                }
                if (CanMinimize)
                {
                    config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_CAN_BE_MINIMIZED;
                }

                config.dwFlags |= NativeMethods.TASKDIALOG_FLAGS.TDF_POSITION_RELATIVE_TO_WINDOW;

                int  selectedButton, selectedRadioButton;
                bool verified;
                ErrorHandler.ThrowOnFailure(NativeMethods.TaskDialogIndirect(
                                                ref config,
                                                out selectedButton,
                                                out selectedRadioButton,
                                                out verified
                                                ));

                SelectedButton      = GetButton(selectedButton, customButtons);
                SelectedRadioButton = GetRadio(selectedRadioButton, _radioButtons);
                SelectedVerified    = verified;
            } finally {
                uiShell.EnableModeless(1);

                if (config.pButtons != IntPtr.Zero)
                {
                    for (int i = 0; i < customButtons.Count; ++i)
                    {
                        Marshal.DestroyStructure(config.pButtons + i * Marshal.SizeOf(typeof(NativeMethods.TASKDIALOG_BUTTON)), typeof(NativeMethods.TASKDIALOG_BUTTON));
                    }
                    Marshal.FreeHGlobal(config.pButtons);
                }
                if (config.pRadioButtons != IntPtr.Zero)
                {
                    for (int i = 0; i < _radioButtons.Count; ++i)
                    {
                        Marshal.DestroyStructure(config.pRadioButtons + i * Marshal.SizeOf(typeof(NativeMethods.TASKDIALOG_BUTTON)), typeof(NativeMethods.TASKDIALOG_BUTTON));
                    }
                    Marshal.FreeHGlobal(config.pRadioButtons);
                }
            }

            return(SelectedButton);
        }
Beispiel #7
0
 internal static extern HRESULT TaskDialogIndirect(
     [In] NativeMethods.TASKDIALOGCONFIG pTaskConfig,
     [Out] out int pnButton,
     [Out] out int pnRadioButton,
     [MarshalAs(UnmanagedType.Bool)][Out] out bool pVerificationFlagChecked);