I am trying to create Sticky WPF window.
If window nearer left stick to left or stick right if nearer to right else stick to top
Below the code I am using,
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);
}
}
The above code moves window only once.
on MSDN, How to: Set a Property After Animating It with a Storyboard
To run animation again, set BeginAnimation Property to NULL,
I tried setting the property to NULL before animation. And the code now works fine.
Now the code looks like
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);
}
}
If Noticed, I am placing window at -190 position at left and top to hide some of its part.
But using Below property, Its resetting the window position to 0. I dont want it to reset the position
_MyWindow.BeginAnimation(Window.LeftProperty, null);
Can anybody suggest how to do multiple animation without resetting existing position?
- Ashish Sapkale