using IOTContainer.Common; using IOTContainer.Communication; using IOTContainer.Model; using IOTContainer.ViewModel; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; 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 { /// /// ScreenWindowNew.xaml 的交互逻辑 /// public partial class ScreenWindowNew : Window { string outLinePath; ProgControl progControl = new ProgControl(); List Stops = new List(); StopPlay IsStop = new StopPlay(); IntPtr hWaitTimer = IntPtr.Zero; int isShowCountDown = 0; private DateTime? curLoadDate; DispatcherTimer LoadScreenTimer = new DispatcherTimer(); List sourceList = new List(); [DllImport("kernel32.dll")] static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long ft, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr pArgToCompletionRoutine, bool fResume); private Random _random; private DoubleAnimation _rectXAnima; private DoubleAnimation _rectYAnima; public ScreenWindowNew(int type = 1) { isShowCountDown = type; ComParameters.Parameters.EdgeWindow?.StopTimer(); InitializeComponent(); _rectXAnima = new DoubleAnimation(); _rectYAnima = new DoubleAnimation(); this.Top = 0; this.Left = 0; //设置窗口大小 this.WindowState = WindowState.Maximized; ComParameters.Parameters.ScreenWindowNew = this; #region 查询是否为同屏设备 if (ComParameters.Parameters.devType == "信发") { TimeJobs.Jobs.QuerySyncMachine(); } #endregion #region 屏保组件 try { if (!string.IsNullOrEmpty(ComParameters.Parameters.screenSaver) && File.Exists(ComParameters.Parameters.screenSaver)) { var bitmap = new BitmapImage(); using (var stream = new MemoryStream(File.ReadAllBytes(ComParameters.Parameters.screenSaver))) { bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = stream; bitmap.EndInit(); bitmap.Freeze(); ImageBrush _imageBrush = new ImageBrush(bitmap); waiting.Background = _imageBrush; } } else { string strImages = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenSaver", "bg.png"); if (File.Exists(strImages) == true) { var bitmap = new BitmapImage(); using (var stream = new MemoryStream(File.ReadAllBytes(strImages))) { bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = stream; bitmap.EndInit(); bitmap.Freeze(); ImageBrush _imageBrush = new ImageBrush(bitmap); waiting.Background = _imageBrush; } } } } catch (Exception ex) { Log.MyLog.WriteLogFile("屏保组件异常:"+ex.ToString(), "ScreenWindowNew"); } #endregion } private void Window_Loaded(object sender, RoutedEventArgs e) { try { outLinePath = AppDomain.CurrentDomain.BaseDirectory ; //string ScreenType = ComParameters.Parameters.resolution; //string ScreenWH = ScreenType.Substring(0, ScreenType.Length - 2); //string[] WH = ScreenWH.Split('*'); //this.Width = int.Parse(WH[0]); //this.Height = int.Parse(WH[1]); this.Width = SystemParameters.PrimaryScreenWidth; this.Height = SystemParameters.PrimaryScreenHeight; //自适应Touch if (ComParameters.Parameters.devType == "导视") { if((this.Width == 3840 && this.Height == 2160) || (this.Width == 2160 && this.Height == 3840)) { TouchLottie.Width = 600; TouchLottie.Height = 600; TouchImage.Width = 600; TouchImage.Height = 600; } else if((this.Width == 2560 && this.Height == 1440) || (this.Width == 1440 && this.Height == 2560)) { TouchLottie.Width = 450; TouchLottie.Height = 450; TouchImage.Width = 450; TouchImage.Height = 450; } } //隐藏光标 WinApi.ShowCursor(0); if (isShowCountDown == 1) { LoadPics(); } else { timeGrid.Visibility = Visibility.Hidden; gridMain.Visibility = Visibility.Visible; LoadData(); } } catch (Exception ex) { Log.MyLog.WriteLogFile("Window_Loaded:" + ex.ToString(), "ScreenWindowNew"); } } private void LoadPics() { try { Storyboard _storyboard = new Storyboard(); for (int i = 0; i < 220; 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/image/CountDowmImage/倒计时透明度_" + 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); Storyboard.SetTargetProperty(oauf, new PropertyPath("(Rectangle.Fill)")); //把动画应用到窗体的Rectangle中 Storyboard.SetTarget(oauf, RectDisImage); //RectDis是Rectangle的名称 _storyboard.Children.Add(oauf); oauf.Freeze(); //把ObjectAnimationUsingKeyFrames动画添加到Storyboard中 } _storyboard.Completed += new EventHandler(time_Completed); //_storyboard.RepeatBehavior = RepeatBehavior.Forever; _storyboard.Begin(); _storyboard.Freeze(); } catch (Exception ex) { Log.MyLog.WriteLogFile("LoadPics:" + ex.ToString(), "ScreenWindowNew"); } } private void time_Completed(object sender, EventArgs e) { timeGrid.Visibility = Visibility.Hidden; gridMain.Visibility = Visibility.Visible; LoadData(); } private void LoadData() { try { WebSocketCom.removeDelegate(); WebSocketCom.playIndexEvent += new WebSocketCom.playIndexDelegate(playIndex_ReceiveEvent); SystemManage.removeDelegate(); SystemManage.TouchMeEvent += new SystemManage.TouchMeDelegate(TouchMe_ReceiveEvent); HttpComm.Http.removeDelegate(); HttpComm.Http.ScreenConfigReceiveEvent += new HttpComm.ScreenConfigDelegate(ScreenConfig_ReceiveEvent); LoadScreenTimer.Tick += new EventHandler(LoadScreen); LoadScreenTimer.Interval = TimeSpan.FromSeconds(30); if (ComParameters.Parameters.devType == "导视" && !string.IsNullOrEmpty(ComParameters.Parameters.labelType) && 0 < Convert.ToInt32(ComParameters.Parameters.labelType) && Convert.ToInt32(ComParameters.Parameters.labelType) <= 10 && ComParameters.Parameters.devAttr == "触摸") { RectBtn.Visibility = Visibility.Visible; //悬浮图标 _random = new Random(); //加载悬浮按钮 //LoadRectBtn(); LoadLottieRectBtn(true); //设置按钮位置和动画 SetRectBtn(); }else if(ComParameters.Parameters.devType == "导视" && !string.IsNullOrEmpty(ComParameters.Parameters.labelType) && Convert.ToInt32(ComParameters.Parameters.labelType) >= 10 && ComParameters.Parameters.devAttr == "触摸" && File.Exists(ComParameters.Parameters.screenSaverTouch)) { string fileExtension = System.IO.Path.GetExtension(ComParameters.Parameters.screenSaverTouch); //悬浮图标 _random = new Random(); //设置按钮位置和动画 SetRectBtn(); if (fileExtension.ToLower() == ".json") { TouchLottie.Visibility = Visibility.Visible; TouchImage.Visibility = Visibility.Hidden; LoadLottieRectBtn(false); } else { TouchLottie.Visibility = Visibility.Hidden; TouchImage.Visibility = Visibility.Visible; TouchImage.Source = new BitmapImage(new Uri(ComParameters.Parameters.screenSaverTouch)); TouchImage.Source.Freeze(); } } else { RectBtn.Visibility = Visibility.Hidden; } StartPlay(); HttpComm.Http.DownResource(); LoadScreenTimer.Start(); } catch (Exception ex) { Log.MyLog.WriteLogFile("Window_Loaded:" + ex.ToString(), "ScreenWindowNew"); } } private void LoadScreen(object sender, EventArgs e) { if (curLoadDate.HasValue && curLoadDate.Value.Date != DateTime.Now.Date) { foreach (var item in sourceList) { if (!item.Token.IsCancellationRequested) { item.Cancel(); } } curLoadDate = DateTime.Now; sourceList.Clear(); HttpComm.Http.DownResource(); GetUControl(); } } private void playIndex_ReceiveEvent() { List progList = result.Where(i => Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + i.endTime) > DateTime.Now).OrderBy(i => i.beginTime).ToList(); PlayScheduler(progList[0]); } private void TouchMe_ReceiveEvent() { Dispatcher.Invoke((Action)(() => { RectBtn.Visibility = ComParameters.Parameters.devAttr == "触摸" && ComParameters.Parameters.devType == "导视" ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden; })); } private void ScreenConfig_ReceiveEvent() { try { foreach (var item in sourceList) { if (!item.Token.IsCancellationRequested) { item.Cancel(); item.Dispose(); } } sourceList.Clear(); GetUControl(); } catch (Exception ex) { Log.MyLog.WriteLogFile("ScreenConfig_ReceiveEvent:" + ex.ToString(), "ScreenWindowNew"); } } private void StartPlay() { curLoadDate = DateTime.Now; GetUControl(); } List result; private void GetUControl() { try { result = HttpComm.Http.GetResource(); if(result == null || result.Count == 0) { //如果没有节目,则继续展示导视 try { if(ComParameters.Parameters.devType == "导视") { Dispatcher.Invoke((Action)(() => { this.Hide(); Thread.Sleep(300); this.Close(); })); } else { Dispatcher.Invoke((Action)(() => { progControl.Dispose(); mainp.Visibility = Visibility.Hidden; waiting.Visibility = Visibility.Visible; })); } } catch (Exception ex) { Log.MyLog.WriteLogFile("GetUControl:" + ex.ToString(), "ScreenWindowNew"); } return; } else { Dispatcher.Invoke(new Action(() => { mainp.Visibility = Visibility.Visible; waiting.Visibility = Visibility.Hidden; })); } if (hWaitTimer != IntPtr.Zero) { WinApi.CloseHandle(hWaitTimer); hWaitTimer = IntPtr.Zero; } LoadScheduler(result); } catch (Exception ex) { Log.MyLog.WriteLogFile("GetUControl:" + ex.ToString(), "ScreenWindowNew"); } } private bool LoadScheduler(List progList) { bool isScheduler = false; if (hWaitTimer != IntPtr.Zero) { WinApi.CloseHandle(hWaitTimer); hWaitTimer = IntPtr.Zero; } try { progList = progList.Where(i => Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + i.endTime) > DateTime.Now).OrderBy(i => i.beginTime).ToList(); if (progList != null && progList.Count > 0) { if (Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + progList[0].beginTime) <= DateTime.Now.AddSeconds(+1)) { PlayScheduler(progList[0]); Action action = new Action(() => { var curDate = DateTime.Now; var stopTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + progList[0].endTime); UsDelay((stopTime.Ticks - curDate.Ticks)); if (hWaitTimer != IntPtr.Zero) { WinApi.CloseHandle(hWaitTimer); hWaitTimer = IntPtr.Zero; } LoadScheduler(progList); }); action.BeginInvoke(null, null); } else { Action action = new Action(() => { var curDate = DateTime.Now; var stopTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + progList[0].beginTime); UsDelay((stopTime.Ticks - curDate.Ticks)); if (hWaitTimer != IntPtr.Zero) { WinApi.CloseHandle(hWaitTimer); hWaitTimer = IntPtr.Zero; } PlayScheduler(progList[0]); Action action1 = new Action(() => { curDate = DateTime.Now; stopTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + progList[0].endTime); if (hWaitTimer != IntPtr.Zero) { WinApi.CloseHandle(hWaitTimer); hWaitTimer = IntPtr.Zero; } curDate = DateTime.Now; UsDelay((stopTime.Ticks - curDate.Ticks)); LoadScheduler(progList); }); action1.BeginInvoke(null, null); }); action.BeginInvoke(null, null); switch (ComParameters.Parameters.devType) { case "导视": this.Dispatcher.Invoke((Action)(() => { this.Hide(); Thread.Sleep(300); this.Close(); })); break; case "信发": this.Dispatcher.Invoke(new Action(() => { progControl.Dispose(); mainp.Visibility = Visibility.Hidden; waiting.Visibility = Visibility.Visible; })); break; } } } else { switch (ComParameters.Parameters.devType) { case "导视": this.Dispatcher.Invoke((Action)(() => { this.Hide(); Thread.Sleep(300); this.Close(); })); break; case "信发": this.Dispatcher.Invoke(new Action(() => { progControl.Dispose(); mainp.Visibility = Visibility.Hidden; waiting.Visibility = Visibility.Visible; })); break; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("LoadScheduler:" + ex.ToString(), "ScreenWindowNew"); } return isScheduler; } private async void PlayScheduler(ScheduleModel scheduler) { try { if (scheduler != null && scheduler.programs != null && scheduler.programs.Count > 0) { this.Dispatcher.Invoke(new Action(() => { mainp.Visibility = Visibility.Visible; waiting.Visibility = Visibility.Hidden; })); var playIndex = 0; CancellationTokenSource source1 = new CancellationTokenSource(); sourceList.Add(source1); StopPlay IsStop = new StopPlay(); foreach (var item in Stops) { item.IsStart = false; } Stops.Clear(); Stops.Add(IsStop); //续播功能 if (scheduler.programs.Count > 1) { if (ComParameters.Parameters.RecordProgramIndex.HasValue) { playIndex = ComParameters.Parameters.RecordProgramIndex.Value; ComParameters.Parameters.CurrentMaterialIndex = ComParameters.Parameters.RecordMaterialIndex.Value; } } while (IsStop.IsStart) { if (source1.Token.IsCancellationRequested) { //isStop = true; // 释放资源操作等等... break; } //同屏设备 if (ComParameters.Parameters.IsSync) { if (ComParameters.Parameters.IsMainMachine)//主设备 { Action ac = new Action(() => { WebSocketCom.Socket.ProgrammePlay(playIndex); }); ac.BeginInvoke(null, null); } else { if (ComParameters.Parameters.ProgramIndex.HasValue) { playIndex = ComParameters.Parameters.ProgramIndex.Value; } } } ComParameters.Parameters.CurrentProgramIndex = playIndex; var item = scheduler.programs[playIndex]; this.Dispatcher.Invoke(new Action(() => { progControl.Dispose(); progControl.LoadProg(this.Width, this.Height, item.backgroundMaterial, item.backgroundColor, item.components); mainp.Content = progControl; playIndex++; if (playIndex >= scheduler.programs.Count) { playIndex = 0; } })); //播放日志上传 Action ac1 = new Action(() => { try { HttpComm.Http.UploadPlayDetail(item.code, item.duration); } catch (Exception ex) { Log.MyLog.WriteLogFile("播放日志上传:" + ex.ToString(), "ScreenWindowNew"); } }); ac1.BeginInvoke(null, null); TimeSpan.TryParse(item.duration, out TimeSpan timeSpan); try { if (source1.Token.IsCancellationRequested) { break; } else { try { //Log.MyLog.WriteLogFile(Convert.ToInt32(timeSpan.TotalMilliseconds).ToString(), "UploadPlayDetail"); if (scheduler.programs.Count == 1) { await Task.Delay(Convert.ToInt32(28800000), source1.Token); } else { await Task.Delay(Convert.ToInt32(timeSpan.TotalMilliseconds), source1.Token); } } catch (Exception) { break; } } if (scheduler.programs.Count > 1) { ComParameters.Parameters.CurrentMaterialIndex = null; } } catch (Exception ex) { Log.MyLog.WriteLogFile("PlayScheduler:" + ex.ToString(), "ScreenWindowNew"); } } } else { switch (ComParameters.Parameters.devType) { case "导视": this.Dispatcher.Invoke((Action)(() => { this.Hide(); Thread.Sleep(300); this.Close(); })); break; case "信发": this.Dispatcher.Invoke(new Action(() => { mainp.Visibility = Visibility.Hidden; waiting.Visibility = Visibility.Visible; })); break; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("PlayScheduler:" + ex.ToString(), "ScreenWindowNew"); } } public void UsDelay(long us) { try { long duetime = -1 * us; if (hWaitTimer != IntPtr.Zero) { WinApi.CloseHandle(hWaitTimer); hWaitTimer = IntPtr.Zero; } if (hWaitTimer == IntPtr.Zero) { hWaitTimer = WinApi.CreateWaitableTimer(IntPtr.Zero, true, IntPtr.Zero); bool flag = SetWaitableTimer(hWaitTimer, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, false); UInt32 isFlag = WinApi.WaitForSingleObject(hWaitTimer, Timeout.Infinite); } } catch (Exception ex) { Log.MyLog.WriteLogFile("UsDelay:" + ex.ToString(), "ScreenWindowNew"); } } private void Window_Closed(object sender, EventArgs e) { try { progControl.Dispose(); //ComParameters.Parameters.EdgeWindow?.StartNewTimer(); switch (ComParameters.Parameters.devType) { case "导视": { ComParameters.Parameters.EdgeWindow.LeaveScreenSave(); break; } case "信发": { break; } } ComParameters.Parameters.IsPlay = false; ComParameters.Parameters.ScreenWindowNew = null; } catch (Exception ex) { Log.MyLog.WriteLogFile("Window_Closed:" + ex.ToString(), "ScreenWindowNew"); } } private void Window_MouseDown(object sender, MouseButtonEventArgs e) { try { //记录屏保播放位置 if (ComParameters.Parameters.CurrentMaterialIndex.HasValue) { ComParameters.Parameters.RecordMaterialIndex = ComParameters.Parameters.CurrentMaterialIndex.Value; } if (ComParameters.Parameters.CurrentProgramIndex.HasValue) { ComParameters.Parameters.RecordProgramIndex = ComParameters.Parameters.CurrentProgramIndex.Value; } switch (ComParameters.Parameters.devType) { case "导视": { //导视点击数据上报 Action ac = new Action(() => { try { HttpComm.Http.UploadClickData(); } catch (Exception ex) { Log.MyLog.WriteLogFile("导视点击数据上报:" + ex.ToString(), "ScreenWindowNew"); } }); ac.BeginInvoke(null, null); this.Dispatcher.Invoke((Action)(() => { this.Hide(); TouchLottie?.Dispose(); Thread.Sleep(300); this.Close(); })); break; } case "信发": { break; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("ScreenWindowNew关闭失败:" + ex.Message); } } #region 悬浮按钮 public void LoadLottieRectBtn(bool lottieTouch) { try { string lottiePath = lottieTouch ? System.IO.Path.Combine(outLinePath, "touch_Lottie", "touch0" + ComParameters.Parameters.labelType + ".json") : ComParameters.Parameters.screenSaverTouch; //string lottiePath = System.IO.Path.Combine(outLinePath, "touch_Lottie", "touch0" + ComParameters.Parameters.labelType + ".json"); if (File.Exists(lottiePath)) TouchLottie.FileName = lottiePath; TouchLottie.UseHardwareAcceleration(true); } catch (Exception ex) { Log.MyLog.WriteLogFile("悬浮按钮加载失败:" + ex.Message); } } /// /// 加载帧动画 /// public void LoadRectBtn() { try { Storyboard _storyboard = new Storyboard(); int count = 99; if (Convert.ToInt32(ComParameters.Parameters.labelType) == 3 || Convert.ToInt32(ComParameters.Parameters.labelType) == 4) { 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); oauf.Freeze(); //把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 private void Window_TouchDown(object sender, TouchEventArgs e) { try { switch (ComParameters.Parameters.devType) { case "导视": { //导视点击数据上报 Action ac = new Action(() => { try { HttpComm.Http.UploadClickData(); } catch (Exception ex) { Log.MyLog.WriteLogFile("导视点击数据上报:" + ex.ToString(), "ScreenWindowNew"); } }); ac.BeginInvoke(null, null); this.Dispatcher.Invoke((Action)(() => { this.Hide(); Thread.Sleep(300); this.Close(); })); break; } case "信发": { break; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("ScreenWindowNew关闭失败:" + ex.Message); } } } public class StopPlay { public bool IsStart = true; public StopPlay() { IsStart = true; } } }