粘性WPF窗口

时间:2015-05-21 06:32:20

标签: c# wpf winforms wpf-animation doubleanimation

我正在尝试创建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上,How to: Set a Property After Animating It with a 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);

有人可以建议如何在不重置现有位置的情况下进行多个动画吗?

  • Ashish Sapkale

1 个答案:

答案 0 :(得分:0)

我认为你将LeftProperty设置为null的想法是对的,但你应该在动画结束后这样做。

因此,为动画添加一个委托,并将该属性设置为null,看看它是否有效。

例如

strftime("%H:%M:%S")

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