Пример #1
0
        /// <summary>
        /// Fade out the window.
        /// </summary>
        public void FadeOut()
        {
            if (!IsLoaded)
            {
                return;           //without this it will crash at the below line.
            }
            RaiseEvent(new RoutedEventArgs(FadingOutEvent));

            Action action;

            if (CloseBehavior == CloseBehaviors.FadeOutAndClose)
            {
                nextCloseBehavior = CloseBehaviors.Close;
                if (IsModal == true)
                {
                    DialogResult = dialogResult;
                }
                action = () => Close();
            }
            else
            {
                action = () => Hide();
            }

            Task.Delay(250).ContinueWith(t => {
                Dispatcher.Invoke(() => {
                    action();
                    RaiseEvent(new RoutedEventArgs(FadedOutEvent));
                });
            });
        }
Пример #2
0
        public new void Show()
        {
            //subscribe parent window's mousedown event
            if (MenuMode)
            {
                if (ParentWindow == null)
                {
                    throw new ArgumentNullException($"{nameof(ParentWindow)} cannot be null when {nameof(MenuMode)} is true.");
                }
                Topmost = true;
                subParentMouseDown();
            }

            //init nextCloseBehavior
            nextCloseBehavior = CloseBehavior;

            base.Show();
        }
Пример #3
0
        /// <summary>
        /// Fade in the window at mouse position. Or the specified Left and Top position.
        /// If the window is already faded in, move the window to the new position. In this case the FadingInEvent will not be raised.
        /// </summary>
        public void FadeIn(double left = double.NaN, double top = double.NaN)
        {
            if (!IsLoaded)
            {
                Opacity = 0d;
                Show();//how to call something similar to initializecomponent?
            }

            //reset nextCloseBehavior
            nextCloseBehavior = CloseBehavior;

            //compute mouse position or set to existing values
            Point  newpos, realpos = default;
            var    mon       = NativeHelpers.GetMonitorFromWindow(this);
            double currscrnW = mon.Right;
            double currscrnH = mon.Bottom;

            if (WindowStartupLocation == WindowStartupLocation.Manual)
            {
                if (left.Equals(double.NaN) || top.Equals(double.NaN))
                {
                    //get the physical pixel-based position.
                    newpos = PointToScreen(Mouse.GetPosition(this));
                    //convert to the actual position considering the DPI settings etc.
                    realpos = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice.Transform(newpos);

                    //make sure the window is displayed inside the screens.
                    double originX, originY;
                    if (currscrnW - realpos.X > ActualWidth)
                    {
                        originX = 0d;
                    }
                    else
                    {
                        originX    = 1d;
                        realpos.X -= ActualWidth;
                    }
                    if (currscrnH - realpos.Y > ActualHeight)
                    {
                        originY = 0d;
                    }
                    else
                    {
                        originY    = 1d;
                        realpos.Y -= ActualHeight;
                    }
                    RenderTransformOrigin_BG = new Point(originX, originY);
                }
                else
                {
                    //make sure the window is displayed inside the screens.
                    if (left < 0d)
                    {
                        left = 0d;
                    }
                    if (top < 0d)
                    {
                        top = 0d;
                    }
                    if (left + ActualWidth > currscrnW)
                    {
                        left = currscrnW - ActualWidth;
                    }
                    if (top + ActualHeight > currscrnH)
                    {
                        top = currscrnH - ActualHeight;
                    }
                    realpos = new Point(left, top);
                }
            }

            if (Opacity == 0d)
            {
                if (realpos != default)
                {
                    Left = realpos.X;
                    Top  = realpos.Y;
                }
                Show();
                RaiseEvent(new RoutedEventArgs(FadingInEvent));
            }
            else
            {
                //move window to the new cursor location
                var easefunc = new CubicEase()
                {
                    EasingMode = EasingMode.EaseInOut
                };
                var anim_move_x = new DoubleAnimation(realpos.X, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.Stop)
                {
                    EasingFunction = easefunc
                };
                var anim_move_y = new DoubleAnimation(realpos.Y, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.Stop)
                {
                    EasingFunction = easefunc
                };
                BeginAnimation(LeftProperty, anim_move_x);
                BeginAnimation(TopProperty, anim_move_y);
            }
        }