Sticky WPF window

本文关键字:window WPF Sticky | 更新日期: 2023-09-27 17:50:05

我正在尝试创建Sticky WPF窗口。

如果窗口靠左,则向左移动;如果窗口靠右,则向右移动否则向左移动

在我使用的代码下面,

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                this.DragMove();
            }
    private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {                
                Point currentPoints = PointToScreen(Mouse.GetPosition(this));
//Place left
                if (currentPoints.X < (300))
                {                 
                    DoubleAnimation moveAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                    _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);
                }
//Place Right
                else if (currentPoints.X > (Screen.PrimaryScreen.WorkingArea.Width - 300))
                {
                    DoubleAnimation moveAnimation = new DoubleAnimation(Screen.PrimaryScreen.WorkingArea.Width + 190, TimeSpan.FromSeconds(1));
                    _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);              
                }
//Place top
                else
                {
                    DoubleAnimation TopAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                    _MyWindow.BeginAnimation(Window.TopProperty, TopAnimation, HandoffBehavior.Compose);
                }
            }

以上代码只移动窗口一次。

在MSDN上,如何:在Storyboard动画后设置属性

要再次运行动画,将BeginAnimation属性设置为NULL,

我尝试在动画之前将属性设置为NULL。现在代码可以正常工作了。

现在代码看起来像

private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point currentPoints = PointToScreen(Mouse.GetPosition(this));
            if (currentPoints.X < (300))
            {
                if (_MyWindow.HasAnimatedProperties)
                    _MyWindow.BeginAnimation(Window.LeftProperty, null);
                DoubleAnimation moveAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);
            }
            else if (currentPoints.X > (Screen.PrimaryScreen.WorkingArea.Width - 300))
            {
                if (_MyWindow.HasAnimatedProperties)
                    _MyWindow.BeginAnimation(Window.LeftProperty, null);
                DoubleAnimation moveAnimation = new DoubleAnimation(Screen.PrimaryScreen.WorkingArea.Width + 190, TimeSpan.FromSeconds(1));
                _MyWindow.BeginAnimation(Window.LeftProperty, moveAnimation);
            }
            else
            {
                if (_MyWindow.HasAnimatedProperties)
                    _MyWindow.BeginAnimation(Window.TopProperty, null);
                DoubleAnimation TopAnimation = new DoubleAnimation(-190, TimeSpan.FromSeconds(1));
                _MyWindow.BeginAnimation(Window.TopProperty, TopAnimation, HandoffBehavior.Compose);
            }
        }

如果注意到,我将窗口放置在左侧和顶部的-190位置,以隐藏其部分。

但使用属性下面,它将窗口位置重置为0。我不希望它重置位置

_MyWindow.BeginAnimation(Window.LeftProperty, null);

有没有人建议如何在不重置现有位置的情况下做多个动画?

  • 阿施施Sapkale

Sticky WPF window

我认为你将LeftProperty设置为null的想法是正确的,但你应该在动画完成后这样做。

所以给你的动画添加一个委托并在其中将属性设置为null看看它是否有效

例子
TopAnimation.Completed += (s,e) =>
{
   _MyWindow.BeginAnimation(Window.TopProperty, null);
};

你的其他选项是使用ThicknessAnimation或TranslateTransform来移动你的窗口。如果你还有问题,请告诉我