2015-05-21 157 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頂部 –