using IOTContainer.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace IOTContainer.View { /// /// MarqueeControl.xaml 的交互逻辑 /// public partial class MarqueeControl : UserControl { Storyboard std = null; DoubleAnimation animation = null; int Speed = 150; string showText = string.Empty; public MarqueeControl() { InitializeComponent(); } public void Load(string text, double width, double height, string fontSize, string backgroundColor, int? backgroundOpacity, string fontColor, string fontStyle, string animationSpeed) { try { std = (Storyboard)canvas.Resources["stdLeft"]; this.Height = height; this.Width = width; canvas.Width = width; canvas.Height = height; content.Height = height; canvas.VerticalAlignment = VerticalAlignment.Center; content.VerticalAlignment = VerticalAlignment.Center; txtItem.FontSize = Convert.ToDouble(fontSize); BrushConverter converter = new BrushConverter(); if (!string.IsNullOrEmpty(backgroundColor)) { SolidColorBrush brush = (SolidColorBrush)converter.ConvertFromString(backgroundColor); if (backgroundOpacity != null) { brush.Opacity = backgroundOpacity.Value / 100.0; } canvas.Background = brush; } if (!string.IsNullOrEmpty(fontColor)) { Brush newFill = (Brush)converter.ConvertFromString(fontColor); txtItem.Foreground = newFill; } if (!string.IsNullOrEmpty(fontStyle)) { switch (fontStyle) { case "正常": txtItem.FontStyle = FontStyles.Normal; break; case "加粗": txtItem.FontWeight = FontWeights.Bold; break; case "斜体": txtItem.FontStyle = FontStyles.Italic; break; case "加粗、斜体": txtItem.FontStyle = FontStyles.Italic; txtItem.FontWeight = FontWeights.Bold; break; default: break; } } if (!string.IsNullOrEmpty(animationSpeed)) { switch (animationSpeed) { case "慢": Speed = 50; break; case "中等": Speed = 90; break; case "快": Speed = 130; break; case "很快": Speed = 180; break; default: Speed = 90; break; } } animation = (DoubleAnimation)std.Children[0]; std.Completed += (t, r) => changeItem(); showText = text; std.Completed += delegate { if (txtItem.Visibility != Visibility.Visible) { txtItem.Visibility = Visibility.Visible; txtItem.Text = showText; } }; changeItem(); } catch (Exception ex) { Log.MyLog.WriteLogFile("Load:" + ex.ToString(), "MarqueeControl"); } } private void changeItem() { try { txtItem.Text = showText; txtItem.UpdateLayout(); double canvasWidth = canvas.ActualWidth; double canvasHeight = canvas.ActualHeight; double txtWidth = txtItem.ActualWidth; double txtHeight = txtItem.ActualHeight; animation.From = canvasWidth; animation.To = -txtWidth; int time = 0; time = (int)((canvasWidth + txtWidth) / Speed); if (time < 2) time = 2; animation.Duration = new Duration(new TimeSpan(0, 0, time)); if (std != null) { std.Begin(); } } catch (Exception ex) { Log.MyLog.WriteLogFile("Load:" + ex.ToString(), "MarqueeControl"); } } public void Dispose() { std.Stop(); std.Children.Clear(); } } }