本文实例为大家分享了WPF上下滚动字幕的具体代码,供大家参考,具体内容如下
XAML代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
< 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 >
|
后台代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
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();
}
}
}
|
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/s0611163/archive/2017/10/09/7641153.html