完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LabelTextAutoScrollShow
{
[ToolboxBitmap(typeof(Label))]
public class LabelTextAutoScrollShow : Label
{
/// <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 = 100;
DateTime _SignTime;
bool bFirst = true;
bool _IfFirst = true;
/// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留1秒
/// </summary>
public int _StopSecond = 2000;
/// <summary>
/// 是否需要滚动
/// </summary>
public bool _BScroll = false;
/// <summary>
/// 开始滚动时延迟的时间
/// </summary>
public int _DelayTime = 2000;
/// <summary>
/// 开始滚动时延迟的时间,单位为毫秒,默认值延迟2秒
/// </summary>
[Description("开始滚动时延迟的时间")]
public int DelayTime
{
get { return _DelayTime; }
set
{
_DelayTime = value;
}
}
/// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留3秒
/// </summary>
public int StopSecond
{
get { return _StopSecond; }
set
{
_StopSecond = value;
}
}
/// <summary>
/// 文字是否滚动
/// </summary>
[Description("文字是否滚动")]
public bool BScroll
{
get { return _BScroll; }
set
{
_BScroll = value;
}
}
/// <summary>
/// 滚动的速度
/// </summary>
[Description("文字滚动的速度")] //显示在属性设计视图中的描述
public double RunSpeed
{
get { return _RunSpeed; }
set
{
_RunSpeed = value;
MarqueeTimer.Interval = _RunSpeed;
}
}
/// <summary>
/// 滚动文字源
/// </summary>
[Description("文字滚动的Text")]
public string TextSource
{
get { return _TextSource; }
set
{
_TextSource = value;
_TempString = _TextSource + " ";
_OutText = _TempString;
}
}
private string SetContent
{
get { return Content.ToString(); }
set
{
Content = value;
}
}
public LabelTextAutoScrollShow()
{
// DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
MarqueeTimer.Interval = _RunSpeed;//文字移动的速度
MarqueeTimer.Enabled = true; //开启定时触发事件
MarqueeTimer.Elapsed += new ElapsedEventHandler(MarqueeTimer_Elapsed);//绑定定时事件
this.Loaded += new RoutedEventHandler(ScrollingTextControl_Loaded);//绑定控件Loaded事件
}
void ScrollingTextControl_Loaded(object sender, RoutedEventArgs e)
{
_TextSource = SetContent;
_TempString = _TextSource + " ";
_OutText = _TempString;
_SignTime = DateTime.Now;
}
void MarqueeTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!_BScroll) return;
if (bFirst && (DateTime.Now - _SignTime).TotalMilliseconds < _DelayTime)
{
return;
}
else
{
bFirst = false;
}
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;
}));
}
}
}
详细操作流程
首先参照我使用自定义控件库CustomControl实现控件在其他项目使用的博文新建一个WPF自定义控件库的项目。
然后点击那个CS文件,把以上代码直接拷贝进去全部替换掉原来的内容。接着把CS文件名改为LabelTextAutoScrollShow。然后在Generic.xaml文件中修改如下图所示的对应名称。
修改对应的三处就可以。改好了你就已经拥有自己的项目了。接下来就按使用自定义控件库CustomControl实现控件在其他项目使用的博文操作就可以在对应的你自己的项目中使用这个控件了。