2015-05-21 115 views
0

我想创建粘滞WPF窗口。粘滞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); 

任何人都可以建议如何做多个动画无需重置现有的位置?

  • 阿希什Sapkale

回答

0

我觉得你LeftProperty设置为空的想法是正确的,但你应该做的是,动画结束后。

因此,将一个委托添加到您的动画中,并将该属性设置为null,然后查看它是否有效。

例如

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

你的其他选择是使用ThicknessAnimation或TranslateTransform将您的窗口。让我知道如果你仍然有问题

+0

其实主要问题是,每当我打电话,_MyWindow.BeginAnimation(Window.TopProperty,null); 它重置其顶部位置为0. 在我的情况下,我想保持我的窗口在-190顶部 –