滚动文本的源代码

/// /// 滚动文本条。 /// /// [DefaultProperty("Text")] [ToolboxBitmap(typeof(ScrollText),"Icon2.ico")] public class ScrollText : Control { #region 枚举值 /// /// 文本的滚动速度 /// public enum ScrollSpeed { /// /// 正常速度 /// Normal, /// /// 较快速度 /// Fast, /// /// 较慢速度 /// Slow, /// /// 不滚动 /// NoSroll } /// /// 文本的滚动样式 /// public enum ScrollStyle { /// /// 只在文本框内滚动 /// Alternate, /// /// 循环滚动 /// Scroll } /// /// 文本的滚动方向 /// public enum ScollOrientation { /// /// 向左滚动 /// Left, /// /// 向右滚动 /// Right, /// /// 向上滚动 /// Up, /// /// 向下滚动 /// Down } public enum ControlBorderStyle { /// /// 指定由画线段组成的直线. /// Dash, /// /// 指定由重复的画线点图案构成的直线. /// DashDot, /// /// 指定由重复的画线点点图案构成的直线 /// DashDotDot, /// /// 指定由点构成的直线 /// Dot, /// /// 指定实线 /// Solid, /// /// 没有三维效果 /// None, /// /// 该边框具有凸起的内外边缘。 /// Raised, /// /// 该边框具有凹下的内外边缘。 /// Sunken } #endregion #region 私有变量 /// /// 在文本框内左右或者上下循环移动时的标志 /// private bool m_flags = false; /// /// 判断是构造还是循环样式变化所用的变量 /// private bool m_coucotor = false; /// /// 定时器,用于滚动文本 /// private Timer tim; /// /// 判断滚动速度 /// private ScrollSpeed speed; /// /// 判断滚动方式 /// private ScrollStyle style; /// /// 判断滚动样式 /// private ScollOrientation orientation; /// /// 滚动位置 /// private int moveX = 0; private int moveY = 0; /// /// 文本的宽度和高度 /// private int textWidth = 0; private int textHeight = 0; /// /// 控件外观的矩形 /// private Rectangle rect ; private Rectangle frameRect ; private bool m_mouseStop = false; /// /// 边框样式 /// private ControlBorderStyle m_borderStyle; /// /// 绘制边框色 /// private Color m_borderColor = Color.Black; #endregion #region 构造器 public ScrollText() { base.BackColor = SystemColors.Window; m_borderStyle = ControlBorderStyle.Sunken; //默认滚动样式 speed = ScrollSpeed.Normal; style = ScrollStyle.Scroll; orientation = ScollOrientation.Left; //通过定时器实现滚动 tim = new Timer(); tim.Enabled = false; tim.Interval = 200; tim.Tick +=new EventHandler(timer1_Tick); //绘制控件的方法 this.SetStyle(ControlStyles.SupportsTransparentBackColor,true); this.SetStyle(ControlStyles.ResizeRedraw,true); this.SetStyle(ControlStyles.Selectable, true); // 启用双缓冲 this.SetStyle(ControlStyles.DoubleBuffer,true); this.SetStyle(ControlStyles.AllPaintingInWmPaint,true); this.SetStyle(ControlStyles.UserPaint,true); } #endregion #region 属性 /// /// 获取或设置边框样式 /// /// [System.ComponentModel.DisplayName("边框样式"),Description("滚动文本的边框样式")] [DefaultValue(ControlBorderStyle.Sunken)] public ControlBorderStyle BorderStyle { get { return this.m_borderStyle; } set { if(!Enum.IsDefined(typeof(ControlBorderStyle),value)) { throw new Exception("不支持的三维边框样式"); } this.m_borderStyle = value; } } /// /// 获取或设置边框色 /// /// [System.ComponentModel.DisplayName("边框颜色"),Description("控件的边框颜色")] public Color BorderColor { get { return m_borderColor; } set { m_borderColor = value; } } /// /// 获取或设置是否支持鼠标放置在文本框上时停止滚动 /// [DisplayName("鼠标悬停"),Description("鼠标悬停在文本上时是否停止滚动")] [DefaultValue(false)] public bool ScrollStop { get { return m_mouseStop; } set { m_mouseStop = value; } } /// /// 文本的滚动速度 /// /// [System.ComponentModel.DisplayName("滚动速度"),Description("文本的滚动速度")] [DefaultValue(ScrollSpeed.Normal)] public ScrollSpeed Speed { get { return this.speed; } set { if (!Enum.IsDefined(typeof(ScrollSpeed),value)) { throw new ArgumentException("不支持的显示样式"); } this.speed = value; switch(speed) { case ScrollSpeed.Normal: this.tim.Enabled = true; this.tim.Interval = 200; break; case ScrollSpeed.NoSroll: moveX = frameRect.X; moveY = frameRect.Height/2 - textHeight/2; this.tim.Enabled = false; break; case ScrollSpeed.Slow: this.tim.Enabled = true; this.tim.Interval = 400; break; case ScrollSpeed.Fast: this.tim.Enabled = true; this.tim.Interval = 100; break; } this.Invalidate(); } } /// /// 获取或设置文本的滚动样式 /// /// [System.ComponentModel.DisplayName("滚动方式"),Description("文本的滚动方式")] [DefaultValue(ScrollStyle.Scroll)] public ScrollStyle Dehavior { get { return this.style; } set { if (!Enum.IsDefined(typeof(ScrollStyle),value)) { throw new ArgumentException("不支持的显示样式"); } this.style = value; //初始化移动位置 moveX = frameRect.X; moveY = frameRect.Height/2 - textHeight/2; this.Invalidate(); } } /// /// 获取或设置文本的滚动方向 /// [DisplayName("滚动方向"),Description("文本的滚动方向")] [DefaultValue(ScollOrientation.Left)] public ScollOrientation Direction { get { return orientation; } set { if(!Enum.IsDefined(typeof(ScollOrientation),value)) { throw new ArgumentException("不支持的显示样式"); } this.orientation = value; //当为不滚动时才可以设置方向 if(speed == ScrollSpeed.NoSroll) { return ; } switch(orientation) { case ScollOrientation.Left: moveX = frameRect.Width - textWidth; moveY = frameRect.Height/2 - textHeight/2; break; case ScollOrientation.Right: moveX = frameRect.X; moveY = frameRect.Height/2 - textHeight/2; break; case ScollOrientation.Up: moveX = frameRect.X; moveY = frameRect.Height - textHeight; break; case ScollOrientation.Down: m_coucotor = true; moveX = frameRect.X; moveY = frameRect.Y; break; } this.Invalidate(); } } /// /// 获取或设置控件文本 /// [Editor(typeof(System.ComponentModel.Design.MultilineStringEditor),typeof(UITypeEditor))] public new string Text { get { return base.Text; } set { base.Text = value; } } /// /// 控制控件的缺省大小 /// protected override Size DefaultSize { get { return new Size(150,21); } } #endregion #region 方法和事件 /// /// 鼠标进入事件 /// /// protected override void OnMouseEnter(EventArgs e) { if(m_mouseStop) { this.tim.Enabled = false; } else { if(this.speed !=ScrollSpeed.NoSroll) { this.tim.Enabled =true; } } base.OnMouseEnter (e); } /// /// 鼠标离开事件 /// /// protected override void OnMouseLeave(EventArgs e) { if(this.speed !=ScrollSpeed.NoSroll) { this.tim.Enabled = true; } base.OnMouseLeave (e); } /// /// 字体改变事件 /// /// protected override void OnFontChanged(EventArgs e) { base.OnFontChanged (e); this.Invalidate(); } /// /// 当控件大小变化的时候保证文本在中间 /// /// 绘制参数 protected override void OnSizeChanged(EventArgs e) { if(speed == ScrollSpeed.NoSroll) { rect = this.ClientRectangle; frameRect = new Rectangle(rect.X,rect.Y,rect.Width - 1,rect.Height - 1); moveX = frameRect.X; moveY = frameRect.Height/2 - textHeight/2; } if(this.Height < 21) { this.Height = 21; } this.Invalidate(); } /// /// 绘制文本外观 /// /// protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); //当字体的高度过大时,加长控件的高度 if(textHeight > this.Height) { this.Height = textHeight; } this.DrawApplearance(e,m_borderColor); } /// /// 绘制文本的外观样式 /// /// /// 边框色 private void DrawApplearance(PaintEventArgs e,Color c) { //绘制边框 rect = this.ClientRectangle; frameRect = new Rectangle(rect.X,rect.Y,rect.Width - 1,rect.Height - 1); Pen borderPen = new Pen(c); //绘制边框3D样式 switch(m_borderStyle) { case ControlBorderStyle.Dash: borderPen.DashStyle = DashStyle.Dash; break; case ControlBorderStyle.DashDot: borderPen.DashStyle = DashStyle.DashDot; break; case ControlBorderStyle.DashDotDot: borderPen.DashStyle = DashStyle.DashDotDot; break; case ControlBorderStyle.Dot: borderPen.DashStyle = DashStyle.Dot; break; case ControlBorderStyle.None: borderPen = new Pen(Color.Empty); break; case ControlBorderStyle.Solid: borderPen.DashStyle = DashStyle.Solid; break; case ControlBorderStyle.Raised: borderPen = new Pen(Color.Empty); ControlPaint.DrawBorder3D(e.Graphics,rect,Border3DStyle.Raised); break; case ControlBorderStyle.Sunken: borderPen = new Pen(Color.Empty); ControlPaint.DrawBorder3D(e.Graphics,rect,Border3DStyle.Sunken); break; } e.Graphics.DrawRectangle(borderPen,frameRect); borderPen.Dispose(); //绘制文本 textWidth = (int)e.Graphics.MeasureString(this.Text,this.Font).Width; textHeight = (int)e.Graphics.MeasureString(this.Text,this.Font).Height; if(moveY == 0 && !m_coucotor) { moveY = frameRect.Height/2 - textHeight/2; } Brush brushText = new SolidBrush(this.ForeColor); RectangleF rectText = new RectangleF(moveX,moveY,this.Width -2 ,this.Height - 2); e.Graphics.DrawString(this.Text,this.Font,brushText,rectText); brushText.Dispose(); } #endregion #region Tick事件 //定时器处理滚动 private void timer1_Tick(object sender, EventArgs e) { //判断滚动方向 if(speed == ScrollSpeed.NoSroll) { return ; } switch(orientation) { case ScollOrientation.Left://向右滚动 //保持垂直方向不变 if(style == ScrollStyle.Scroll) { moveY = frameRect.Height/2 - textHeight/2; //移动水平方向直到文本消失后从右边绘出文本 //完成文本从右到左的滚动 if(moveX > frameRect.X - textWidth) { moveX = moveX -5; } else { moveX = frameRect.X + frameRect.Width; } } else { moveY = frameRect.Height/2 - textHeight/2; if(moveX < frameRect.Width - textWidth && !m_flags) { moveX = moveX + 5; } if(moveX >= frameRect.Width - textWidth) { this.m_flags = true; } if(moveX >frameRect.X && m_flags) { moveX = moveX - 5; } if(moveX <=frameRect.X) { this.m_flags = false; } } break; case ScollOrientation.Right://向左滚动 if(style == ScrollStyle.Scroll) { moveY = frameRect.Height/2 - textHeight/2; if(moveX < frameRect.Width) { moveX = moveX +5; } else { moveX = frameRect.X - textWidth; } } else { moveY = frameRect.Height/2 - textHeight/2; if(moveX < frameRect.Width - textWidth && !m_flags) { moveX = moveX + 5; } if(moveX >= frameRect.Width - textWidth) { this.m_flags = true; } if(moveX >frameRect.X && m_flags) { moveX = moveX - 5; } if(moveX <=frameRect.X) { this.m_flags = false; } } break; case ScollOrientation.Up://向下滚动 if(style == ScrollStyle.Scroll) { moveX = frameRect.X;//保持水平方向不变 if(moveY > frameRect.X - textHeight) { moveY = moveY - 5; } else { moveY = frameRect.Y + frameRect.Height; } } else { moveX = frameRect.X; if(moveY < frameRect.Height - textHeight && !m_flags) { moveY = moveY + 5; } if(moveY >= frameRect.Height - textHeight) { this.m_flags = true; } if(moveY >frameRect.Y && m_flags) { moveY = moveY - 5; } if(moveY <=frameRect.Y) { this.m_flags = false; } } break; case ScollOrientation.Down://向上滚动 if(style == ScrollStyle.Scroll) { moveX = frameRect.X; if(moveY < frameRect.Height) { moveY = moveY + 5; } else { moveY = frameRect.Y -textHeight; } } else { moveX = frameRect.X; if(moveY < frameRect.Height - textHeight && !m_flags) { moveY = moveY + 5; } if(moveY >= frameRect.Height - textHeight) { this.m_flags = true; } if(moveY >frameRect.Y && m_flags) { moveY = moveY - 5; } if(moveY <=frameRect.Y) { this.m_flags = false; } } break; } //使控件失效,重新绘制控件 this.Invalidate(); } #endregion }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值