最近因项目需要,需在winform中实现文字从右向左,从下向上,滚动效果,其中试过在PictureBox,RichTexBox,Label组件中实现文字滚动,发现Label中效果最好,PictureBox次之,RichTexBox中不能实现。
现分享如下:Label控件中实现从下向上滚动效果,PictureBox组件中实现从右向左滚动效果,winform工程中添加Label,PictureBox组件,各创建一个Label,PictureBox的继承类,实现如下:
Label继承类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace gundong
{
class NewLabel : Label
{
public NewLabel()
{
this.updateTimer = new Timer();
this.updateTimer.Interval = 30;
this.updateTimer.Enabled = true;
this.updateTimer.Tick += delegate { this.OnTick(); };
//drawRect.X = x;
drawRect.X = this.width + stringSize.Width;
drawRect.Y = y;
drawRect.Width = width;
//drawRect.Height = height;
drawRect.Height = GetHeight();
//x = 0 - stringSize.Width;
//x = this.width + stringSize.Width;
}
private Timer updateTimer;
String drawString = "文字很长时,就会超出这个矩形框,我想实现的功能是。我给你看看。。尽量不要在重绘的时候赋图片。重绘一般都画图神马的。。程序写好了不会导致闪烁的。状态栏走马灯似的移动文本,重绘代码发上来。我给你看看。";
Font drawFont = new Font("宋体", 12);
SizeF stringSize = new SizeF();
SolidBrush drawBrush = new SolidBrush(Color.Black);
float x = 0;
float y = 200;
//float width = 562;
float width = 283;
float height = 90;
RectangleF drawRect = new RectangleF();
//从左到右
// pri