Exemplo n.º 1
0
        void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (Dispatcher.Thread != Thread.CurrentThread)
            {
                Dispatcher.BeginInvoke(new EventHandler <ElapsedEventArgs>(Timer_Elapsed), new object[] { sender, e });
                return;
            }
            try
            {
                Point curMousePos = m_lastItemOver != null?Win32Calls.GetPosition(m_lastItemOver) : new Point(-1, -1);

                if (m_lastItemOver != null && new Rect(0, 0, m_lastItemOver.ActualWidth, m_lastItemOver.ActualHeight).Contains(curMousePos))
                {
                    TimeSpan ts = DateTime.Now - m_timerTime;
                    if (ts.TotalMilliseconds > 500 || Mouse.LeftButton == MouseButtonState.Pressed)
                    {
                        ShowPopup();
                        m_lastItemOver.IsChecked = true;
                        m_lastItemOver           = null;
                        m_timerTime = DateTime.Now;
                    }
                    m_timer.Start();
                }
                else
                {
                    var app = Application.Current;
                    if (app == null)
                    {
                        return;
                    }

                    var window = app.MainWindow;
                    if (window == null || !window.IsActive)
                    {
                        return;
                    }

                    if (PART_Popup.IsOpen && !PART_Popup.Resizing)
                    {
                        var pt32 = new Windows.POINT();
                        Win32Calls.GetCursorPos(ref pt32);
                        Point  mousePos = new Point(pt32.x, pt32.y);
                        Point  pos      = PointToScreen(new Point(0, 0));
                        Matrix m        = PresentationSource.FromVisual(Window.GetWindow(this)).CompositionTarget.TransformToDevice;
                        if (m != Matrix.Identity)
                        {
                            m.Invert();
                            mousePos = m.Transform(mousePos);
                            pos      = m.Transform(pos);
                        }
                        switch (TabsPlacement)
                        {
                        case System.Windows.Controls.Dock.Top:
                            pos.Y += ActualHeight;
                            break;

                        case System.Windows.Controls.Dock.Bottom:
                            pos.Y -= PART_Popup.Height;
                            break;

                        case System.Windows.Controls.Dock.Left:
                            pos.X += ActualWidth;
                            break;

                        case System.Windows.Controls.Dock.Right:
                            pos.X -= PART_Popup.Width;
                            break;
                        }
                        bool  b2      = new Rect(pos.X, pos.Y, PART_Popup.Width, PART_Popup.Height).Contains(mousePos);
                        Point posThis = Win32Calls.GetPosition(this);
                        bool  b3      = new Rect(0, 0, ActualWidth, ActualHeight).Contains(posThis);
                        if (!b2 && !b3)
                        {
                            TimeSpan ts = DateTime.Now - m_timerTime;
                            if (ts.TotalMilliseconds > 1000)
                            {
                                // if mouse is outside for more than some time, then collapse this popup
                                ClosePopup();
                            }
                            else
                            {
                                m_timer.Start();
                            }
                        }
                        else
                        {
                            m_timerTime = DateTime.Now;
                            m_timer.Start();
                        }
                    }
                }
            }
            catch (InvalidOperationException)
            {
                // Can be thrown when closing the application and popup is still open...
            }
        }