原文:WPF 走马灯 文字滚动 自定义控件

    /// <summary>
/// Label走马灯自定义控件
/// </summary>
[ToolboxBitmap(typeof(Label))] //设置工具箱中显示的图标
public class ScrollingTextControl : 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 = ; DateTime _SignTime;
bool _IfFirst = true; /// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留3秒
/// </summary>
int _StopSecond = ; /// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留3秒
/// </summary>
public int StopSecond
{
get { return _StopSecond; }
set
{
_StopSecond = 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;
}
} /// <summary>
/// 构造函数
/// </summary>
public ScrollingTextControl()
{
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 (string.IsNullOrEmpty(_OutText)) return; if (_OutText.Substring() + _OutText[] == _TempString)
{
if (_IfFirst)
{
_SignTime = DateTime.Now;
} if ((DateTime.Now - _SignTime).TotalMilliseconds > _StopSecond)
{
_IfFirst = true; ;
}
else
{
_IfFirst = false;
return;
}
} _OutText = _OutText.Substring() + _OutText[]; Dispatcher.BeginInvoke(new Action(() =>
{
SetContent = _OutText;
})); } }

以上放到cs文件中 然后 编译 就可以在工具箱中看到ScrollingTextControl 名称的label控件

<Window x:Class="WpfDemoNew.Window21"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window21" Height="300" Width="300" xmlns:my="clr-namespace:WpfDemoNew.Control">
<Grid x:Name="grid">
<my:ScrollingTextControl Content="12345" Height="52" HorizontalAlignment="Left" Margin="10,10,0,0" x:Name="scrollingTextControl1" VerticalAlignment="Top" Width="121" Foreground="Black" />
</Grid>
</Window>

WPF 走马灯 文字滚动 自定义控件的更多相关文章

  1. 单行文字滚动就用myslider

    单行文字滚动就用myslider,myslider是一个小型的内容滚动jquery插件. 首先请看实例:http://keleyi.com/jq/myslider/demo/4.htm 然后来看代码: ...

  2. winfrom 文字滚动

    winfrom 文字滚动 http://www.codeproject.com/Articles/6913/Creating-a-professional-looking-GDI-drawn-cust ...

  3. marquee 标签 文字滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. js鼠标经过文字滚动,移开还原

    不说别的,直接贴代码. <div class="kj-scroll" id="countrylist0" onmouseover="wPAa = ...

  5. marquee|各种文字滚动代码(适用公告)

    marquee|各种文字滚动代码(适用公告)  

  6. Unity3D 文字滚动跑马灯效果

    需求 在日常游戏中,文字滚动效果是比较常用的.例如日常游戏顶部的新闻公告,聊天系统的文字滚动,都属于这个范围. 思路 由于使用的地方比较广泛,所以希望能够尽量独立的游戏之外,能够做到随处使用的功能.N ...

  7. WPF checkbox文字下掉

    WPF checkbox文字下掉 可以使用 <Style TargetType="CheckBox"> <Setter Property="Margin ...

  8. section标签实现文字滚动

    h5新增样式 section标签 使用demo //h5新增属性 h5新增滚动的标签 <marquee> <div style="padding-left: 20px;ma ...

  9. HTML之marquee(文字滚动)详解

    语法: <marquee></marquee> 以下是一个最简单的例子: 代码如下: <marquee><font size=+3 color=red> ...

随机推荐

  1. 【42.86%】【Codeforces Round #380D】Sea Battle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. QMap 的增删改查

    map 是一种数据容器,它提供一种由key 到 value 的映射.map 的key 是唯一的, 也是有序的.map 通常由近似平衡的红黑树来实现.key 的有序性,使得插入,查找节点比较有效.map ...

  3. 数据类型总结——Array(数组类型)

    相关文章 简书原文:https://www.jianshu.com/p/1e4425383a65 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...

  4. SAP ABAP编程 字符串加密-MD5_CALCULATE_HASH_FOR_CHAR

    DATA: str1 TYPE c LENGTH 12 VALUE 'zxcv',       str2 TYPE c LENGTH 32,       str3 TYPE c LENGTH 32. ...

  5. Best Practices for QML and Qt Quick

    Despite all of the benefits that QML and Qt Quick offer, they can be challenging in certain situatio ...

  6. C# datatable增加行(datarow)数据为另一个datatable中某行

    两个表A和B,两表结构相同.现在需要将A表中部分行拷贝到B表中,直接用 DataTableB.rows.add(dataTableA.rows[0]) 这样的方法式会报"row已经属于A表& ...

  7. js进阶正则表达式11RegExp的属性和方法(RegExp的属性和方法,就是RegExp对象.(点)什么的形式)(正则表达式执行之前会被编译)

    js进阶正则表达式11RegExp的属性和方法(RegExp的属性和方法,就是RegExp对象.(点)什么的形式)(正则表达式执行之前会被编译) 一.总结 1. RegExp的属性和方法,就是RegE ...

  8. Django日志器的使用

    Logging Mudel A quick logging primer Django uses Python’s builtin logging module to perform system l ...

  9. Spark源代码阅读笔记之DiskStore

    Spark源代码阅读笔记之DiskStore BlockManager底层通过BlockStore来对数据进行实际的存储.BlockStore是一个抽象类,有三种实现:DiskStore(磁盘级别的持 ...

  10. UItextfield 动态限制输入的字数

    @property (nonatomic, strong) UITextField *txtName; - (void)viewDidLoad { [super viewDidLoad]; //UIC ...