Click here to Skip to main content
Click here to Skip to main content

Star Wars style text scroller

By , 9 Jul 2007
 

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:

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

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

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:

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:

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:

// 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:

// 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)

About the Author

alexey N
Software Developer
Russian Federation Russian Federation
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to set the background color to transparent?memberandre_muc22 Jan '13 - 12:20 
Hello alexey,
 
thank you very much. I love this text scroller.
Could you tell me how to set the background to transparent? I would lay a video (maybe DirectX) with a starfield animation behind the text.
 
Thank you very much!
 
andre
GeneralMy vote of 5memberxhantt19 Sep '12 - 18:29 
Excelent, a famous effect very well explained
GeneralMy vote of 5membermanoj kumar choubey18 Feb '12 - 0:13 
Nice
GeneralIs it possible to add images to the textmemberJoe Sonderegger24 Jan '11 - 19:28 
Hello I was wondering if it is possible to add images that are added to the text.
Does anyone know how?
Have a nice life!!

QuestionVB wanted?memberdherrmann9 Jun '09 - 0:48 
Hi alexey,
I have translated your C# into the VB-Code.
Are you interested, so I can send it to you.
 
Regards
Dietrich from Austria
AnswerRe: VB wanted?memberalexey N9 Jun '09 - 6:30 
Hi, Dietrich.
 
No. I think that it's a "5 seconds deal" with help of some programs, such as SharpDevelop (Tools->Convert code to->VB.NET) or Reflector.
 
But thanks, anyway.
 
My english is not so good. Please, correct my errors.
Best regards, Alexey.

GeneralRe: VB wanted? [modified]memberdherrmann10 Jun '09 - 23:49 
You are right- I do it this way anyway Wink | ;-)
I have changed your program and added some things for example:
''' <summary>
''' Alignment of the text.
''' </summary>
Private m_textalignment As StringAlignment
...........
''' <summary>
''' Alignment of the text.
''' </summary>
Public Property TextAlignment() As StringAlignment
   Get
      Return m_textalignment
   End Get
   Set(ByVal value As StringAlignment)
      m_textalignment = value
   End Set
End Property
..............
''' <summary>
        ''' Paint handler.
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)
            With e.Graphics
                ' Sets antialiasing mode for better quality.
                .SmoothingMode = SmoothingMode.HighQuality
                .TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
                ' Prepares background.
                .FillRectangle(New SolidBrush(Me.BackColor), Me.ClientRectangle)
            End With
            ' Creates GraphicsPath for text.
            Dim path As New GraphicsPath()
            Dim format As StringFormat = StringFormat.GenericTypographic
            format.Alignment = m_textalignment
 
            ' Visible lines counter.
            Dim visibleLines As Integer = 0
            Dim pt As Point
            Dim ptX As Short
            Select Case format.Alignment
                Case StringAlignment.Center
                    ptX = Me.ClientSize.Width / 2
                Case StringAlignment.Far
                    ptX = Me.ClientSize.Width
                Case StringAlignment.Near
                    ptX = 0
            End Select
 
            For i As Integer = m_text.Length - 1 To 0 Step -1
                pt = New Point(ptX, CInt((m_scrollingOffset + Me.ClientSize.Height _
                                    - (m_text.Length - i) * m_font.Size)))
                ' Adds visible lines to path.
                If (pt.Y + m_font.Size > 0) AndAlso (pt.Y < Me.Height) Then
                    path.AddString(m_text(i), m_font.FontFamily, _
                                   CInt(m_font.Style), m_font.Size, pt, format)
                    visibleLines += 1
                End If
            Next
 
...............
This is VB and you can translate it, you know..., and use it as you like.
 
regards-
Dietrich
 
modified on Thursday, June 11, 2009 12:23 PM

GeneralPausememberMember 327739631 Mar '09 - 1:40 
How to pause and resume text?
GeneralRe: Pausememberalexey N31 Mar '09 - 6:45 
Add this method to Scroller class:
 
public void Pause()
{
	m_timer.Enabled = !m_timer.Enabled;
}

 
My english is not so good. Please, correct my errors.
Best regards, Alexey.

GeneralTotally Awesomememberjwdyott26 Jan '09 - 6:11 
This is the best thing I've ever found on the internet. I'm working on a Geek Quiz for my friends. I have sounds, pictures, etc. This is going to make an awesome opening. Thanks!

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by alexey N
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid