Пример #1
0
        /// <summary>
        /// Displays the <see cref="ContextMenu" /> if it was set.
        /// </summary>
        /// <param name="cursorPosition">The cursor Position.</param>
        private void ShowContextMenu(Point cursorPosition)
        {
            if (this.IsDisposed)
            {
                return;
            }

            // raise preview event no matter whether context menu is currently set
            // or not (enables client to set it on demand)
            var args = this.RaisePreviewTrayContextMenuOpenEvent();

            if (args.Handled)
            {
                return;
            }

            if (this.ContextMenu != null)
            {
                // use absolute position
                this.ContextMenu.Placement        = PlacementMode.AbsolutePoint;
                this.ContextMenu.HorizontalOffset = cursorPosition.X;
                this.ContextMenu.VerticalOffset   = cursorPosition.Y;
                this.ContextMenu.IsOpen           = true;

                // activate the message window to track deactivation - otherwise, the context menu
                // does not close if the user clicks somewhere else
                NativeMethods.SetForegroundWindow(this.messageSink.MessageWindowHandle);

                // bubble event
                this.RaiseTrayContextMenuOpenEvent();
            }
        }
Пример #2
0
        /// <summary>
        /// Displays the <see cref="TrayPopup" /> control if it was set.
        /// </summary>
        /// <param name="cursorPosition">The cursor Position.</param>
        private void ShowTrayPopup(Point cursorPosition)
        {
            if (this.IsDisposed)
            {
                return;
            }

            // raise preview event no matter whether popup is currently set
            // or not (enables client to set it on demand)
            var args = this.RaisePreviewTrayPopupOpenEvent();

            if (args.Handled)
            {
                return;
            }

            if (this.TrayPopup != null)
            {
                // use absolute position, but place the popup centered above the icon
                this.TrayPopupResolved.Placement        = PlacementMode.AbsolutePoint;
                this.TrayPopupResolved.HorizontalOffset = cursorPosition.X;
                this.TrayPopupResolved.VerticalOffset   = cursorPosition.Y;

                // open popup
                this.TrayPopupResolved.IsOpen = true;

                // activate the message window to track deactivation - otherwise, the context menu
                // does not close if the user clicks somewhere else
                NativeMethods.SetForegroundWindow(this.messageSink.MessageWindowHandle);

                // raise attached event - item should never be null unless developers
                // changed the CustomPopup directly...
                if (this.TrayPopup != null)
                {
                    RaisePopupOpenedEvent(this.TrayPopup);
                }

                // bubble routed event
                this.RaiseTrayPopupOpenEvent();
            }
        }
Пример #3
0
        private void OnMouseEvent(object sender, MouseEvent me)
        {
            if (this.IsDisposed)
            {
                return;
            }

            switch (me)
            {
            case MouseEvent.MouseMove:
                this.RaiseTrayMouseMoveEvent();

                // immediately return - there's nothing left to evaluate
                return;

            case MouseEvent.IconRightMouseDown:
                this.RaiseTrayRightMouseDownEvent();
                break;

            case MouseEvent.IconLeftMouseDown:
                this.RaiseTrayLeftMouseDownEvent();
                break;

            case MouseEvent.IconRightMouseUp:
                this.RaiseTrayRightMouseUpEvent();
                break;

            case MouseEvent.IconLeftMouseUp:
                this.RaiseTrayLeftMouseUpEvent();
                break;

            case MouseEvent.IconMiddleMouseDown:
                this.RaiseTrayMiddleMouseDownEvent();
                break;

            case MouseEvent.IconMiddleMouseUp:
                this.RaiseTrayMiddleMouseUpEvent();
                break;

            case MouseEvent.IconDoubleClick:

                // cancel single click timer
                this.singleClickTimer.Change(Timeout.Infinite, Timeout.Infinite);

                // bubble event
                this.RaiseTrayMouseDoubleClickEvent();
                break;

            case MouseEvent.BalloonToolTipClicked:
                this.RaiseTrayBalloonTipClickedEvent();
                break;

            default:
                var message = string.Format(CultureInfo.CurrentUICulture, Properties.Resources.MissingHandlerForMouseEventFlag_, me);
                throw new ArgumentOutOfRangeException("me", message);
            }

            // get mouse coordinates
            // var cursorPositionInterop = new Interop.Point();
            // var cursorPosition = NativeMethods.GetCursorPos(ref cursorPositionInterop);
            var cursorPosition = new Point();

            NativeMethods.GetCursorPos(ref cursorPosition);

            var isLeftClickCommandInvoked = false;

            // show popup, if requested
            if (me.IsMatch(this.PopupActivation))
            {
                if (me == MouseEvent.IconLeftMouseUp)
                {
                    // show popup once we are sure it's not a double click
                    this.delayedTimerAction = () =>
                    {
                        this.LeftClickCommand.ExecuteIfEnabled(this.LeftClickCommandParameter, this.LeftClickCommandTarget ?? this);
                        this.ShowTrayPopup(cursorPosition);
                    };
                    this.singleClickTimer.Change(NativeMethods.GetDoubleClickTime(), Timeout.Infinite);
                    isLeftClickCommandInvoked = true;
                }
                else
                {
                    // show popup immediately
                    this.ShowTrayPopup(cursorPosition);
                }
            }

            // show context menu, if requested
            if (me.IsMatch(this.MenuActivation))
            {
                if (me == MouseEvent.IconLeftMouseUp)
                {
                    // show context menu once we are sure it's not a double click
                    this.delayedTimerAction = () =>
                    {
                        this.LeftClickCommand.ExecuteIfEnabled(this.LeftClickCommandParameter, this.LeftClickCommandTarget ?? this);
                        this.ShowContextMenu(cursorPosition);

                        // ReSharper disable LocalizableElement
                        this.ShowBalloonTip("test", "testmessage", BalloonIconStates.Warning, this.Handle);

                        // ReSharper restore LocalizableElement
                    };
                    this.singleClickTimer.Change(NativeMethods.GetDoubleClickTime(), Timeout.Infinite);
                    isLeftClickCommandInvoked = true;
                }
                else
                {
                    // show context menu immediately
                    this.ShowContextMenu(cursorPosition);
                }
            }

            // make sure the left click command is invoked on mouse clicks
            if (me == MouseEvent.IconLeftMouseUp && !isLeftClickCommandInvoked)
            {
                // show context menu once we are sure it's not a double click
                this.delayedTimerAction = () => this.LeftClickCommand.ExecuteIfEnabled(this.LeftClickCommandParameter, this.LeftClickCommandTarget ?? this);
                this.singleClickTimer.Change(NativeMethods.GetDoubleClickTime(), Timeout.Infinite);
            }
        }