Beispiel #1
0
        /// <summary>
        /// Shows the window as a modal dialog
        /// </summary>
        public DialogResult ShowDialog()
        {
            this.Parent  = WindowElement.ModalContainerPanel;
            this.isModal = true;

            this.LockKeyboardNavigation();
            this.LockMouseOutside();

            this.Show();

            // Set DialogResult default value
            this.DialogResult = DialogResult.None;

            try
            {
                // Push the current thread to a modal state
                ComponentDispatcher.PushModal();

                // Create a DispatcherFrame instance and use it to start a message loop
                this.dispatcherFrame = new DispatcherFrame();
                Dispatcher.PushFrame(this.dispatcherFrame);
            }
            finally
            {
                // Pop the current thread from modal state
                ComponentDispatcher.PopModal();
            }

            return(this.DialogResult);
        }
Beispiel #2
0
        public async Task <int> TranslateAcceleratorAsync(MSG msg)
        {
            var message = new Message
            {
                hwnd    = msg.hwnd,
                message = (int)msg.message,
                wParam  = msg.wParam,
                lParam  = msg.lParam,
                time    = (int)msg.time,
                pt_x    = msg.pt.x,
                pt_y    = msg.pt.y
            };

            var used = ComponentDispatcher.RaiseThreadMessage(ref message);

            if (used)
            {
                msg.message = (uint)message.message;
                msg.wParam  = message.wParam;
                msg.lParam  = message.lParam;

                return(VSConstants.S_OK);
            }

            if (_propertyPageSite != null)
            {
                await _projectThreadingService.SwitchToUIThread();

                return(_propertyPageSite.TranslateAccelerator(new MSG[] { msg }));
            }

            return(VSConstants.S_OK);
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        public bool TranslateAccelerator(ref MSG msg)
        {
            var message = new Message
            {
                hwnd    = msg.hwnd,
                message = (int)msg.message,
                wParam  = msg.wParam,
                lParam  = msg.lParam,
                time    = (int)msg.time,
                pt_x    = msg.pt.x,
                pt_y    = msg.pt.y
            };

            var used = ComponentDispatcher.RaiseThreadMessage(ref message);

            if (used)
            {
                msg.message = (uint)message.message;
                msg.wParam  = message.wParam;
                msg.lParam  = message.lParam;

                return(true);
            }

            return(false);
        }
Beispiel #5
0
 /// <summary>
 /// 给JS注入事件
 /// </summary>
 /// <param name="webBrowser"></param>
 /// <param name="eventName"></param>
 public static void InvokeJavaScript(WebBrowser webBrowser, string eventName)
 {
     LogHelper.WriteMethodLog(true);
     ComponentDispatcher.PushModal();
     Application.Current.Dispatcher.Invoke(new System.Action(() =>
     {
         webBrowser.InvokeScript(eventName);
     }));
     LogHelper.WriteMethodLog(false);
 }
Beispiel #6
0
 private static TResult PushFrame <TResult>(IDispatcherService dispatcher, Func <Task <TResult> > task)
 {
     return(dispatcher.Invoke(() =>
     {
         var frame = new DispatcherFrame();
         var frameTask = task().ContinueWith(x => { frame.Continue = false; return x.Result; });
         ComponentDispatcher.PushModal();
         Dispatcher.PushFrame(frame);
         ComponentDispatcher.PopModal();
         return frameTask.Result;
     }));
 }
Beispiel #7
0
 public static void Enter(ModalStateCancellationToken cancellationToken)
 {
     try
     {
         ComponentDispatcher.PushModal();
         Dispatcher.PushFrame(cancellationToken.DispatcherFrame);
     }
     finally
     {
         ComponentDispatcher.PopModal();
     }
 }
Beispiel #8
0
        /// <summary>
        /// Pumps messages for a fixed number of MS
        /// </summary>
        /// <param name="numMilliseconds">
        /// Number of milliseconds to wait.
        /// </param>
        public static void DoEvents(int numMilliseconds)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            while (sw.ElapsedMilliseconds < numMilliseconds)
            {
                Waiter.DoEvents();
                ComponentDispatcher.RaiseIdle();
                System.Threading.Thread.Sleep(0);
            }
            sw.Stop();
        }
        protected override bool PreProcessMessage(ref Message msg)
        {
            // send translated message via component dispatcher
            MSG dispatchMsg = new MSG();

            dispatchMsg.hwnd    = msg.HWnd;
            dispatchMsg.lParam  = msg.LParam;
            dispatchMsg.wParam  = msg.WParam;
            dispatchMsg.message = msg.Msg;
            if (ComponentDispatcher.RaiseThreadMessage(ref dispatchMsg) == true)
            {
                msg.Result = (IntPtr)1;
                return(true);
            }
            return(base.PreProcessMessage(ref msg));
        }
        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();
            }
        }
Beispiel #11
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);
        }
Beispiel #12
0
        public int TranslateAccelerator(VsMsg[] pMsg)
        {
            if (pMsg == null || pMsg.Length == 0)
            {
                throw new ArgumentNullException(nameof(pMsg));
            }

            var xMsg     = pMsg[0];
            var xMessage = new Message();

            xMessage.hwnd    = xMsg.hwnd;
            xMessage.message = (int)xMsg.message;
            xMessage.wParam  = xMsg.wParam;
            xMessage.lParam  = xMsg.lParam;
            xMessage.time    = (int)xMsg.time;
            xMessage.pt_x    = xMsg.pt.x;
            xMessage.pt_y    = xMsg.pt.y;

            var xUsed = ComponentDispatcher.RaiseThreadMessage(ref xMessage);

            if (xUsed)
            {
                xMsg.message = (uint)xMessage.message;
                xMsg.wParam  = xMessage.wParam;
                xMsg.lParam  = xMessage.lParam;

                return(VSConstants.S_OK);
            }

            int xResult = 0;

            if (mPropertyPageSite != null)
            {
                ProjectThreadingService.SwitchToUIThread();
                xResult = mPropertyPageSite.TranslateAccelerator(pMsg);
            }

            return(xResult);
        }
Beispiel #13
0
        /// <summary>
        /// Processes all UI messages currently in the message queue.
        /// </summary>
        public static void DoEvents()
        {
            // Create new nested message pump.
            DispatcherFrame nestedFrame = new DispatcherFrame();

            // Dispatch a callback to the current message queue, when getting called,
            // this callback will end the nested message loop.
            // note that the priority of this callback should be lower than the that of UI event messages.
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
                DispatcherPriority.Background, exitFrameCallback, nestedFrame);

            // pump the nested message loop, the nested message loop will immediately
            // process the messages left inside the message queue.
            Dispatcher.PushFrame(nestedFrame);

            // If the "exitFrame" callback doesn't get finished, Abort it.
            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }

            ComponentDispatcher.RaiseIdle();
        }
Beispiel #14
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();
                }
            }
        }
        protected override bool PreProcessMessage(ref Message msg)
        {
            // trap keyboard messages if window has focus
            if (msg.Msg == 256)
            {
                if (msg.WParam == (IntPtr)17)
                {
                    isControlKeyDepressed = true;
                    isOtherKeyDepressed   = false;
                }
                else if (msg.WParam == (IntPtr)16)
                {
                    isShifKeyDepressed  = true;
                    isOtherKeyDepressed = false;
                }
                else
                {
                    if (isOtherKeyDepressed)
                    {
                        isControlKeyDepressed = false;
                        isShifKeyDepressed    = false;
                    }
                    isOtherKeyDepressed = true;
                    if (isControlKeyDepressed)
                    {
                        if (isShifKeyDepressed)
                        {
                            switch (msg.WParam.ToInt64())
                            {
                            //case 65: // Ctrl+Shit+A command
                            //case 67: // Ctrl+Shit+C command
                            //case 78: // Ctrl+Shit+N command
                            //case 79: // Ctrl+Shit+O command
                            //case 83: // Ctrl+Shit+S command
                            //case 85: // Ctrl+Shit+U command
                            //case 88: // Ctrl+Shit+X command
                            //    isCommandCombinationDepressed = true;
                            //    break;
                            default:
                                isCommandCombinationDepressed = false;
                                break;
                            }
                        }
                        else
                        {
                            switch (msg.WParam.ToInt64())
                            {
                            case 70:     // Ctrl+E command
                                isCommandCombinationDepressed = true;
                                break;

                            default:
                                isCommandCombinationDepressed = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        isCommandCombinationDepressed = false;
                    }
                }

                if (isCommandCombinationDepressed == true)
                {
                    // send translated message via component dispatcher
                    MSG dispatchMsg = new MSG();
                    dispatchMsg.hwnd    = msg.HWnd;
                    dispatchMsg.lParam  = msg.LParam;
                    dispatchMsg.wParam  = msg.WParam;
                    dispatchMsg.message = msg.Msg;
                    ComponentDispatcher.RaiseThreadMessage(ref dispatchMsg);
                    msg.Result = (IntPtr)1;
                    return(true);
                }
            }
            return(base.PreProcessMessage(ref msg));
        }
        public ModalResult ShowModal(PopupItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (this.VerifyIsProcessDialogBox(item))
            {
                throw new InvalidOperationException($"视图类型 {typeof(ProcessDialogBox)} 不可以模态方式进行显示。");
            }

            //以模态显示窗口时,若发现最上层是模态窗口
            //当顶级窗口为MessageDialogBox时不予显示当前窗口
            //当顶级窗口不是MessageDialogBox时,若当前为MessageDialogBox,则显示
            if (this.VerifyTopItemModal())
            {
                if (this.VerifyTopItemIsMessageDialogBox() ||
                    !this.VerifyIsMessageDialogBox(item))
                {
                    PopupItemContainer container = this.PopupContainerFromIndex(this._popupStack.Items.Count - 1);
                    if (container != null)
                    {
                        container.Flicker();
                    }

                    return(null);
                }
            }

            this.VerifyCanShow(item);

            item._isShowing      = true;
            item._showingAsModal = true;
            item._modalResult    = null;
            CancelEventArgs ce = null;

            try
            {
                item.InternalShowing(out ce);
            }
            catch (Exception)
            {
                //状态还原
                item._isShowing      = false;
                item._showingAsModal = false;
                throw;
            }

            bool notcanceled = !ce.Cancel;

            try
            {
                if (notcanceled)
                {
                    ComponentDispatcher.PushModal();
                    item._dispatcherFrame = new DispatcherFrame();
                    try
                    {
                        this.ShowCore(item);
                    }
                    finally
                    {
                        //确保Shown事件异常时,Modal的结果不受影响
                        Dispatcher.PushFrame(item._dispatcherFrame);
                    }
                    return(item.ModalResult);
                }
            }
            finally
            {
                //确保ComponentDispatcher有进有出
                if (notcanceled)
                {
                    ComponentDispatcher.PopModal();
                }
            }

            return(null);
        }
        public ModalResult ShowModal(PopupItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (this.VerifyIsProcessDialogBox(item))
            {
                throw new Exception($"视图类型 {typeof(ProcessDialogBox)} 不可以模态方式进行显示。");
            }

            //以模态显示窗口时,若发现最上层是模态窗口
            //当顶级窗口为MessageDialogBox时不予显示当前窗口
            //当顶级窗口不是MessageDialogBox时,若当前为MessageDialogBox,则显示
            if (this.VerifyTopItemModal())
            {
                if (this.VerifyTopItemIsMessageDialogBox() ||
                    !this.VerifyIsMessageDialogBox(item))
                {
                    PopupItemContainer container = this.PopupContainerFromIndex(this._popupStack.Items.Count - 1);
                    if (container != null)
                    {
                        container.Flicker();
                    }

                    return(null);
                }
            }

            item.VerifyCanShow(this);

            item._showingAsModal = true;
            item._modalResult    = null;
            CancelEventArgs ce = null;

            try
            {
                item.InternalShowing(out ce);
            }
            catch (Exception)
            {
                item._showingAsModal = false;
                throw;
            }

            try
            {
                if (!ce.Cancel)
                {
                    ComponentDispatcher.PushModal();
                    item._dispatcherFrame = new DispatcherFrame();
                    item.ParentHostStack  = this;
                    item._isClosed        = false;
                    this.AddItem(item);
                    item.InternalShown(out EventArgs e);
                    Dispatcher.PushFrame(item._dispatcherFrame);
                    return(item.ModalResult);
                }
            }
            finally
            {
                ComponentDispatcher.PopModal();
                item.InternalDiapose();
            }

            return(null);
        }