private void ShowErrorDialog(IWin32Window parent, string caption, string message, string details) { using (TraceSources.IapDesktop.TraceMethod().WithParameters(caption, message, details)) { var config = new UnsafeNativeMethods.TASKDIALOGCONFIG() { cbSize = (uint)Marshal.SizeOf(typeof(UnsafeNativeMethods.TASKDIALOGCONFIG)), hwndParent = parent.Handle, dwFlags = 0, dwCommonButtons = UnsafeNativeMethods.TASKDIALOG_COMMON_BUTTON_FLAGS.TDCBF_OK_BUTTON, pszWindowTitle = "An error occured", MainIcon = TaskDialogIcons.TD_ERROR_ICON, pszMainInstruction = caption, pszContent = message, pszExpandedInformation = details.ToString() }; UnsafeNativeMethods.TaskDialogIndirect( ref config, out int buttonPressed, out int radioButtonPressed, out bool verificationFlagPressed); } }
public int ShowOptionsTaskDialog( IWin32Window parent, IntPtr mainIcon, string windowTitle, string mainInstruction, string content, string details, IList <string> optionCaptions, string verificationText, out bool verificationFlagPressed) { // The options to show. var options = optionCaptions .Select(caption => Marshal.StringToHGlobalUni(caption)) .ToArray(); // Wrap each option by a TASKDIALOG_BUTTON_RAW structure and // marshal them one by one into a native memory buffer. var buttonsBuffer = Marshal.AllocHGlobal( Marshal.SizeOf <UnsafeNativeMethods.TASKDIALOG_BUTTON_RAW>() * options.Length); var currentButton = buttonsBuffer; for (int i = 0; i < options.Length; i++) { Marshal.StructureToPtr <UnsafeNativeMethods.TASKDIALOG_BUTTON_RAW>( new UnsafeNativeMethods.TASKDIALOG_BUTTON_RAW() { nButtonID = ButtonIdOffset + i, // Add offset to avoid conflict with IDOK/IDCANCEL. pszButtonText = options[i] }, currentButton, false); currentButton += Marshal.SizeOf <UnsafeNativeMethods.TASKDIALOG_BUTTON_RAW>(); } try { var config = new UnsafeNativeMethods.TASKDIALOGCONFIG() { cbSize = (uint)Marshal.SizeOf(typeof(UnsafeNativeMethods.TASKDIALOGCONFIG)), hwndParent = parent.Handle, dwFlags = UnsafeNativeMethods.TASKDIALOG_FLAGS.TDF_USE_COMMAND_LINKS, dwCommonButtons = UnsafeNativeMethods.TASKDIALOG_COMMON_BUTTON_FLAGS.TDCBF_OK_BUTTON | UnsafeNativeMethods.TASKDIALOG_COMMON_BUTTON_FLAGS.TDCBF_CANCEL_BUTTON, pszWindowTitle = windowTitle, MainIcon = mainIcon, pszMainInstruction = mainInstruction, pszContent = content, pButtons = buttonsBuffer, cButtons = (uint)options.Length, pszExpandedInformation = details, pszVerificationText = verificationText }; UnsafeNativeMethods.TaskDialogIndirect( ref config, out int buttonPressed, out int radioButtonPressed, out verificationFlagPressed); if (buttonPressed == UnsafeNativeMethods.IDOK) { // Pick first option. return(0); } else if (buttonPressed >= ButtonIdOffset) { // Option selected. return(buttonPressed - ButtonIdOffset); } else { throw new OperationCanceledException("Task dialog was cancelled"); } } finally { foreach (var option in options) { Marshal.FreeHGlobal(option); } Marshal.FreeHGlobal(buttonsBuffer); } }