C#实现走马灯状态栏

注意:
Invalidate();不会立即执行;
Invalidate(Rectangle r);会立即执行
//
        // 摘要:
        //     使 System.Windows.Forms.ToolStripItem 的整个图面无效并导致重绘该图面。
        public void Invalidate();
        //
        // 摘要:
        //     通过将 System.Windows.Forms.ToolStripItem 的指定区域添加到 System.Windows.Forms.ToolStripItem
        //     的更新区域(即下次执行绘制操作时将重新绘制的区域)使该指定区域无效,并导致向 System.Windows.Forms.ToolStripItem
        //     发送绘制消息。
        //
        // 参数:
        //   r:
        //     一个 System.Drawing.Rectangle,表示要使之无效的区域。
        public void Invalidate(Rectangle r);

    public class XToolStripMovableTextLabel : System.Windows.Forms.ToolStripStatusLabel
    {
        #region 构造和析构

        /// <summary>
        /// 构造
        /// </summary>
        public XToolStripMovableTextLabel()
            : base()
        {
            m_Timer = new System.Windows.Forms.Timer();
            m_Timer.Interval = 30;
            m_Timer.Enabled = true;
            m_Timer.Tick += new EventHandler(m_Timer_Tick);

            m_textRectangle = new System.Drawing.Rectangle();
            m_textRectangle.X = m_x;
            m_textRectangle.Width = this.Width;
            m_textRectangle.Height = this.Height;
            //m_x = 0 - m_textSize.Width;
        }

        void m_Timer_Tick(object sender, EventArgs e)
        {
            // 如果文本宽度小于控件宽度,不滚动显示
            if (m_textSize.Width < this.Width)
            {
                m_Timer.Enabled = false;
                return;
            }
            // 计算滚动显示的文本位置
            m_x--;
            if (m_x < 0 - this.m_textSize.Width)
            {
                m_x = 0 + this.Width;
            }
            m_textRectangle.X = m_x;
            this.Invalidate(m_textRectangle);
            //this.Invalidate();
            //System.Windows.Forms.Application.DoEvents();
        }

        #endregion // 构造和析构

        #region 公有方法
        #endregion // 公有方法

        #region 保护方法

        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            //base.OnPaint(e);
            m_textSize = e.Graphics.MeasureString(this.Text, this.Font);
            m_textRectangle.Width = (int)m_textSize.Width;
            m_textRectangle.Height = this.Height;

            e.Graphics.DrawString(this.Text, this.Font, System.Drawing.Brushes.Black, m_textRectangle);
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                if (m_textSize.Width > this.Width)
                {
                    m_Timer.Enabled = true;
                }
            }
        }

        #endregion // 保护方法

        #region 私有方法
        #endregion // 私有方法

        #region 属性及其私有变量

        /// <summary>
        /// 计时器
        /// </summary>
        private System.Windows.Forms.Timer m_Timer = null;

        /// <summary>
        /// 文字的大小
        /// </summary>
        private System.Drawing.SizeF m_textSize;

        /// <summary>
        /// 文本显示矩形
        /// </summary>
        private System.Drawing.Rectangle m_textRectangle;

        /// <summary>
        /// 文本的起始位置
        /// </summary>
        private int m_x = 0;
        #endregion // 属性及其私有变量
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值