原文: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  表示感谢

最新文章

  1. 个人对RCU的理解
  2. JMeter学习-026-JMeter 分布式(远程)参数化测试实例
  3. MySQL出错:ERROR 1045 (28000)的解决方法
  4. display:none,overflow:hidden,visibility:hidden之间的区别
  5. Python学习教程(learning Python)--2.2.2 Python全局和局部变量
  6. 转:45 Useful JavaScript Tips, Tricks and Best Practices
  7. 学习笔记——访问者模式Visitor
  8. pageRank算法 python实现
  9. 文件操作命令(del)
  10. 获取json串里的某个属性值
  11. Eclipse 00: 安装教程 + 汉化 + 简单创建java项目
  12. 解决eclipse修改后台代码ctrl+s总是【自动重启服务器】问题
  13. @Scheduler与cron
  14. Leaflet_扩展Leaflet:类(2017-10-26)
  15. CSS-下拉导航条
  16. ios开发之-- tableview/collectionview获取当前点击的cell
  17. 十年前,女:“对不起,我不会喜欢你的,你不要再坚持了,就好比让 Linux 和 Windows 同时运行在一台PC机上,可能吗?
  18. from __future__ import print_function
  19. [C++] CONST 2
  20. Request常用方法

热门文章

  1. LDR: LdrpWalkImportDescriptor() failed to probe C:\WINDOWS\system32\opencv_core243d.dll for its manifest, ntstatus 0xc0150002
  2. [React] Use React.cloneElement to Modify and Add Additional Properties to React Children
  3. 从源码角度实现一个自己的Promise
  4. 【u108】取数游戏
  5. Android 从硬件到应用:一步一步向上爬 4 -- 使用 JNI 方法调硬件驱动
  6. sourceinsight4
  7. 使用Html5开发Android和iOS应用:HBuilder、Html5Plus、MUI
  8. [Angular] Ngrx/effects, Action trigger another action
  9. 《图说VR》——HTC Vive控制器按键事件解耦使用
  10. java线程池框架源代码分析