Click here to Skip to main content
15,891,513 members
Articles / Programming Languages / C#
Article

A marquee control in C#

Rate me:
Please Sign up or sign in to vote.
3.58/5 (19 votes)
19 Feb 2003CPOL1 min read 191.4K   6K   43   13
A marquee control written in C#

Image 1

Overview

This application demonstrates how to create a "string loop" in C#. A common use of "string loops" is in web advertisements, where the slogan or product name appears to be revolving in a circle. The code creates a rectangle in the middle of the client area that is just large enough to fit the entire message, minus any trailing white space. Clicking on the form or resizing it will cause the "string loop" to restart.

Points to Notice

Rather than use a string to represent the message ("Catch Me If You Can…" ) it is far more efficient and intuitive to use the StringBuilder class, found in the System.Text namespace. StringBuilder allows you to directly manipulate a string, rather than having to make copies of a regular string. The manipulation of the StringBuilder’s internal string is quite straightforward:

C#
private void DrawMovingText()
{
    Graphics grfx = CreateGraphics();
    Font font = new Font( "Courier New", 20, FontStyle.Bold );
    string spaces = " ";
    // 
    // StringBuilder is used to allow for efficient manipulation of 
    // one string, rather than generating many separate strings
    //
    StringBuilder str = 
        new StringBuilder( "Catch Me If You Can..." + spaces );

    Rectangle rect = CreateRect( grfx, str, font );

    int numCycles = str.Length * 3 + 1;
    for( int i = 0; i < numCycles; ++i )
    {
        grfx.FillRectangle( Brushes.White, rect );
        grfx.DrawString( str.ToString(), font, Brushes.Red, rect );
        // relocate the first char to the end of the string
        str.Append( str[0] );
        str.Remove( 0, 1 );
        // pause for visual effect
        Thread.Sleep( 150 );
    }
    grfx.Dispose();
}

To avoid having DrawMovingText() eat up all of the CPU's attention it is placed on a worker thread. Every time the user clicks on the form or resizes it, the thread is killed and reborn. Obviously, if you wanted to alter the speed at which the letters "move" you would simply adjust the number of milliseconds that the thread sleeps for.

C#
//
// All of these events will restart the thread that draws 
// the text
//
private void Form_Load( object sender, System.EventArgs e )
{
    StartThread();
}
private void Form_MouseUp(object sender, 
                          System.Windows.Forms.MouseEventArgs e)
{
    StartThread();
}
private void Form_Resize( object sender, System.EventArgs e )
{
    StartThread();
}
private void label_Click(object sender, System.EventArgs e)
{
    StartThread();
}
private void StartThread()
{
    thread.Abort();
    // give the thread time to die
    Thread.Sleep( 100 ); 
    Invalidate();
    thread = new Thread(new ThreadStart(DrawMovingText));
    thread.Start();
}

The final point of interest is how the size and location of the rectangle that contains the message are determined. The Graphics class has a MeasureString method that will give you the width or height of the string that you are passing in. Those are used for the width and height of the rectangle. You then use those values in tandem with the dimensions of the form to determine the coordinates of where the rectangle will originate from.

C#
private Rectangle CreateRect
( Graphics grfx, StringBuilder str, Font font )
{
    // + 5 to allow last char to fit
    int w = (int)grfx.MeasureString(str.ToString(), font).Width + 5; 
    int h = (int)grfx.MeasureString( str.ToString(), font ).Height;
    int x = (int)this.Width / 2 - w / 2;
    int y = (int)this.Height / 2 - h;
    return new Rectangle( x, y, w, h );
}

License

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


Written By
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions

 
QuestionNice piece of app! Pin
Daniel Quiming2-Nov-14 19:54
Daniel Quiming2-Nov-14 19:54 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey5-Mar-12 2:27
professionalManoj Kumar Choubey5-Mar-12 2:27 
GeneralMy vote of 1 Pin
ali32b21-Nov-10 4:30
ali32b21-Nov-10 4:30 
QuestionTransparent background of rectangle Pin
Member 273427022-Jun-09 18:45
Member 273427022-Jun-09 18:45 
AnswerRe: Transparent background of rectangle Pin
freakyit17-Sep-09 22:22
freakyit17-Sep-09 22:22 
Generalthumbs up Pin
nelsonpaixao1-Aug-08 13:35
nelsonpaixao1-Aug-08 13:35 
GeneralWidth of scrolling text. Pin
Divermarv27-Aug-04 12:44
Divermarv27-Aug-04 12:44 
GeneralRe: Width of scrolling text. Pin
Icemanlaw30-Jan-05 4:34
Icemanlaw30-Jan-05 4:34 
GeneralThread.Join Pin
Thong Nguyen20-Feb-03 17:23
Thong Nguyen20-Feb-03 17:23 
GeneralRe: Thread.Join Pin
Thong Nguyen20-Feb-03 17:26
Thong Nguyen20-Feb-03 17:26 
GeneralRe: Thread.Join Pin
Michael Potter21-Feb-03 2:53
Michael Potter21-Feb-03 2:53 
GeneralRe: Thread.Join Pin
leppie21-Feb-03 6:12
leppie21-Feb-03 6:12 
GeneralRe: Thread.Join Pin
Michael Potter21-Feb-03 6:51
Michael Potter21-Feb-03 6:51 

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.