예제 #1
0
 // The new task dialog does not support the existing Win32 functions for closing (e.g. EndDialog()); instead,
 // a "click button" message is sent. In this case, we're abstracting out to say that the TaskDialog consumer can
 // simply call "Close" and we'll "click" the cancel button. Note that the cancel button doesn't actually
 // have to exist for this to work.
 internal void NativeClose()
 {
     showState = NativeDialogShowState.Closing;
     SendMessageHelper(
         NativeMethods.TASKDIALOG_MESSAGES.TDM_CLICK_BUTTON,
         (int)NativeMethods.TASKDIALOG_COMMON_BUTTON_RETURN_ID.IDCANCEL, 0);
 }
예제 #2
0
        internal void NativeShow()
        {
            // Applies config struct and other settings, then
            // calls main Win32 function.
            if (settings == null)
            {
                throw new InvalidOperationException(
                          "An error has occurred in dialog configuration.");
            }

            // 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 = NativeDialogShowState.Showing;

                // Here is the way we use "vanilla" P/Invoke to call
                // TaskDialogIndirect().
                HRESULT hresult = NativeMethods.TaskDialogIndirect(
                    nativeDialogConfig,
                    out selectedButtonID,
                    out selectedRadioButtonID,
                    out checkBoxChecked);

                if (ErrorHelper.Failed(hresult))
                {
                    string msg;
                    switch (hresult)
                    {
                    case HRESULT.E_INVALIDARG:
                        msg = "Invalid arguments to Win32 call.";
                        break;

                    case HRESULT.E_OUTOFMEMORY:
                        msg = "Dialog contents too complex.";
                        break;

                    default:
                        msg = String.Format(

                            "An unexpected internal error occurred in the Win32 call:{0:x}",
                            hresult);
                        break;
                    }
                    Exception e = Marshal.GetExceptionForHR((int)hresult);
                    throw new Win32Exception(msg, e);
                }
            }
            finally
            {
                showState = NativeDialogShowState.Closed;
            }
        }
예제 #3
0
        public bool?ShowDialog()
        {
            bool?result = null;

            try
            {
                // Fetch derived native dialog (i.e. Save or Open)

                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();

                // Process custom controls, and validate overall state
                ProcessControls();
                ValidateCurrentDialogState();

                // Apply outer properties to native dialog instance
                ApplyNativeSettings(nativeDialog);

                // Show dialog
                showState = NativeDialogShowState.Showing;
                int hresult = nativeDialog.Show(GetHandleFromWindow(parentWindow));
                showState = NativeDialogShowState.Closed;

                // Create return information
                if (ErrorHelper.Matches(hresult, Win32ErrorCode.ERROR_CANCELLED))
                {
                    canceled = true;
                    fileNames.Clear();
                }
                else
                {
                    canceled = false;

                    // Populate filenames - though only if user didn't cancel
                    PopulateWithFileNames(fileNames);
                }
                result = !canceled.Value;
            }
            catch
            {
                MessageBox.Show("Folder browser dialog unavailable.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                CleanUpNativeFileDialog();
                showState = NativeDialogShowState.Closed;
            }
            return(result);
        }
예제 #4
0
        /// <summary>
        /// Displays the dialog.
        /// </summary>
        /// <returns>A <see cref="Microsoft.SDK.Samples.VistaBridge.Library.CommonFileDialogResult"/> object.</returns>
        public CommonFileDialogResult ShowDialog()
        {
            CommonFileDialogResult result;

            try
            {
                // Fetch derived native dialog (i.e. Save or Open).
                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();

                // Apply outer properties to native dialog instance.
                ApplyNativeSettings(nativeDialog);
                InitializeEventSink(nativeDialog);

                // Clear user data if Reset has been called
                // since the last show.
                if (resetSelections)
                {
                    resetSelections = false;
                }

                // Show dialog.
                showState = NativeDialogShowState.Showing;
                int hresult = nativeDialog.Show(
                    CommonFileDialog.GetHandleFromWindow(parentWindow));
                showState = NativeDialogShowState.Closed;

                // Create return information.
                if (ErrorHelper.Matches(hresult,
                                        Win32ErrorCode.ERROR_CANCELLED))
                {
                    canceled = true;
                    fileNames.Clear();
                }
                else
                {
                    canceled = false;

                    // Populate filenames if user didn't cancel.
                    PopulateWithFileNames(fileNames);
                }
                result = new CommonFileDialogResult(canceled.Value);
            }
            finally
            {
                CleanUpNativeFileDialog();
            }
            return(result);
        }
예제 #5
0
        public CommonFileDialogResult ShowDialog()
        {
            CommonFileDialogResult result;

            try
            {
                // Fetch derived native dialog (i.e. Save or Open)
                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();

                // Process custom controls, and validate overall state
                ProcessControls();
                ValidateCurrentDialogState();

                // Apply outer properties to native dialog instance
                ApplyNativeSettings(nativeDialog);
                InitializeEventSink(nativeDialog);

                // Show dialog
                showState = NativeDialogShowState.Showing;
                int hresult = nativeDialog.Show(GetHandleFromWindow(parentWindow));
                showState = NativeDialogShowState.Closed;

                // Create return information
                if (ErrorHelper.Matches(hresult, Win32ErrorCode.ERROR_CANCELLED))
                {
                    canceled = true;
                    fileNames.Clear();
                }
                else
                {
                    canceled = false;

                    // Populate filenames - though only if user didn't cancel
                    PopulateWithFileNames(fileNames);
                }
                result = new CommonFileDialogResult(canceled.Value);
            }
            finally
            {
                CleanUpNativeFileDialog();
            }
            return(result);
        }
 public void Dispose()
 {
     Marshal.ReleaseComObject(Dialog);
     State = NativeDialogShowState.Closed;
 }
예제 #7
0
        internal void NativeShow()
        {
            // Applies config struct and other settings, then
            // calls main Win32 function
            if (settings == null)
            {
                throw new InvalidOperationException("An error has occurred in dialog configuration.");
            }

            // Do a last-minute parse of the various dialog control lists, only
            // allocate the memory at the last minute
            MarshalDialogControlStructs(settings);

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

                // Here is the way we would use "vanilla" PInvoke to call TaskDialogIndirect;
                // but if we did so here, we'd be at the mercy of the CLR's native DLL cache -
                // see below for more details.
                // HRESULT hresult = UnsafeNativeMethods.TaskDialogIndirect(
                //    nativeDialogConfig,
                //    out selectedButtonID,
                //    out selectedRadioButtonID,
                //    out checkBoxChecked);

                // Instead, we load comctl and bind to TaskDialogIndirect
                // We do it this way so we can rigidly control which version of comctl32
                // we get loaded, as the CLR's PInvoke mechanism maintains a DLL cache that is
                // NOT invalidated when the OS activation context is changed such that different
                // DLL versions are needed
                // TEST: Try to get the activation context goo working
                //DllVersionManager.CreateCorrectActivationContext();

                // TEST: Currently no worky, as the activation context isn't getting pushed properly
                IntPtr tdi = DllVersionManager.GetNativeFunctionPointer(
                    ExternDll.ComCtl32, "TaskDialogIndirect");

                // TEST: temporarily using the full path to v6 comctl32
                //IntPtr tdi = DllVersionManager.GetFunctionPointer(
                //    ExternDll.FullPathComCtl32, "TaskDialogIndirect");
                //DllVersionManager.ResetActivationContext();
                SafeNativeMethods.TDIDelegate taskDialogFunctionPointer = (SafeNativeMethods.TDIDelegate)
                                                                          Marshal.GetDelegateForFunctionPointer(tdi, typeof(SafeNativeMethods.TDIDelegate));

                // Invoke TaskDialogIndirect!
                HRESULT hresult = taskDialogFunctionPointer(
                    nativeDialogConfig,
                    out selectedButtonID,
                    out selectedRadioButtonID,
                    out checkBoxChecked);
                if (ErrorHelper.Failed(hresult))
                {
                    string msg;
                    switch (hresult)
                    {
                    case HRESULT.E_INVALIDARG:
                        msg = "Invalid arguments to Win32 call";
                        break;

                    case HRESULT.E_OUTOFMEMORY:
                        msg = "Dialog contents too complex";
                        break;

                    default:
                        msg = "An unexpected internal error occurred in the Win32 call: " + hresult;
                        break;
                    }
                    throw new Win32Exception(msg);
                }
            }
            finally
            {
                showState = NativeDialogShowState.Closed;
            }
        }
예제 #8
0
        public CommonFileDialogResult ShowDialog()
        {
            CommonFileDialogResult result;

            try
            {
                // Fetch derived native dialog (i.e. Save or Open)
                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();

                // Process custom controls, and validate overall state
                ProcessControls();
                ValidateCurrentDialogState();

                // Apply outer properties to native dialog instance
                ApplyNativeSettings(nativeDialog);
                InitializeEventSink(nativeDialog);

                // Show dialog
                showState = NativeDialogShowState.Showing;
                int hresult = nativeDialog.Show(GetHandleFromWindow(parentWindow));
                showState = NativeDialogShowState.Closed;

                // Create return information
                if (ErrorHelper.Matches(hresult, Win32ErrorCode.ERROR_CANCELLED))
                {
                    canceled = true;
                    fileNames.Clear();
                }
                else
                {
                    canceled = false;

                    // Populate filenames - though only if user didn't cancel
                    PopulateWithFileNames(fileNames);
                }
                result = new CommonFileDialogResult(canceled.Value);
            }
            finally
            {
                CleanUpNativeFileDialog();
            }
            return result;
        }
        /// <summary>
        /// Displays the dialog.
        /// </summary>
        /// <returns>A <see cref="Microsoft.SDK.Samples.VistaBridge.Library.CommonFileDialogResult"/> object.</returns>
        public CommonFileDialogResult ShowDialog()
        {
            CommonFileDialogResult result;

            try
            {
                // Fetch derived native dialog (i.e. Save or Open).
                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();

                // Apply outer properties to native dialog instance.
                ApplyNativeSettings(nativeDialog);
                InitializeEventSink(nativeDialog);

                // Clear user data if Reset has been called 
                // since the last show.
                if (resetSelections)
                {
                    resetSelections = false;
                }

                // Show dialog.
                showState = NativeDialogShowState.Showing;
                int hresult = nativeDialog.Show(
                    CommonFileDialog.GetHandleFromWindow(parentWindow));
                showState = NativeDialogShowState.Closed;

                // Create return information.
                if (ErrorHelper.Matches(hresult, 
                    Win32ErrorCode.ERROR_CANCELLED))
                {
                    canceled = true;
                    fileNames.Clear();
                }
                else
                {
                    canceled = false;

                    // Populate filenames if user didn't cancel.
                    PopulateWithFileNames(fileNames);
                }
                result = new CommonFileDialogResult(canceled.Value);
            }
            finally
            {
                CleanUpNativeFileDialog();
            }
            return result;
        }
예제 #10
0
        internal void NativeShow()
        {
            // Applies config struct and other settings, then
            // calls main Win32 function.
            if (settings == null)
                throw new InvalidOperationException(
                    "An error has occurred in dialog configuration.");

            // 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 = NativeDialogShowState.Showing;

                // Here is the way we use "vanilla" P/Invoke to call 
                // TaskDialogIndirect().  
                HRESULT hresult = NativeMethods.TaskDialogIndirect(
                    nativeDialogConfig,
                    out selectedButtonID,
                    out selectedRadioButtonID,
                    out checkBoxChecked );

                if (ErrorHelper.Failed(hresult))
                {
                    string msg;
                    switch (hresult)
                    {
                        case HRESULT.E_INVALIDARG:
                            msg = "Invalid arguments to Win32 call.";
                            break;
                        case HRESULT.E_OUTOFMEMORY:
                            msg = "Dialog contents too complex.";
                            break;
                        default:
                            msg = String.Format(
                             
                                "An unexpected internal error occurred in the Win32 call:{0:x}",
                                hresult);
                            break;
                    }
                    Exception e = Marshal.GetExceptionForHR((int)hresult);
                    throw new Win32Exception(msg,e);
                }
            }
            finally
            {
                showState = NativeDialogShowState.Closed;
            }
        }
예제 #11
0
 // The new task dialog does not support the existing 
 // Win32 functions for closing (e.g. EndDialog()); instead,
 // a "click button" message is sent. In this case, we're 
 // abstracting out to say that the TaskDialog consumer can
 // simply call "Close" and we'll "click" the cancel button. 
 // Note that the cancel button doesn't actually
 // have to exist for this to work.
 internal void NativeClose()
 {
     showState = NativeDialogShowState.Closing;
     SendMessageHelper(
         SafeNativeMethods.TASKDIALOG_MESSAGES.TDM_CLICK_BUTTON,
         (int)SafeNativeMethods.TASKDIALOG_COMMON_BUTTON_RETURN_ID.IDCANCEL, 0);
 }
예제 #12
0
        public bool?ShowDialog()
        {
            bool?result = null;

            try
            {
                // Fetch derived native dialog (i.e. Save or Open)

                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();

                // Process custom controls, and validate overall state
                ProcessControls();
                ValidateCurrentDialogState();

                // Apply outer properties to native dialog instance
                ApplyNativeSettings(nativeDialog);

                // Show dialog
                showState = NativeDialogShowState.Showing;
                int hresult = nativeDialog.Show(GetHandleFromWindow(parentWindow));
                showState = NativeDialogShowState.Closed;

                // Create return information
                if (ErrorHelper.Matches(hresult, Win32ErrorCode.ERROR_CANCELLED))
                {
                    canceled = true;
                    fileNames.Clear();
                }
                else
                {
                    canceled = false;

                    // Populate filenames - though only if user didn't cancel
                    PopulateWithFileNames(fileNames);
                }
                result = !canceled.Value;
            }
            catch
            {
                //If Vista Style dialog is unavailable, fall back to Windows Forms
                System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog
                {
                    SelectedPath        = fileName,
                    ShowNewFolderButton = true,
                    Description         = this.Title
                };
                result = (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK);
                if (result.HasValue && result.Value)
                {
                    canceled = false;
                    fileNames.Clear();
                    fileNames.Add(dialog.SelectedPath);
                }
                else
                {
                    fileNames.Clear();
                    canceled = true;
                }
            }
            finally
            {
                CleanUpNativeFileDialog();
                showState = NativeDialogShowState.Closed;
            }
            return(result);
        }
예제 #13
0
        internal void NativeShow()
        {
            // Applies config struct and other settings, then
            // calls main Win32 function
            if (settings == null)
                throw new InvalidOperationException("An error has occurred in dialog configuration.");

            // Do a last-minute parse of the various dialog control lists, only 
            // allocate the memory at the last minute
            MarshalDialogControlStructs(settings);

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

                // Here is the way we would use "vanilla" PInvoke to call TaskDialogIndirect;
                // but if we did so here, we'd be at the mercy of the CLR's native DLL cache -
                // see below for more details.
                // HRESULT hresult = UnsafeNativeMethods.TaskDialogIndirect(
                //    nativeDialogConfig,
                //    out selectedButtonID,
                //    out selectedRadioButtonID,
                //    out checkBoxChecked);

                // Instead, we load comctl and bind to TaskDialogIndirect
                // We do it this way so we can rigidly control which version of comctl32
                // we get loaded, as the CLR's PInvoke mechanism maintains a DLL cache that is 
                // NOT invalidated when the OS activation context is changed such that different
                // DLL versions are needed
                // TEST: Try to get the activation context goo working
                //DllVersionManager.CreateCorrectActivationContext();

                // TEST: Currently no worky, as the activation context isn't getting pushed properly
                IntPtr tdi = DllVersionManager.GetNativeFunctionPointer(
                    ExternDll.ComCtl32, "TaskDialogIndirect");
                
                // TEST: temporarily using the full path to v6 comctl32 
                //IntPtr tdi = DllVersionManager.GetFunctionPointer(
                //    ExternDll.FullPathComCtl32, "TaskDialogIndirect");
                //DllVersionManager.ResetActivationContext();
                SafeNativeMethods.TDIDelegate taskDialogFunctionPointer = (SafeNativeMethods.TDIDelegate)
                    Marshal.GetDelegateForFunctionPointer(tdi, typeof(SafeNativeMethods.TDIDelegate));

                // Invoke TaskDialogIndirect!
                HRESULT hresult = taskDialogFunctionPointer(
                    nativeDialogConfig,
                    out selectedButtonID,
                    out selectedRadioButtonID,
                    out checkBoxChecked);
                if (ErrorHelper.Failed(hresult))
                {
                    string msg;
                    switch (hresult)
                    {
                        case HRESULT.E_INVALIDARG:
                            msg = "Invalid arguments to Win32 call";
                            break;
                        case HRESULT.E_OUTOFMEMORY:
                            msg = "Dialog contents too complex";
                            break;
                        default:
                            msg = "An unexpected internal error occurred in the Win32 call: " + hresult;
                            break;
                    }
                    throw new Win32Exception(msg);
                }
            }
            finally
            {
                showState = NativeDialogShowState.Closed;
            }
        }
        public bool? ShowDialog()
        {
            bool? result = null;

            try
            {
                // Fetch derived native dialog (i.e. Save or Open)

                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();

                // Process custom controls, and validate overall state
                ProcessControls();
                ValidateCurrentDialogState();

                // Apply outer properties to native dialog instance
                ApplyNativeSettings(nativeDialog);

                // Show dialog
                showState = NativeDialogShowState.Showing;
                int hresult = nativeDialog.Show(GetHandleFromWindow(parentWindow));
                showState = NativeDialogShowState.Closed;

                // Create return information
                if (ErrorHelper.Matches(hresult, Win32ErrorCode.ERROR_CANCELLED))
                {
                    canceled = true;
                    fileNames.Clear();
                }
                else
                {
                    canceled = false;

                    // Populate filenames - though only if user didn't cancel
                    PopulateWithFileNames(fileNames);
                }
                result = !canceled.Value;
            }
            catch
            {
                //If Vista Style dialog is unavailable, fall back to Windows Forms

                System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
                dialog.SelectedPath = fileName;
                dialog.ShowNewFolderButton = true;
                dialog.Description = this.Title;

                result = (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK);
                if (result.HasValue && result.Value)
                {
                    canceled = false;
                    fileNames.Clear();
                    fileNames.Add(dialog.SelectedPath);
                }
                else
                {
                    fileNames.Clear();
                    canceled = true;
                }
            }
            finally
            {
                CleanUpNativeFileDialog();
                showState = NativeDialogShowState.Closed;
            }
            return result;
        }
예제 #15
0
        internal void NativeShow()
        {
            // Applies config struct and other settings, then
            // calls main Win32 function.
            if (settings == null)
            {
                throw new InvalidOperationException(
                          "An error has occurred in dialog configuration.");
            }

            // 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 = NativeDialogShowState.Showing;

                // Here is the way we would use "vanilla" PInvoke to call TaskDialogIndirect;
                // but if we did so here, we'd be at the mercy of the CLR's native DLL cache -
                // see below for more details.
                // HRESULT hresult = NativeMethods.TaskDialogIndirect(
                //    nativeDialogConfig,
                //    out selectedButtonID,
                //    out selectedRadioButtonID,
                //    out checkBoxChecked);

                // Instead, we load comctl and bind to TaskDialogIndirect
                // We do it this way so we can rigidly control
                // which version of comctl32 is loaded,
                // The CLR's PInvoke mechanism maintains a DLL cache that is
                // NOT invalidated when the OS activation context is changed
                // such that different DLL versions are needed.


                IntPtr tdi = DllVersionManager.GetNativeFunctionPointer(
                    ExternDll.ComCtl32, "TaskDialogIndirect");

                NativeMethods.TDIDelegate taskDialogFunctionPointer =
                    (NativeMethods.TDIDelegate)
                    Marshal.GetDelegateForFunctionPointer(
                        tdi,
                        typeof(NativeMethods.TDIDelegate)
                        );

                // Invoke TaskDialogIndirect.
                HRESULT hresult = taskDialogFunctionPointer(
                    nativeDialogConfig,
                    out selectedButtonID,
                    out selectedRadioButtonID,
                    out checkBoxChecked);
                if (ErrorHelper.Failed(hresult))
                {
                    string msg;
                    switch (hresult)
                    {
                    case HRESULT.E_INVALIDARG:
                        msg = "Invalid arguments to Win32 call.";
                        break;

                    case HRESULT.E_OUTOFMEMORY:
                        msg = "Dialog contents too complex.";
                        break;

                    default:
                        msg = String.Format(

                            "An unexpected internal error occurred in the Win32 call:{0:x}",
                            hresult);
                        break;
                    }
                    Exception e = Marshal.GetExceptionForHR((int)hresult);
                    throw new Win32Exception(msg, e);
                }
            }
            finally
            {
                showState = NativeDialogShowState.Closed;
            }
        }