using PCScreenSavers.Common; using PCScreenSavers.ViewModel; using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Animation; namespace PCScreenSavers.Win { /// /// MarqueeControl.xaml 的交互逻辑 /// public partial class MarqueeControl : UserControl { App app = ((App)Application.Current); Storyboard std = null; DoubleAnimation animation = null; int total; Class_Log log = new Class_Log(); public MarqueeControl() { InitializeComponent(); } public MarqueeType ShowType { get { return (MarqueeType)this.GetValue(ShowTypeProperty); } set { this.SetValue(ShowTypeProperty, value); } } public static readonly DependencyProperty ShowTypeProperty = DependencyProperty.Register("ShowType", typeof(MarqueeType), typeof(MarqueeControl), new PropertyMetadata(MarqueeType.Up)); public double Speed { get { return (double)this.GetValue(SpeedProperty); } set { this.SetValue(SpeedProperty, value); } } public static readonly DependencyProperty SpeedProperty = DependencyProperty.Register("Speed", typeof(double), typeof(MarqueeControl), new PropertyMetadata(1.5)); public int SubTitleType { get { return (int)this.GetValue(SubTitleTypeProperty); } set { this.SetValue(SubTitleTypeProperty, value); } } public static readonly DependencyProperty SubTitleTypeProperty = DependencyProperty.Register("SubTitleType", typeof(int), typeof(MarqueeControl), new PropertyMetadata(1)); private void UserControl_Loaded(object sender, RoutedEventArgs e) { if (ShowType == MarqueeType.Up || ShowType == MarqueeType.Down) { std = (Storyboard)canvas.Resources["stdUp"]; content.Width = canvas.ActualWidth; txtItem.TextWrapping = TextWrapping.Wrap; } if (ShowType == MarqueeType.Left || ShowType == MarqueeType.Right) { std = (Storyboard)canvas.Resources["stdLeft"]; content.Height = canvas.ActualHeight; } animation = (DoubleAnimation)std.Children[0]; std.Completed += (t, r) => changeItem(); } private List itemsSource; public List ItemsSource { get { return itemsSource; } set { this.Dispatcher.BeginInvoke(new Action(() => { if (std != null) { std.Stop(); txtItem.Text = ""; itemsSource = value; if (itemsSource != null && itemsSource.Count > 0) { if (SubTitleType == 1) { app.TopSubTitleIndex = 0; } else { app.BottomSubTitleIndex = 0; } total = value.Count; changeItem(); } } })); } } private void changeItem() { try { if (SubTitleType == 1) { if (app.TopSubTitleIndex >= itemsSource.Count) { app.TopSubTitleIndex = 0; ItemsSource = null; app.TopSubTitle.Clear(); if (std != null) { std.Begin(); } return; } txtItem.Text = app.TopSubTitle[app.TopSubTitleIndex].Replace("\n", "").ToString(); } else { if (app.BottomSubTitleIndex >= itemsSource.Count) { app.BottomSubTitleIndex = 0; ItemsSource = null; //ItemsSource = app.BottomSubTitle; app.BottomSubTitle.Clear(); if (std != null) { std.Begin(); } return; } txtItem.Text = app.BottomSubTitle[app.BottomSubTitleIndex].Replace("\n", "").ToString(); } //if (index >= itemsSource.Count) //{ // index = 0; // if (SubTitleType == 1) // { // //ItemsSource = app.TopSubTitle; // ItemsSource = null; // app.TopSubTitle.Clear(); // log.WriteLogFile("清空app.TopSubTitle" + app.TopSubTitle.Count); // } // else // { // ItemsSource = null; // //ItemsSource = app.BottomSubTitle; // app.BottomSubTitle.Clear(); // log.WriteLogFile("清空app.BottomSubTitle" + app.BottomSubTitle.Count); // } // if (std != null) // { // std.Begin(); // } //} txtItem.UpdateLayout(); //double canvasWidth = canvas.ActualWidth; double canvasWidth = canvas.ActualWidth; double canvasHeight = canvas.ActualHeight; double txtWidth = txtItem.ActualWidth; double txtHeight = txtItem.ActualHeight; if (ShowType == MarqueeType.Up) { animation.From = canvasHeight; animation.To = -txtHeight; } else if (ShowType == MarqueeType.Down) { animation.From = -txtHeight; animation.To = canvasHeight; } else if (ShowType == MarqueeType.Left) { animation.From = canvasWidth; animation.To = -txtWidth; } else if (ShowType == MarqueeType.Right) { animation.From = -txtWidth; animation.To = canvasWidth; } int time = 0; if (ShowType == MarqueeType.Up || ShowType == MarqueeType.Down) { //time = (int)(txtHeight / canvasHeight * Speed); time = (int)((canvasHeight+ txtHeight) / Speed); } if (ShowType == MarqueeType.Left || ShowType == MarqueeType.Right) { //time = (int)(txtWidth / canvasWidth * Speed); time = (int)((canvasWidth+ txtWidth) / Speed); } if (time < 2) time = 2; animation.Duration = new Duration(new TimeSpan(0, 0, time)); if (std != null) { std.Begin(); } if (SubTitleType == 1) { app.TopSubTitleIndex++; } else { app.BottomSubTitleIndex++; } //index++; } catch (Exception e) { log.WriteLogFile(e.Message, "subtitle"); //throw; } } } }