public bool?ShowDialog(Window owner) { this.CheckPermissionsToShowDialog(); if (owner == null) { return(this.ShowDialog()); } if (!Environment.UserInteractive) { throw new InvalidOperationException(SR.Get("CantShowModalOnNonInteractive")); } IntPtr criticalHandle = new WindowInteropHelper(owner).CriticalHandle; if (criticalHandle == IntPtr.Zero) { throw new InvalidOperationException(); } this._hwndOwnerWindow = criticalHandle; bool?result; try { ComponentDispatcher.CriticalPushModal(); result = new bool?(this.RunDialog(criticalHandle)); } finally { ComponentDispatcher.CriticalPopModal(); } return(result); }
public Nullable <bool> ShowDialog(Window owner) { CheckPermissionsToShowDialog(); // If a valid window wasn't passed into this function, we'll // call ShowDialog() to use the active window instead of // throwing an exception if (owner == null) { return(ShowDialog()); } // 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)); } // Get the handle of the owner window using WindowInteropHelper. IntPtr hwndOwner = (new WindowInteropHelper(owner)).CriticalHandle; // Just in case, check if the window's handle is zero. if (hwndOwner == IntPtr.Zero) { // CODE throw new InvalidOperationException(); } // 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(); } }
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); }
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(); } } }