private static IntPtr AllocateAndMarshalButtons(TaskDialogNativeMethods.TaskDialogButton[] structs)
        {
            IntPtr initialPtr = Marshal.AllocHGlobal(
                Marshal.SizeOf(typeof(TaskDialogNativeMethods.TaskDialogButton)) * structs.Length);

            IntPtr currentPtr = initialPtr;
            foreach (TaskDialogNativeMethods.TaskDialogButton button in structs)
            {
                Marshal.StructureToPtr(button, currentPtr, false);
                currentPtr = (IntPtr)((int)currentPtr + Marshal.SizeOf(button));
            }

            return initialPtr;
        }
        internal void NativeShow()
        {
            // Applies config struct and other settings, then
            // calls main Win32 function.
            if (settings == null)
            {
                throw new InvalidOperationException(LocalizedMessages.NativeTaskDialogConfigurationError);
            }

            // Do a last-minute parse of the various dialog control lists,
            // and only allocate the memory at the last minute.

            MarshalDialogControlStructs();

            // Make the call and show the dialog.
            // NOTE: this call is BLOCKING, though the thread
            // WILL re-enter via the DialogProc.
            try
            {
                ShowState = DialogShowState.Showing;

                int  selectedButtonId;
                int  selectedRadioButtonId;
                bool checkBoxChecked;

                // Here is the way we use "vanilla" P/Invoke to call TaskDialogIndirect().
                HResult hresult = TaskDialogNativeMethods.TaskDialogIndirect(
                    nativeDialogConfig,
                    out selectedButtonId,
                    out selectedRadioButtonId,
                    out checkBoxChecked);

                if (CoreErrorHelper.Failed(hresult))
                {
                    string msg;
                    switch (hresult)
                    {
                    case HResult.InvalidArguments:
                        msg = LocalizedMessages.NativeTaskDialogInternalErrorArgs;
                        break;

                    case HResult.OutOfMemory:
                        msg = LocalizedMessages.NativeTaskDialogInternalErrorComplex;
                        break;

                    default:
                        msg = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                            LocalizedMessages.NativeTaskDialogInternalErrorUnexpected,
                                            hresult);
                        break;
                    }
                    Exception e = Marshal.GetExceptionForHR((int)hresult);
                    throw new Win32Exception(msg, e);
                }

                SelectedButtonId      = selectedButtonId;
                SelectedRadioButtonId = selectedRadioButtonId;
                CheckBoxChecked       = checkBoxChecked;
            }
            catch (EntryPointNotFoundException exc)
            {
                throw new NotSupportedException(LocalizedMessages.NativeTaskDialogVersionError, exc);
            }
            finally
            {
                ShowState = DialogShowState.Closed;
            }
        }
        // Allocates a new string on the unmanaged heap, 
        // and stores the pointer so we can free it later.

        private IntPtr MakeNewString(string text, TaskDialogNativeMethods.TaskDialogElements element)
        {
            IntPtr newStringPtr = Marshal.StringToHGlobalUni(text);
            updatedStrings[(int)element] = newStringPtr;
            return newStringPtr;
        }
 // Checks to see if the given element already has an 
 // updated string, and if so, 
 // frees it. This is done in preparation for a call to 
 // MakeNewString(), to prevent
 // leaks from multiple updates calls on the same element 
 // within a single native dialog lifetime.
 private void FreeOldString(TaskDialogNativeMethods.TaskDialogElements element)
 {
     int elementIndex = (int)element;
     if (updatedStrings[elementIndex] != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(updatedStrings[elementIndex]);
         updatedStrings[elementIndex] = IntPtr.Zero;
     }
 }
        private int SendMessageHelper(TaskDialogNativeMethods.TaskDialogMessages message, int wparam, long lparam)
        {
            // Be sure to at least assert here - 
            // messages to invalid handles often just disappear silently
            Debug.Assert(hWndDialog != null, "HWND for dialog is null during SendMessage");

            return (int)CoreNativeMethods.SendMessage(
                hWndDialog,
                (uint)message,
                (IntPtr)wparam,
                new IntPtr(lparam));
        }
 private bool IsOptionSet(TaskDialogNativeMethods.TaskDialogOptions flag)
 {
     return ((nativeDialogConfig.taskDialogFlags & flag) == flag);
 }
 private void UpdateIconCore(TaskDialogStandardIcon icon, TaskDialogNativeMethods.TaskDialogIconElement element)
 {
     AssertCurrentlyShowing();
     SendMessageHelper(
         TaskDialogNativeMethods.TaskDialogMessages.UpdateIcon,
         (int)element,
         (long)icon);
 }
        private void UpdateTextCore(string s, TaskDialogNativeMethods.TaskDialogElements element)
        {
            AssertCurrentlyShowing();

            FreeOldString(element);
            SendMessageHelper(
                TaskDialogNativeMethods.TaskDialogMessages.SetElementText,
                (int)element,
                (long)MakeNewString(s, element));
        }
        private void ApplyOptionConfiguration(TaskDialogNativeMethods.TaskDialogConfiguration dialogConfig)
        {
            // Handle options - start with no options set.
            TaskDialogNativeMethods.TaskDialogOptions options = TaskDialogNativeMethods.TaskDialogOptions.None;
            if (cancelable)
            {
                options |= TaskDialogNativeMethods.TaskDialogOptions.AllowCancel;
            }
            if (footerCheckBoxChecked.HasValue && footerCheckBoxChecked.Value)
            {
                options |= TaskDialogNativeMethods.TaskDialogOptions.CheckVerificationFlag;
            }
            if (hyperlinksEnabled)
            {
                options |= TaskDialogNativeMethods.TaskDialogOptions.EnableHyperlinks;
            }
            if (detailsExpanded)
            {
                options |= TaskDialogNativeMethods.TaskDialogOptions.ExpandedByDefault;
            }
            if (Tick != null)
            {
                options |= TaskDialogNativeMethods.TaskDialogOptions.UseCallbackTimer;
            }
            if (startupLocation == TaskDialogStartupLocation.CenterOwner)
            {
                options |= TaskDialogNativeMethods.TaskDialogOptions.PositionRelativeToWindow;
            }

            // 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 == TaskDialogExpandedDetailsLocation.ExpandFooter)
            {
                options |= TaskDialogNativeMethods.TaskDialogOptions.ExpandFooterArea;
            }

            // Finally, apply options to config.
            dialogConfig.taskDialogFlags = options;
        }
Example #10
0
 /// <summary>
 /// Sets important text properties.
 /// </summary>
 /// <param name="dialogConfig">An instance of a <see cref="TaskDialogNativeMethods.TaskDialogConfiguration"/> object.</param>
 private void ApplyTextConfiguration(TaskDialogNativeMethods.TaskDialogConfiguration dialogConfig)
 {
     // note that nulls or empty strings are fine here.
     dialogConfig.content = text;
     dialogConfig.windowTitle = caption;
     dialogConfig.mainInstruction = instructionText;
     dialogConfig.expandedInformation = detailsExpandedText;
     dialogConfig.expandedControlText = detailsExpandedLabel;
     dialogConfig.collapsedControlText = detailsCollapsedLabel;
     dialogConfig.footerText = footerText;
     dialogConfig.verificationText = checkBoxText;
 }
Example #11
0
        private void ApplyGeneralNativeConfiguration(TaskDialogNativeMethods.TaskDialogConfiguration dialogConfig)
        {
            // If an owner wasn't specifically specified, 
            // we'll use the app's main window.
            if (ownerWindow != IntPtr.Zero)
            {
                dialogConfig.parentHandle = ownerWindow;
            }

            // Other miscellaneous sets.
            dialogConfig.mainIcon = new TaskDialogNativeMethods.IconUnion((int)icon);
            dialogConfig.footerIcon = new TaskDialogNativeMethods.IconUnion((int)footerIcon);
            dialogConfig.commonButtons = (TaskDialogNativeMethods.TaskDialogCommonButtons)standardButtons;
        }