using IOTContainer.Common; using IOTContainer.ViewModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; 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.Shapes; using System.Windows.Threading; namespace IOTContainer.View { /// /// TouchMe.xaml 的交互逻辑 /// public partial class TouchMe : Window { private double _topWidth; public double TopWidth { get { return _topWidth; } set { _topWidth = value; ComParameters.Parameters.TopTotalWidth = value + this.Width; } } private double _bottomWidth; public double BottomWidth { get { return _bottomWidth; } set { _bottomWidth = value; ComParameters.Parameters.BottomTotalWidth = value + this.Width; } } private DoubleAnimation _rectXAnima; private DoubleAnimation _rectYAnima; private Random _random; public TouchMe() { InitializeComponent(); ComParameters.Parameters.TouchMe = this; this.DataContext = new TouchMeViewModel(); //设置窗口大小 this.WindowState = WindowState.Maximized; this.Width = SystemParameters.PrimaryScreenWidth; this.Height = SystemParameters.PrimaryScreenHeight; _rectXAnima = new DoubleAnimation(); _rectYAnima = new DoubleAnimation(); this.Loaded += (sender, e) => { Thread thread = new Thread(() => { Dispatcher.BeginInvoke(new Action(() => { if (ComParameters.Parameters.devType == "导视" && !string.IsNullOrEmpty(ComParameters.Parameters.labelType) && ComParameters.Parameters.labelType != "0") { //悬浮图标 _random = new Random(); //加载悬浮按钮 LoadRectBtn(); //设置按钮位置和动画 SetRectBtn(); } SetTop(); })); }); thread.IsBackground = true; thread.Start(); //Task.Run(() => //{ // this.Dispatcher.Invoke(() => // { // if (ComParameters.Parameters.devType == "导视" && !string.IsNullOrEmpty(ComParameters.Parameters.labelType) && ComParameters.Parameters.labelType != "0") // { // //悬浮图标 // _random = new Random(); // //加载悬浮按钮 // LoadRectBtn(); // //设置按钮位置和动画 // SetRectBtn(); // } // Task.Delay(1000).Wait(); // SetTop(); // }); //}); }; } private void Window_Loaded(object sender, RoutedEventArgs e) { } private void Window_MouseDown(object sender, MouseButtonEventArgs e) { try { switch (ComParameters.Parameters.devType) { case "导视": { ComParameters.Parameters.ScreenWindowNew?.Dispatcher.Invoke(() => { ComParameters.Parameters.ScreenWindowNew.Close(); }); this.Close(); break; } case "信发": { break; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("TouchMe关闭失败:" + ex.Message); } } private void SetTop() { try { var count = 0; var _timer = new DispatcherTimer(); _timer.Tick += (sender, e) => { count++; if (!this.Topmost) { this.Topmost = true; } if (count >= 6) { _timer.Stop(); } }; _timer.Interval = TimeSpan.FromSeconds(0.5); _timer.Start(); } catch (Exception ex) { throw; } } #region 悬浮按钮 /// /// 加载帧动画 /// public void LoadRectBtn() { try { Storyboard _storyboard = new Storyboard(); int count = 99; if (Convert.ToInt32(ComParameters.Parameters.labelType) < 3) { count = 68; } for (int i = 0; i < count; i++) { ObjectAnimationUsingKeyFrames oauf = new ObjectAnimationUsingKeyFrames(); //ObjectAnimationUsingKeyFrames 可以对指定 Duration 内的一组 KeyFrames 中的 Object 属性值进行动画处理 BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); string name = i.ToString().PadLeft(5, '0'); bitmap.UriSource = new Uri("pack://application:,,,/IOTContainer;component/touch/touch" + ComParameters.Parameters.labelType + "/touch_" + name + ".png"); bitmap.CacheOption = BitmapCacheOption.Default; bitmap.EndInit(); bitmap.Freeze(); ImageBrush imgBrush = new ImageBrush(bitmap); //读取图片文件 DiscreteObjectKeyFrame dokf = new DiscreteObjectKeyFrame(); //DiscreteObjectKeyFrame 通过使用离散内插,可以在前一个关键帧的 Object 值及其自己的 Value 之间进行动画处理。 dokf.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(i * 30)); //KeyTime 获取或设置应到达关键帧的目标 Value 的时间 //这里每隔40毫秒设置一张图片,也就是每秒1000/40=25帧 dokf.Value = imgBrush; imgBrush.Freeze(); oauf.KeyFrames.Add(dokf); dokf.Freeze(); Storyboard.SetTargetProperty(oauf, new PropertyPath("(Rectangle.Fill)")); //把动画应用到窗体的Rectangle中 Storyboard.SetTarget(oauf, RectBtn); //RectDis是Rectangle的名称 _storyboard.Children.Add(oauf); //把ObjectAnimationUsingKeyFrames动画添加到Storyboard中 } _storyboard.RepeatBehavior = RepeatBehavior.Forever; _storyboard.Begin(); _storyboard.Freeze(); } catch (Exception ex) { Log.MyLog.WriteLogFile("悬浮按钮加载失败:" + ex.Message); } } /// /// 根据配置设置悬浮图标的位置 /// private void SetRectBtn() { try { switch (ComParameters.Parameters.labelEffect) { case "0"://固定位置 { switch (ComParameters.Parameters.labelLocation) { case "0"://右下角显示 { Canvas.SetRight(RectBtn, 0); Canvas.SetBottom(RectBtn, 0); break; } case "1"://居中 { Canvas.SetLeft(RectBtn, (this.Width - RectBtn.Width) / 2); Canvas.SetTop(RectBtn, (this.Height - RectBtn.Height) / 2); break; } } break; } case "1"://自由移动 { RectBtnMove(); break; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("设置悬浮图标的位置出错:" + ex.Message); } } private void RectBtnMove_Completed(object sender, EventArgs e) { RectBtnMove(); } /// /// 设置悬浮图标的动画 /// private void RectBtnMove() { Storyboard story = new Storyboard(); try { switch (ComParameters.Parameters.labelLocation) { case "2"://左右移动 { Canvas.SetLeft(RectBtn, 0); Canvas.SetBottom(RectBtn, 0); _rectXAnima.From = Canvas.GetLeft(RectBtn); _rectXAnima.To = this.Width - RectBtn.Width; _rectXAnima.Duration = new Duration(TimeSpan.FromSeconds((this.Width - RectBtn.Width - Canvas.GetLeft(RectBtn)) / ComParameters.Parameters.rectBtnSpeed)); _rectXAnima.AutoReverse = true; _rectXAnima.RepeatBehavior = RepeatBehavior.Forever; Storyboard.SetTarget(_rectXAnima, RectBtn); Storyboard.SetTargetProperty(_rectXAnima, new PropertyPath(Canvas.LeftProperty)); story.Children.Add(_rectXAnima); break; } case "3"://随机移动 { Canvas.SetLeft(RectBtn, 0); Canvas.SetTop(RectBtn, 0); //随机位置 var endX = _random.Next(0, Convert.ToInt32(this.Width - RectBtn.Width)); var endY = _random.Next(0, Convert.ToInt32(this.Height - RectBtn.Height)); //计算两点间的距离并计算所耗时间 var x = Math.Abs(Canvas.GetLeft(RectBtn) - endX); var y = Math.Abs(Canvas.GetTop(RectBtn) - endY); var z = Math.Sqrt(x * x + y * y); var time = z / ComParameters.Parameters.rectBtnSpeed; //X轴方向移动 _rectXAnima.From = Canvas.GetLeft(RectBtn); _rectXAnima.To = endX; _rectXAnima.Duration = new Duration(TimeSpan.FromSeconds(time)); Storyboard.SetTarget(_rectXAnima, RectBtn); Storyboard.SetTargetProperty(_rectXAnima, new PropertyPath(Canvas.LeftProperty)); story.Children.Add(_rectXAnima); //Y轴方向移动 _rectYAnima.From = Canvas.GetTop(RectBtn); _rectYAnima.To = endY; _rectYAnima.Duration = new Duration(TimeSpan.FromSeconds(time)); Storyboard.SetTarget(_rectYAnima, RectBtn); Storyboard.SetTargetProperty(_rectYAnima, new PropertyPath(Canvas.TopProperty)); story.Children.Add(_rectYAnima); story.Completed += RectBtnMove_Completed; break; } } story.Begin(); } catch (Exception ex) { Log.MyLog.WriteLogFile("设置悬浮图标的动画出错:" + ex.Message); } } #endregion #region 字幕 /// /// 顶部字幕 /// /// /// private void TopSubtitle_SizeChanged(object sender, SizeChangedEventArgs e) { try { TopWidth = TopSubtitle.ActualWidth; if (!string.IsNullOrEmpty(TopSubtitle.Text)) { var duration = ComParameters.Parameters.TopTotalWidth / (double)ComParameters.Parameters.subtitleSpeed; var da = new DoubleAnimation(this.Width, 0 - TopWidth, new Duration(TimeSpan.FromSeconds(duration))); TopSubtitle.RenderTransform = new TranslateTransform(); da.AutoReverse = false; Storyboard.SetTarget(da, TopSubtitle); Storyboard.SetTargetProperty(da, new PropertyPath("RenderTransform.X")); var sb = new Storyboard(); sb.Children.Add(da); sb.Begin(); } } catch (Exception ex) { Log.MyLog.WriteLogFile("顶部字幕播放失败:" + ex); } } /// /// 底部字幕 /// /// /// private void BottomSubtitle_SizeChanged(object sender, SizeChangedEventArgs e) { try { BottomWidth = BottomSubtitle.ActualWidth; if (!string.IsNullOrEmpty(BottomSubtitle.Text)) { var duration = ComParameters.Parameters.BottomTotalWidth / (double)ComParameters.Parameters.subtitleSpeed; var da = new DoubleAnimation(this.Width, 0 - BottomWidth, new Duration(TimeSpan.FromSeconds(duration))); BottomSubtitle.RenderTransform = new TranslateTransform(); da.AutoReverse = false; Storyboard.SetTarget(da, BottomSubtitle); Storyboard.SetTargetProperty(da, new PropertyPath("RenderTransform.X")); var sb = new Storyboard(); sb.Children.Add(da); sb.Begin(); } } catch (Exception ex) { Log.MyLog.WriteLogFile("底部字幕播放失败:" + ex); } } #endregion } }