WPF实现上下滚动字幕效果

 更新时间:2017年10月10日 14:10:37   作者:秋荷雨翔  
这篇文章主要为大家详细介绍了WPF实现上下滚动字幕效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

XAML代码:

<local:WorkSpaceContent x:Class="SunCreate.CombatPlatform.Client.NoticeMarquee"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:SunCreate.CombatPlatform.Client;assembly=SunCreate.CombatPlatform.Client"
  mc:Ignorable="d" 
  d:DesignHeight="35" d:DesignWidth="300" Loaded="WorkSpaceContent_Loaded" MouseEnter="WorkSpaceContent_MouseEnter" MouseLeave="WorkSpaceContent_MouseLeave">
 <local:WorkSpaceContent.Resources>
 <ControlTemplate x:Key="btnTemplate" TargetType="Button">
  <TextBlock Name="txt" Margin="5 0 5 0" Text="{TemplateBinding Content}" FontSize="12" Cursor="Hand" ToolTip="{TemplateBinding ToolTip}" Foreground="#fff" VerticalAlignment="Center"></TextBlock>
  <ControlTemplate.Triggers>
  <Trigger Property="IsMouseOver" Value="true">
   <Setter TargetName="txt" Property="Foreground" Value="#ff5e5e"></Setter>
  </Trigger>
  </ControlTemplate.Triggers>
 </ControlTemplate>
 <Storyboard x:Key="storyboard">
  <DoubleAnimation Duration="0:0:1" To="25" Storyboard.TargetName="stackPanel" Storyboard.TargetProperty="RenderTransform.Y"/>
 </Storyboard>
 </local:WorkSpaceContent.Resources>
 <Grid Background="#00a6da">
 <Grid.ColumnDefinitions>
  <ColumnDefinition Width="60"></ColumnDefinition>
  <ColumnDefinition></ColumnDefinition>
 </Grid.ColumnDefinitions>
 <TextBlock Margin="15 0 5 0" Text="公告:" FontSize="12" Foreground="#ffff33" VerticalAlignment="Center"></TextBlock>
 <ScrollViewer Grid.Column="1" Name="scrollViewer" HorizontalScrollBarVisibility="Hidden"
   HorizontalContentAlignment="Stretch"
   VerticalScrollBarVisibility="Hidden"
   VerticalContentAlignment="Stretch" Height="25">
  <Border Height="25" >
  <StackPanel x:Name="stackPanel" Margin="0 -25 0 0" >
   <StackPanel.RenderTransform>
   <TranslateTransform />
   </StackPanel.RenderTransform>
   <Button Name="btn1" Height="25" Click="btn_Click" Template="{StaticResource btnTemplate}"></Button>
   <Button Name="btn2" Height="25" Click="btn_Click" Template="{StaticResource btnTemplate}"></Button>
   <Button Name="btn3" Height="25" Click="btn_Click" Template="{StaticResource btnTemplate}"></Button>
  </StackPanel>
  </Border>
 </ScrollViewer>
 </Grid>
</local:WorkSpaceContent>

后台代码:

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
{
 /// <summary>
 /// 公告滚动显示
 /// </summary>
 public partial class NoticeMarquee : WorkSpaceContent
 {
 private System.Timers.Timer _timer;
 private List<TES_NOTICE> _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<INoticeService>().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("\r\n", string.Empty);
   btn1.CommandParameter = data1.ID;
   btn1.ToolTip = data1.NOTICE_CONTENT;
  }

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

  if (data3 != null)
  {
   btn3.Content = data3.NOTICE_CONTENT.Trim().Replace("\r\n", 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();
 }
 }
}

效果图:

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

相关文章

  • C#简单判断字符编码的方法

    C#简单判断字符编码的方法

    这篇文章主要介绍了C#简单判断字符编码的方法,可实现判断utf-8,unicode,ansi等编码的功能,简单实用,需要的朋友可以参考下
    2016-06-06
  • C#使用Npoi导出Excel并合并行列

    C#使用Npoi导出Excel并合并行列

    这篇文章主要为大家详细介绍了C#使用Npoi导出Excel并合并行列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • 一文带你快速学会C#中WinForm框架的使用详解

    一文带你快速学会C#中WinForm框架的使用详解

    WinForm是一门非常经济实惠的技术,就是说,可以在短时间内学会,并迅速借此进行项目开发。本文就来和大家聊聊WinForm框架的使用方法,希望对大家有所帮助
    2023-02-02
  • 基于使用递归推算指定位数的斐波那契数列值的解决方法

    基于使用递归推算指定位数的斐波那契数列值的解决方法

    本篇文章介绍了,基于使用递归推算指定位数的斐波那契数列值的解决方法。需要的朋友参考下
    2013-05-05
  • C#文件合并的方法

    C#文件合并的方法

    这篇文章主要介绍了C#文件合并的方法,实例分析了C#基于FileStream操作文件合并的相关技巧,需要的朋友可以参考下
    2015-07-07
  • 详解C#如何实现一个安全的事件订阅器

    详解C#如何实现一个安全的事件订阅器

    事件订阅器是一个对象,它订阅(或监听)某个事件,并在事件发生时执行相应的操作,本文主要介绍了C#实现一个安全的事件订阅器的相关知识,感兴趣的可以了解下
    2024-01-01
  • C#面向对象编程中开闭原则的示例详解

    C#面向对象编程中开闭原则的示例详解

    在面向对象编程中,SOLID 是五个设计原则的首字母缩写,旨在使软件设计更易于理解、灵活和可维护。本文将通过实例详细讲讲C#面向对象编程中开闭原则,需要的可以参考一下
    2022-07-07
  • c#调用arcgis地图rest服务示例详解(arcgis地图输出)

    c#调用arcgis地图rest服务示例详解(arcgis地图输出)

    ArcGIS REST API提供了简单、开放的接口来访问和使用ArcGIS Server发布的服务。使用ArcGIS REST API通过URL可以获取和操作每一个服务中的所有资源和操作
    2013-12-12
  • C#中while循环语句用法实例详解

    C#中while循环语句用法实例详解

    这篇文章主要介绍了C#中while循环语句用法,以实例形式详细分析了while语句的用法,并对return,continue,break的区别做了进一步的分析,需要的朋友可以参考下
    2014-10-10
  • NPOI实现两级分组合并功能(示例讲解)

    NPOI实现两级分组合并功能(示例讲解)

    下面小编就为大家分享一篇NPOI实现两级分组合并功能的示例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12

最新评论