我正在尝试创建 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 上,如何:使用情节提要对其进行动画处理后设置属性
要再次运行动画,请将 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