Click here to Skip to main content
15,867,453 members
Articles / Multimedia / GDI+
Article

Star Wars style text scroller

Rate me:
Please Sign up or sign in to vote.
4.73/5 (48 votes)
9 Jul 2007CPOL3 min read 144.9K   3.2K   106   44
Text scroller control with 3D-look (like in the intro of the Star Wars movies)

Screenshot - preview.jpg

Introduction

You can find text scrollers in many programs, especially in their About dialogs. In most cases, it's a simple colored text that moves up. In this article, I attempted to create something unusual. I decided to write an "outgoing" text component, which looks like a 3D effect such as in the intro to the Star Wars movies.

GDI+ provides many easy-to-use objects and functions. Using these functions, you can do something special without much trouble. Before writing this component with the help of GDI+, I tried to create it with GDI. This required much more time and resulted in ten times more code. It seemed to be a hard task, but later I recreated this same component in GDI+. That was easy. After that, I decided to write this article to demonstrate some features of GDI+.

How we can do this

We can create this "outgoing" effect by transforming all points of text from rectangular to trapezoidal shape:

Transform process.

To animate our transformed text, we'll use an offset variable and timer. On the timer tick, we'll change this variable and repaint the control.

The code

The most useful code is situated in the Paint event handler. First of all, we need to enable anti-aliasing for better quality:

C#
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

After that, we erase background to clean the previous frame:

C#
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 
    this.ClientRectangle);

Then we create a GraphicsPath object and fill it with visible lines of text depending on the offset:

C#
GraphicsPath path = new GraphicsPath();
for (int i = m_text.Length - 1; i >= 0; i--)
{
    Point pt = new Point((int) 
        ((this.Width - e.Graphics.MeasureString(m_text[i], 
        this.Font).Width) / 2), (int)(m_scrollingOffset +
        this.Height - (m_text.Length - i) * this.Font.Size));

    // Adds visible lines to path.
    if ((pt.Y + this.Font.Size > 0) && (pt.Y < this.Height))
        path.AddString(m_text[i], new FontFamily("Arial"), 
        (int)FontStyle.Bold, this.Font.Size,
        pt, StringFormat.GenericDefault);
}

After that, we transform our GraphicsPath from rectangle to trapezoid:

C#
path.Warp(new PointF[4] 
{ 
    new PointF(this.Width / 3, 0),
    new PointF(this.Width * 2 / 3, 0),
    new PointF(0, this.Height),
    new PointF(this.Width, this.Height)
},
new RectangleF(this.ClientRectangle.X, 
this.ClientRectangle.Y, this.ClientRectangle.Width, 
this.ClientRectangle.Height),
null, WarpMode.Perspective);

The text is now ready. Next, we need to draw it and dispose of the GraphicsPath object:

C#
// Draws wrapped path.
e.Graphics.FillPath(new SolidBrush(this.ForeColor), path);
path.Dispose();

To make control more realistic, we can draw some "fog" using LinearGradientBrush with transparent color:

C#
// Draws fog effect with help of gradient brush with alpha colors.
using (Brush br = new LinearGradientBrush(new Point(0, 0), 
    new Point(0, this.Height),
    Color.FromArgb(255, this.BackColor), Color.FromArgb(0, 
    this.BackColor)))
    {
        e.Graphics.FillRectangle(br, this.ClientRectangle);
    }

Using the code

The Scroller class represents an easy-to-use component with customizable font, background color, text color and, of course, text content. You can simply copy the Scroller class to your project to use it. Also, you can create a separate class library for the Scroller class.

Properties of the Scroller control

This control has the following properties:

  • TextToScroll – this text will be separated into lines at the \n symbol
  • BackColor – color of background
  • ForeColor – color of text
  • Interval - delay in milliseconds between frames for controlled scrolling speed
  • TextFont - font that is used to draw; units must be set to GraphicsUnit.Pixel
  • TopPartSizePercent - top part size of text in percent of control width

Methods of the Scroller control

  • Start() – starts the animation from the beginning
  • Stop() – stops the animation

Points of interest

When I was creating this control, I noticed that the GraphicsPath class can help you in situations where you need some specific transformation of graphics objects, including point transformations.

Disclaimer

You can use this code in any type of project, free or commercial. If you do, please add a link to this article in your code comments or the About dialog box.

Thanks for reading and thanks to all who have helped me improve this article.

History

  • 9 July, 2007 - First updated version posted
    • Fixed bug with text offset: Font Units is now set to GraphicsUnit.Pixel
    • Added cycle scrolling feature
    • Added Interval property
    • Added TextFont property
    • Added TopPartSizePercent property
  • 16 May, 2007 - Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSame but in Java? Pin
MoteuchiYota8-May-23 10:04
MoteuchiYota8-May-23 10:04 
QuestionHow to set the background color to transparent? Pin
andre_muc22-Jan-13 12:20
andre_muc22-Jan-13 12:20 
GeneralMy vote of 5 Pin
xhantt19-Sep-12 18:29
xhantt19-Sep-12 18:29 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey18-Feb-12 0:13
professionalManoj Kumar Choubey18-Feb-12 0:13 
GeneralIs it possible to add images to the text Pin
Joe Sonderegger24-Jan-11 19:28
Joe Sonderegger24-Jan-11 19:28 
QuestionVB wanted? Pin
dherrmann9-Jun-09 0:48
dherrmann9-Jun-09 0:48 
AnswerRe: VB wanted? Pin
alexey N9-Jun-09 6:30
alexey N9-Jun-09 6:30 
GeneralRe: VB wanted? [modified] Pin
dherrmann10-Jun-09 23:49
dherrmann10-Jun-09 23:49 
GeneralPause Pin
Romano Dela Torre31-Mar-09 1:40
Romano Dela Torre31-Mar-09 1:40 
GeneralRe: Pause Pin
alexey N31-Mar-09 6:45
alexey N31-Mar-09 6:45 
GeneralTotally Awesome Pin
jwdyott26-Jan-09 6:11
jwdyott26-Jan-09 6:11 
Generalcpu Pin
zmrcic12-Aug-08 1:39
zmrcic12-Aug-08 1:39 
GeneralRe: cpu Pin
alexey N12-Aug-08 6:29
alexey N12-Aug-08 6:29 
Questionc# c++? Pin
cccfff77723-Jan-08 0:54
cccfff77723-Jan-08 0:54 
AnswerRe: c# c++? Pin
alexey N13-Feb-08 17:10
alexey N13-Feb-08 17:10 
Generalscroller Pin
Mr Imacx10-Oct-07 16:11
Mr Imacx10-Oct-07 16:11 
GeneralFix for text centering Pin
chaiguy13373-Oct-07 12:27
chaiguy13373-Oct-07 12:27 
The Graphics.MeasureString() function does a very poor job of accurately measuring the width of a given string, which throws off your manual centering mechanism in OnPaint. Here is a slight modification which uses StringFormat to center instead, which works a lot better:

private void OnPaint( object sender, PaintEventArgs e ) {<br />
	// Sets antialiasing mode for better quality.<br />
	e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;<br />
<br />
	// Prepares background.<br />
	e.Graphics.FillRectangle( new SolidBrush( this.BackColor ), this.ClientRectangle );<br />
<br />
	// Creates GraphicsPath for text.<br />
	GraphicsPath path = new GraphicsPath();<br />
	StringFormat format = StringFormat.GenericTypographic;<br />
	format.Alignment = StringAlignment.Center;<br />
<br />
	// Visible lines counter;<br />
	int visibleLines = 0;<br />
<br />
	for ( int i = m_text.Length - 1; i >= 0; i-- ) {<br />
		Point pt = new Point( this.ClientSize.Width / 2,<br />
			(int)( m_scrollingOffset + this.ClientSize.Height - ( m_text.Length - i ) * m_font.Size ) );<br />
<br />
		// Adds visible lines to path.<br />
		if ( ( pt.Y + m_font.Size > 0 ) && ( pt.Y < this.Height ) ) {<br />
			path.AddString( m_text[i], m_font.FontFamily, (int)m_font.Style, m_font.Size, pt, format );<br />
<br />
			visibleLines++;<br />
		}<br />
	}<br />
<br />
...<br />


Logan
GeneralRe: Fix for text centering Pin
alexey N10-Oct-07 7:50
alexey N10-Oct-07 7:50 
GeneralAwesome!! Pin
chaiguy13372-Oct-07 18:04
chaiguy13372-Oct-07 18:04 
GeneralRe: Awesome!! Pin
alexey N2-Oct-07 18:13
alexey N2-Oct-07 18:13 
Questioncontrol Pin
Tamtararamtam11-Aug-07 5:38
Tamtararamtam11-Aug-07 5:38 
AnswerRe: control Pin
alexey N12-Aug-07 18:55
alexey N12-Aug-07 18:55 
GeneralNeat Pin
Paul Conrad14-Jul-07 8:27
professionalPaul Conrad14-Jul-07 8:27 
GeneralAngle of trapeze Pin
ChrlsHrdy7-Jul-07 16:30
ChrlsHrdy7-Jul-07 16:30 
GeneralRe: Angle of trapeze Pin
alexey N11-Jul-07 0:10
alexey N11-Jul-07 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.