栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

WPF实现上下滚动字幕效果

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WPF实现上下滚动字幕效果

本文实例为大家分享了WPF上下滚动字幕的具体代码,供大家参考,具体内容如下

XAML代码:


 
 
  
  
  
   
  
  
 
 
  
 
 
 
 
  
  
 
 
 
  
  
   
   
   
   
   
   
  
  
 
 

后台代码:

using SunCreate.CombatPlatform.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
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 SunCreate.CombatPlatform.Client
{
 /// 
 /// 公告滚动显示
 /// 
 public partial class NoticeMarquee : WorkSpaceContent
 {
 private System.Timers.Timer _timer;
 private List _data;
 private int _index;
 private Storyboard _storyboard;

 public NoticeMarquee()
 {
  InitializeComponent();
 }

 private void WorkSpaceContent_Loaded(object sender, RoutedEventArgs e)
 {
  if (_timer == null)
  {
  _storyboard = (Storyboard)this.FindResource("storyboard");

  System.Threading.Tasks.Task.Factory.StartNew(() =>
  {
   while (true)
   {
   int total = 0;
   _data = HI.Get().GetListPage(null, DateTime.MinValue, DateTime.Now, 1, 3, ref total).ToList();
   _data.Reverse();
   _index = _data.Count - 1;
   Dispatcher.BeginInvoke(new Action(() =>
   {
    stackPanel.RenderTransform = new TranslateTransform(0, 25);
   }));
   ShowData();
   Thread.Sleep(60 * 1000);
   }
  });

  _timer = new System.Timers.Timer();
  _timer.Interval = 5000;
  _timer.Elapsed += Action;
  _timer.Start();
  }
 }

 private void Action(object sender, ElapsedEventArgs e)
 {
  Dispatcher.BeginInvoke(new Action(() =>
  {
  stackPanel.RenderTransform = new TranslateTransform(0, 0);
  _storyboard.Begin();
  }));

  _index--;
  if (_index < 0)
  {
  _index = _data.Count - 1;
  }

  ShowData();
 }

 private void ShowData()
 {
  Dispatcher.BeginInvoke(new Action(() =>
  {
  TES_NOTICE data1 = GetData(_index, 0);
  TES_NOTICE data2 = GetData(_index, 1);
  TES_NOTICE data3 = GetData(_index, 2);

  if (data1 != null)
  {
   btn1.Content = data1.NOTICE_CONTENT.Trim().Replace("rn", string.Empty);
   btn1.CommandParameter = data1.ID;
   btn1.ToolTip = data1.NOTICE_CONTENT;
  }

  if (data2 != null)
  {
   btn2.Content = data2.NOTICE_CONTENT.Trim().Replace("rn", string.Empty);
   btn2.CommandParameter = data2.ID;
   btn2.ToolTip = data2.NOTICE_CONTENT;
  }

  if (data3 != null)
  {
   btn3.Content = data3.NOTICE_CONTENT.Trim().Replace("rn", string.Empty);
   btn3.CommandParameter = data3.ID;
   btn3.ToolTip = data3.NOTICE_CONTENT;
  }
  }));
 }

 private TES_NOTICE GetData(int index, int n)
 {
  if (_data != null)
  {
  int i = index + n;
  if (i > _data.Count - 1)
  {
   i = i % _data.Count;
  }
  return _data[i];
  }
  return null;
 }

 private void WorkSpaceContent_MouseEnter(object sender, MouseEventArgs e)
 {
  _timer.Stop();
 }

 private void WorkSpaceContent_MouseLeave(object sender, MouseEventArgs e)
 {
  _timer.Start();
 }

 private void btn_Click(object sender, RoutedEventArgs e)
 {
  Button btn = e.Source as Button;
  string dataId = btn.CommandParameter.ToString();
  NoticeView noticeView = new NoticeView(dataId);
  noticeView.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  noticeView.ShowDialog();
 }
 }
}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123197.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号