winform滚动字幕
来源:互联网 发布:网络架构是什么 编辑:程序博客网 时间:2024/06/10 03:45
最近因项目需要,需在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();
//从左到右
// private void OnTick()
// {
// x++;
// if (x > this.width + stringSize.Width)
// x = 0 - stringSize.Width;
// drawRect.X = x;
// this.Invalidate();
//}
//从右到左
private void OnTick()
{
//x--;
y--;
if (y < -drawRect.Height)
y = 0 + 200 /*+ stringSize.Width*/;
drawRect.X = x;
drawRect.Y = y;
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
stringSize = e.Graphics.MeasureString(drawString, drawFont);
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), drawRect.X, drawRect.Y, drawRect.Width, drawRect.Height);
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
base.OnPaint(e);
}
private float GetHeight()
{
Graphics g = this.CreateGraphics();
Font font = new Font("宋体", 12);
//设方框固定为600
SizeF size = new SizeF(283, float.MaxValue);
//打印的起点
PointF p = new PointF(10, 10);
int charNum, lineNum;
float height = 0;
//测量需要画几行
g.MeasureString(drawString, font, size, StringFormat.GenericTypographic, out charNum, out lineNum);
//获取字体的高度
float fontHeight = font.GetHeight(g);
//size = new SizeF(600, lineNum * fontHeight);
//g.DrawRectangle(Pens.Red, 10, 10, size.Width, size.Height);
//g.DrawString(content, font, Brushes.Blue, new RectangleF(p, size), StringFormat.GenericTypographic);
height = (lineNum+1) * fontHeight;
return height;
}
}
}
PictureBox继承类:
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 NewPictureBox : System.Windows.Forms.PictureBox
{
public NewPictureBox()
{
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;
//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 = 2;
//float width = 562;
float width = 210;
float height = 17;
RectangleF drawRect = new RectangleF();
//从左到右
// private void OnTick()
// {
// x++;
// if (x > this.width + stringSize.Width)
// x = 0 - stringSize.Width;
// drawRect.X = x;
// this.Invalidate();
//}
//从右到左
private void OnTick()
{
x--;
if (x < -stringSize.Width)
x = 0 + this.width /*+ stringSize.Width*/;
drawRect.X = x;
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
stringSize = e.Graphics.MeasureString(drawString, drawFont);
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), drawRect.X, drawRect.Y, stringSize.Width, stringSize.Height);
drawRect.Width = stringSize.Width;
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
base.OnPaint(e);
}
}
}
Form1.Designer.cs
namespace gundong
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.newPictureBox = new gundong.NewPictureBox();
this.newLabel = new gundong.NewLabel();
((System.ComponentModel.ISupportInitialize)(this.newPictureBox)).BeginInit();
this.SuspendLayout();
//
// newPictureBox
//
this.newPictureBox.BackColor = System.Drawing.Color.White;
this.newPictureBox.Location = new System.Drawing.Point(38, 32);
this.newPictureBox.Name = "newPictureBox";
this.newPictureBox.Size = new System.Drawing.Size(151, 218);
this.newPictureBox.TabIndex = 2;
this.newPictureBox.TabStop = false;
//
// newLabel
//
this.newLabel.BackColor = System.Drawing.Color.White;
this.newLabel.Location = new System.Drawing.Point(337, 32);
this.newLabel.Name = "newLabel";
this.newLabel.Size = new System.Drawing.Size(284, 218);
this.newLabel.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(664, 306);
this.Controls.Add(this.newPictureBox);
this.Controls.Add(this.newLabel);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.newPictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
//private System.Windows.Forms.Label label1;
private NewLabel newLabel;
private NewPictureBox newPictureBox;
}
}
效果图如下:
- .Winform滚动字幕
- Winform实现滚动字幕
- Winform实现滚动字幕
- winform滚动字幕
- winform技巧—滚动的字幕
- WinForm Commbobox赋值和取值,字幕滚动
- 滚动字幕
- 滚动字幕
- 滚动字幕
- 滚动字幕
- 滚动字幕
- 滚动字幕
- 滚动字幕
- 滚动字幕
- 在C# WinForm 轻松实现滚动字幕特效的关于窗体
- javascript制作滚动字幕
- JS 滚动字幕
- 滚动字幕(C#)
- NYOJ 题目328 完全覆盖(数学)
- Python 错误和异常小结
- N后问题
- Android:Layout_weight的深刻理解
- 黑马程序员 —OC(Foundation)
- winform滚动字幕
- [每天一个知识点]14-Java语言-字符串拼接
- 网上交易的守护者-数字证书
- 1073.Scientific Notation
- JS_对象创建的几种方式
- MyEclipse2014安装插件的几种方式
- 1074.Reversing Linked List
- 解决pyside-uic编译QBottonGroup的报错:unexpected error in pyside-uic
- 1075.PAT Judge