原文: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控件,数据绑定方式实现的更多相关文章

  1. android中实现跑马灯效果以及AutoCompleteTestView与MultiAutoCompleteTextView的学习

    跑马灯效果 1.用过属性的方式实现跑马灯效果 属性:                  android:singleLine="true" 这个属性是设置TextView文本中文字 ...

  2. TextView的跑马灯效果(AS开发实战第二章学习笔记)

    TextView的跑马灯效果跑马灯用到的属性与方法说明singleLine 指定文本是否单行显示ellipsize 指定文本超出范围后的省略方式focusable 指定是否获得焦点,跑马灯效果要求设置 ...

  3. WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果

    原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果   本以为Label也有TextChanged 事件,但在使 ...

  4. Android开发:文本控件详解——TextView(二)文字跑马灯效果实现

    一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...

  5. Android_TextView之跑马灯效果

    对于android控件中的TextView,相信大家一定不陌生,在显示文本内容时十分方便.不过我在使用时遇到一个小问题,就是当文字交多时,如何为用户进行展示.今天就为大家介绍一种解决方案--跑马灯效果 ...

  6. Android 实现多行文本跑马灯效果

    Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了. android:ellipsize="marquee" android:focusable=&q ...

  7. android:ellipsize实现跑马灯效果总结(转)

      最近无意间看到了涉及到跑马灯效果的代码,于是在网上查阅了很多资料,在这里对自己看的一些文章进行一下总结,顺便加上自己的一些体会. 让我们一步步逐渐向下. 首先我们要实现走马灯这样一个效果,通常来说 ...

  8. flex 简单跑马灯效果(竖着显示)

    <mx:Move id="move_area" target="{VBox_AreaWarning}"/> //move效果,模拟跑马灯 <s ...

  9. Dom操作--跑马灯效果

    这里给园友们演示的是Dom操作实现跑马灯效果,相信我们很多人都用Winform实现过跑马灯效果,其中的关键就是Tirm控件,那么在Dom操作中是用setInterval方法来实现隔一段时间执行一段代码 ...

随机推荐

  1. shiro实现登录安全认证(转)

    shiro实现登录安全认证 shiro的优势,不需要再代码里面判断是否登录,是否有执行的权限,实现了从前端页面到后台代码的权限的控制非常的灵活方便 传统的登录认证方式是,从前端页面获取到用户输入的账号 ...

  2. crontab经验 分类: B3_LINUX 2015-03-06 11:17 282人阅读 评论(0) 收藏

    1.基本格式  第1列分钟1-59  第2列小时1-23(0表示子夜)  第3列日1-31  第4列月1-12  第5列星期0-6(0表示星期天)  第6列要运行的命令 2.关于日志 (1)基本日志位 ...

  3. Intellij IDEA中使用Debug

    Intellij IDEA中使用Debug Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行过程中参数的变化.通常我们也可以启用 ...

  4. 使用ionic3快速开发webapp(一)

    Ionic可以让我们使用web技术快速构建接近原生体验的跨平台移动应用. 一.安装ionic 1.需要先安装 Node.js(版本8.x之上): 2.安装cordova 和 ionic: $ npm ...

  5. asm 的hello world 2011.04.28

    这几天一直在弄一个嵌入式的程序,搭环境,熟悉库函数,熟悉汇编,乱成一锅粥,到现在还是没有什么系统性的收获. 或许下周弄出来吧,(一定得弄出来,不然老大该跟我急了……). 今天,熟悉汇编,好歹用汇编写出 ...

  6. JAVA SkipList 跳表 的原理和使用例子

    跳跃表是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间),并且对并发算法友好. 关于跳跃表的具体介绍可以参考MIT的公开课:跳跃表 跳跃表的应 ...

  7. css3-5 css3鼠标、列表和尺寸样式怎么用(文字有关的样式会被继承)

    css3-5  css3鼠标.列表和尺寸样式怎么用(文字有关的样式会被继承) 一.总结 一句话总结:css标签中文字有关的样式会被继承.由常用样式记起. 1.鼠标常用样式有哪些? cursor:poi ...

  8. 【Windows Defender Antivirus Service 永久禁用 】

    cmd 管理员运行 执行 reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender” /v “DisableAn ...

  9. SpringMVC中跳转路径的问题

    1. @RequestMapping 1)@RequestMapping既可以作用于类,也可以作用于方法 2)@RequestMapping中value值(即跳转的路径),可以加 "/&qu ...

  10. Live Unit Testing

    Live Unit Testing 相对于传统的Unit Test,VS2017 带来了一个新的功能,叫Live Unit Testing,从字面意思理解就是实时单元测试,在实际的使用中,这个功能就是 ...