110,580
社区成员
发帖
与我相关
我的任务
分享
class Marquee
{
PictureBox PB = new PictureBox();
int X = 0;
String DrawText = String.Empty;
Bitmap OrgBmp = null;
delegate void SetImage(Image NewImage);
public void Start(Form Parent, String Text, Point Location, Size RectSize)
{
this.DrawText = Text;
PB.Parent = Parent;
PB.Location = Location;
PB.Size = RectSize;
X = RectSize.Width;
PB.Visible = true;
PB.Image = new Bitmap(PB.ClientRectangle.Width, PB.ClientRectangle.Height);
Graphics G = Graphics.FromImage(PB.Image);
G.FillRectangle(new SolidBrush(PB.BackColor), PB.ClientRectangle);
G.Dispose();
OrgBmp = new Bitmap(PB.Image);
Thread T = new Thread(new ThreadStart(DoDraw));
T.Start();
}
public void Stop()
{
PB.Visible = false;
}
void DoDraw()
{
while (PB.Visible)
{
Bitmap CacheBmp = new Bitmap(OrgBmp);
Graphics G = Graphics.FromImage(CacheBmp);
G.DrawString(DrawText, new Font("宋体", 10), new SolidBrush(Color.Black),
new PointF(X = X-- < -G.MeasureString(this.DrawText, new Font("宋体", 10)).Width ? PB.Size.Width : X, 0));
G.Dispose();
PB.Invoke(new SetImage(DoSetImage), new Object[] { CacheBmp });
Thread.Sleep(30);
}
}
void DoSetImage(Image NewImage)
{
PB.Image = NewImage;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication37
{
public partial class Form1 : Form
{
Button B = new Button();
Button B2 = new Button();
Marquee M = null;
public Form1()
{
InitializeComponent();
B.Parent = this;
B.Text = "开始";
B.Click += new EventHandler(B_Click);
B2.Parent = this;
B2.Text = "停止";
B2.Click += new EventHandler(B2_Click);
B2.Location = Point.Add(B.Location, new Size(B.Width, 0));
}
void B_Click(object sender, EventArgs e)
{
M = new Marquee();
M.Start(this, "ABCDEFG", Point.Add(B.Location, new Size(0, B.Height)), new Size(100, 50));
}
void B2_Click(object sender, EventArgs e)
{
M.Stop();
M = null;
}
}
class Marquee
{
PictureBox PB = new PictureBox();
int X = 0;
String DrawText = String.Empty;
Bitmap OrgBmp = null;
delegate void SetImage(Image NewImage);
public void Start(Form Parent, String Text, Point Location, Size RectSize)
{
this.DrawText = Text;
PB.Parent = Parent;
PB.Location = Location;
PB.Size = RectSize;
X = RectSize.Width;
PB.Visible = true;
PB.Image = new Bitmap(PB.ClientRectangle.Width, PB.ClientRectangle.Height);
Graphics G = Graphics.FromImage(PB.Image);
G.FillRectangle(new SolidBrush(PB.BackColor), PB.ClientRectangle);
G.Dispose();
OrgBmp = new Bitmap(PB.Image);
Thread T = new Thread(new ThreadStart(DoDraw));
T.Start();
}
public void Stop()
{
PB.Visible = false;
}
void DoDraw()
{
while (PB.Visible)
{
Bitmap CacheBmp = new Bitmap(OrgBmp);
Graphics G = Graphics.FromImage(CacheBmp);
G.DrawString(DrawText, new Font("宋体", 10), new SolidBrush(Color.Black),
new PointF(X = X-- < -G.MeasureString(this.DrawText, new Font("宋体", 10)).Width ? PB.Size.Width : X, 0));
G.Dispose();
PB.Invoke(new SetImage(DoSetImage), new Object[] { CacheBmp });
Thread.Sleep(30);
}
}
void DoSetImage(Image NewImage)
{
PB.Image = NewImage;
}
}
}