欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF实现上下滚动字幕效果

程序员文章站 2023-12-17 14:38:34
本文实例为大家分享了wpf上下滚动字幕的具体代码,供大家参考,具体内容如下 xaml代码:

本文实例为大家分享了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();
 }
 }
}

效果图:

WPF实现上下滚动字幕效果

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

上一篇:

下一篇: