Exemple #1
0
        public virtual bool?ShowDialog()
        {
            this.CheckPermissionsToShowDialog();
            if (!Environment.UserInteractive)
            {
                throw new InvalidOperationException(SR.Get("CantShowModalOnNonInteractive"));
            }
            IntPtr intPtr = UnsafeNativeMethods.GetActiveWindow();

            if (intPtr == IntPtr.Zero && Application.Current != null)
            {
                intPtr = Application.Current.ParkingHwnd;
            }
            HwndWrapper hwndWrapper = null;
            bool?       result;

            try
            {
                if (intPtr == IntPtr.Zero)
                {
                    hwndWrapper = new HwndWrapper(0, 0, 0, 0, 0, 0, 0, "", IntPtr.Zero, null);
                    intPtr      = hwndWrapper.Handle;
                }
                this._hwndOwnerWindow = intPtr;
                try
                {
                    ComponentDispatcher.CriticalPushModal();
                    result = new bool?(this.RunDialog(intPtr));
                }
                finally
                {
                    ComponentDispatcher.CriticalPopModal();
                }
            }
            finally
            {
                if (hwndWrapper != null)
                {
                    hwndWrapper.Dispose();
                }
            }
            return(result);
        }
Exemple #2
0
        public virtual Nullable <bool> ShowDialog()
        {
            CheckPermissionsToShowDialog();

            // Don't allow file dialogs to be shown if not in interactive mode
            // (for example, if we're running as a service)
            if (!Environment.UserInteractive)
            {
                throw new InvalidOperationException(SR.Get(SRID.CantShowModalOnNonInteractive));
            }

            // Call GetActiveWindow to retrieve the window handle to the active window
            // attached to the calling thread's message queue.  We'll set the owner of
            // the common dialog to this handle.
            IntPtr hwndOwner = UnsafeNativeMethods.GetActiveWindow();

            if (hwndOwner == IntPtr.Zero)
            {
                // No active window, so we'll use the parking window as the owner,
                // if its available.
                if (Application.Current != null)
                {
                    hwndOwner = Application.Current.ParkingHwnd;
                }
            }

            HwndWrapper tempParentHwnd = null;

            try
            {
                // No active window and application wasn't available or didn't have
                // a ParkingHwnd, we create a hidden parent window for the dialog to
                // prevent breaking UIAutomation.
                if (hwndOwner == IntPtr.Zero)
                {
                    tempParentHwnd = new HwndWrapper(0, 0, 0, 0, 0, 0, 0, "", IntPtr.Zero, null);
                    hwndOwner      = tempParentHwnd.Handle;
                }

                // Store the handle of the owner window inside our class so we can use it
                // to center the dialog later.
                _hwndOwnerWindow = hwndOwner;

                // Signal that this thread is going to go modal.
                try
                {
                    ComponentDispatcher.CriticalPushModal();

                    return(RunDialog(hwndOwner));
                }
                finally
                {
                    ComponentDispatcher.CriticalPopModal();
                }
            }
            finally
            {
                if (tempParentHwnd != null)
                {
                    tempParentHwnd.Dispose();
                }
            }
        }