WPF實(shí)現(xiàn)上下滾動(dòng)字幕效果
本文實(shí)例為大家分享了WPF上下滾動(dòng)字幕的具體代碼,供大家參考,具體內(nèi)容如下
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>
后臺(tái)代碼:
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>
 /// 公告滾動(dòng)顯示
 /// </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();
 }
 }
}
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#實(shí)現(xiàn)的pdf生成圖片文字水印類實(shí)例
欄 目:C#教程
下一篇:C#結(jié)合AForge實(shí)現(xiàn)攝像頭錄像
本文標(biāo)題:WPF實(shí)現(xiàn)上下滾動(dòng)字幕效果
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5464.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
 - 01-10C#實(shí)現(xiàn)多線程下載文件的方法
 - 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
 - 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
 - 01-10C#自定義簽名章實(shí)現(xiàn)方法
 - 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
 - 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
 


閱讀排行
- 1C語言 while語句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
 - 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-10C#中split用法實(shí)例總結(jié)
 


