原文: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. 【例题3-3 UVA - 401】Palindromes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果一个字符没有对应的镜像,那么它对应的是一个空格. 然后注意 aba这种情况. 这种情况下b也要查一下它的镜像是不是和b一样. [ ...

  2. [Javascript Natural] Break up language strings into parts using Natural

    A part of Natural Language Processing (NLP) is processing text by “tokenizing” language strings. Thi ...

  3. [CSS] Dynamically Size Elements with Pure CSS

    Learn how to size elements based on the dimensions of the viewport, even when the browser is resized ...

  4. update中加入select最常用的update语法

    update中加入select最常用的update语法 (转) (2010-08-20 11:40:16) 转载▼ 标签: it 分类: SQL 最常用的update语法是:UPDATE <ta ...

  5. js进阶 9-15 多选框如何限制选中数目

    js进阶 9-15 多选框如何限制选中数目 一.总结 一句话总结: 1.多选框如何限制选中数目? 没点击选择一次,来统计现在总共选了多少个,如果超出,就给onclick事件返回false,取消oncl ...

  6. ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案

    原文:ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不大 ...

  7. 使用jsp,tomcat实现用户登录注册留言的代码

    以下jsp中,未使用样式表对网页进行排版和表单的验证(每个jsp的表单填写的时候应该进行空值与空格的验证,防止提交时出错) 所有错误,链接到error.jsp <%@ page language ...

  8. Xor - Trie树

    题目描述 求一棵带边权的树的一条最大 Xor 路径的值.这里的"路径"不一定从根到叶子结点,中间一段路径只要满足条件也可以. 输入格式 第一行,一个整数 N ,表示一颗树有 N 个 ...

  9. Oracle数据库零散知识02

    15,函数的创建,要求必须有返回值,必须在语句中调用,需要多个返回值时,使用out参数类型,在user_procedures表中查询属性,在user_source表中查询源代码,创建示例: CREAT ...

  10. 【noip模拟】德充符

    时间限制:2s 内存限制:512MB [题目描述] 申徒嘉和郑子产都是伯昏无人的学生,子产因为申徒嘉是残疾人,非常看不起他,于是想要刁难他. 子产给了申徒嘉 n个数 a1,a2...an. 现在他要求 ...