WPF 实现跑马灯效果的Label控件,数据绑定方式实现
原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现
项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件;具体代码如下
using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using Tool;
namespace iMasteRayClient.View.ViewUnit
{
public class UIScrollingLabel : Label
{
/// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留3秒
/// </summary>
public int StopSecond
{
get { return _StopSecond; }
set
{
_StopSecond = value;
}
}
/// <summary>
/// 滚动的速度
/// </summary>
public double RunSpeed
{
get { return _RunSpeed; }
set
{
_RunSpeed = value;
MarqueeTimer.Interval = _RunSpeed;
}
}
/// <summary>
/// 滚动文字源
/// </summary>
public string TextSource
{
get { return _TextSource; }
set
{
_TextSource = value;
_TempString = _TextSource + " ";
_OutText = _TempString;
}
}
private string SetContent
{
get { return Content.ToString(); }
set
{
Content = value;
}
}
/// <summary>
/// 构造函数
/// </summary>
public UIScrollingLabel(double m_Width, double m_Height,string m_Text)
{
this.Width = m_Width;
this.Height = m_Height;
MarqueeTimer.Interval = _RunSpeed;//文字移动的速度
MarqueeTimer.Enabled = false ; //开启定时触发事件
MarqueeTimer.Elapsed += new ElapsedEventHandler(MarqueeTimer_Elapsed);//绑定定时事件
this.Loaded += new RoutedEventHandler(ScrollingTextControl_Loaded);//绑定控件Loaded事件
this.TargetUpdated += UIScrollingLabel_TargetUpdated;//绑定使用数据绑定方式修改Content后的响应事件,即判断是否开启滚动定时器
}
private void UIScrollingLabel_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
{
TextSource = this.Content.ToString() ;
if (GetTextDisplayWidthHelper.GetTextDisplayWidth(this) > this.Width)
{
MarqueeTimer.Enabled = true;
}
else
{
MarqueeTimer.Enabled = false;
}
}
void ScrollingTextControl_Loaded(object sender, RoutedEventArgs e)
{
_TextSource = SetContent;
_TempString = _TextSource + " ";
_OutText = _TempString;
_SignTime = DateTime.Now;
}
void MarqueeTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (string.IsNullOrEmpty(_OutText)) return;
if (_OutText.Substring(1) + _OutText[0] == _TempString)
{
if (_IfFirst)
{
_SignTime = DateTime.Now;
}
if ((DateTime.Now - _SignTime).TotalMilliseconds > _StopSecond)
{
_IfFirst = true;
}
else
{
_IfFirst = false;
return;
}
}
_OutText = _OutText.Substring(1) + _OutText[0];
Dispatcher.BeginInvoke(new Action(() =>
{
SetContent = _OutText;
}));
}
/// <summary>
/// 定时器
/// </summary>
Timer MarqueeTimer = new Timer();
/// <summary>
/// 滚动文字源
/// </summary>
String _TextSource = "";
/// <summary>
/// 输出文本
/// </summary>
String _OutText = string.Empty;
/// <summary>
/// 过度文本存储
/// </summary>
string _TempString = string.Empty;
/// <summary>
/// 文字的滚动速度
/// </summary>
double _RunSpeed = 200;
DateTime _SignTime;
bool _IfFirst = true;
/// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留3秒
/// </summary>
int _StopSecond = 3000;
}
}
其中需要判断文本的显示长度,故实现了一个测量文本长度的工具类:
using System;
using System.Windows;
using System.Windows.Media;
using System.Globalization;
using System.Windows.Controls;
namespace Tool
{
static class GetTextDisplayWidthHelper
{
public static Double GetTextDisplayWidth(Label label)
{
return GetTextDisplayWidth(label.Content.ToString(), label.FontFamily, label.FontStyle, label.FontWeight, label.FontStretch, label.FontSize);
}
public static Double GetTextDisplayWidth(string str, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch,double FontSize)
{
var formattedText = new FormattedText(
str,
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
FontSize,
Brushes.Black
);
Size size = new Size(formattedText.Width, formattedText.Height);
return size.Width;
}
}
}
参考﹎蓝言觅ぷ雨的文章,连接:http://www.cnblogs.com/lanymy/archive/2012/07/11/2586643.html 表示感谢
WPF 实现跑马灯效果的Label控件,数据绑定方式实现的更多相关文章
- android中实现跑马灯效果以及AutoCompleteTestView与MultiAutoCompleteTextView的学习
跑马灯效果 1.用过属性的方式实现跑马灯效果 属性: android:singleLine="true" 这个属性是设置TextView文本中文字 ...
- TextView的跑马灯效果(AS开发实战第二章学习笔记)
TextView的跑马灯效果跑马灯用到的属性与方法说明singleLine 指定文本是否单行显示ellipsize 指定文本超出范围后的省略方式focusable 指定是否获得焦点,跑马灯效果要求设置 ...
- WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果 本以为Label也有TextChanged 事件,但在使 ...
- Android开发:文本控件详解——TextView(二)文字跑马灯效果实现
一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...
- Android_TextView之跑马灯效果
对于android控件中的TextView,相信大家一定不陌生,在显示文本内容时十分方便.不过我在使用时遇到一个小问题,就是当文字交多时,如何为用户进行展示.今天就为大家介绍一种解决方案--跑马灯效果 ...
- Android 实现多行文本跑马灯效果
Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了. android:ellipsize="marquee" android:focusable=&q ...
- android:ellipsize实现跑马灯效果总结(转)
最近无意间看到了涉及到跑马灯效果的代码,于是在网上查阅了很多资料,在这里对自己看的一些文章进行一下总结,顺便加上自己的一些体会. 让我们一步步逐渐向下. 首先我们要实现走马灯这样一个效果,通常来说 ...
- flex 简单跑马灯效果(竖着显示)
<mx:Move id="move_area" target="{VBox_AreaWarning}"/> //move效果,模拟跑马灯 <s ...
- Dom操作--跑马灯效果
这里给园友们演示的是Dom操作实现跑马灯效果,相信我们很多人都用Winform实现过跑马灯效果,其中的关键就是Tirm控件,那么在Dom操作中是用setInterval方法来实现隔一段时间执行一段代码 ...
随机推荐
- linux下FAT32格式u盘只读的问题及解决方法
以下是网上看到的解决办法:http://blog.csdn.net/heqiuya/article/details/7870554 其实是掉电保护,之前挂在的SD变成了制度文件,只需要将SD卡重新挂载 ...
- 【29.27%】【hdu 5908】Abelian Period
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) 问题描述 设SS是一个数字串,定义 ...
- 制作Kinect体感控制小车教程 <一>
转载请注明出处:http://blog.csdn.net/lxk7280 Kinect体感控制小车 Kine ...
- 最简单的基于FFmpeg的移动端样例:IOS HelloWorld
===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...
- [SCSS] Write Custom Functions with the SCSS @function Directive
Writing SCSS @functions is similar to writing functions in other programming languages; they can acc ...
- [Angular] Providers and useFactory
// service.ts import { Injectable, Inject } from '@angular/core'; import { Http } from '@angular/htt ...
- Make chrome extension
How to Make a Chrome Extension. https://robots.thoughtbot.com/how-to-make-a-chrome-extension Skip to ...
- 【u010】银河英雄传说
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开 ...
- [javase学习笔记]-6.5 类类型參数与匿名对象
这一节我们来说说类类型參数和匿名对象. 我们继续用之前的小汽车类吧 class Car { int num;//这是轮胎数属性 String color;//这是颜色属性 String brand;/ ...
- UVA 10561 - Treblecross(博弈SG函数)
UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...