Marquee in Windows Forms
Posted On August 2, 2003 by Raja Kishore Reddy filed under Internet
In this article we develop a commercial-style marquee. As always, it is required that the .NET Redistributable Framework 1.1 and the .NET SDK 1.1 be installed on your machine. Then, run the C:\Program Files\Microsoft.NET\SDK\v1.1\Bin\sdkvars.bat batch file to set the .NET environment variables correctly.
Difference between Marquee and Text Scroll
A text scroll just scrolls text backwards or forwards, optionally bouncing back and forth between the boundaries. This is what we had created in the first part of this series. The disadvantage with this approach is that only small strings can be displayed. Larger strings cannot be displayed properly, as they will not fit on the screen.
Hence, commercial implementations of text scrollers possess an innovative addition: the text scrolls off the screen, moving continuously from left to right. Once the text is finished, it starts all over again. This enables the display of longer messages. In addition, it is found that some people like to spend their time reading such marquees, thereby adding value for potential advertisers. Note that the text should scroll from right to left rather than left to right as English text is read from left to right. Thus, most commercial marquees move from right to left. As we shall be doing this anyway in the next part, in this part we shall do it the opposite way.
Such commercial marquees can be seen in railway stations, on ad hoardings and at airports. Let us now modify our previous amusing program into a more serious commercial-quality marquee.
Commencement of the Program
The program starts at the static Main() block, and being a Windows Form,the constructor oForm() is called:
private static void Main()
{
Application.Run(new oForm());
}
The constructor oForm() is a subclass of Form, indicating that the application at hand is a Form.
Restarting the Text
Since most of the program is identical to the text scroller, interested readers can learn the basic concepts in the first part. The full code is given below (see source code listing). Note that we now have a much longer text, spread across multiple lines. The crux of the program is where we restart the text:
if( x> (Width) ) // restart from left
{ x = 0 - (int)s_width ; } // with offset
Here, we obtain the string width string s_width = g.MeasureString(s, f).Width; and subtract it from the initial zero position, where s denotes a string, and f a font.
The full source code (Code 1)is given below:
Difference between Marquee and Text Scroll
A text scroll just scrolls text backwards or forwards, optionally bouncing back and forth between the boundaries. This is what we had created in the first part of this series. The disadvantage with this approach is that only small strings can be displayed. Larger strings cannot be displayed properly, as they will not fit on the screen.
Hence, commercial implementations of text scrollers possess an innovative addition: the text scrolls off the screen, moving continuously from left to right. Once the text is finished, it starts all over again. This enables the display of longer messages. In addition, it is found that some people like to spend their time reading such marquees, thereby adding value for potential advertisers. Note that the text should scroll from right to left rather than left to right as English text is read from left to right. Thus, most commercial marquees move from right to left. As we shall be doing this anyway in the next part, in this part we shall do it the opposite way.
Such commercial marquees can be seen in railway stations, on ad hoardings and at airports. Let us now modify our previous amusing program into a more serious commercial-quality marquee.
Commencement of the Program
The program starts at the static Main() block, and being a Windows Form,the constructor oForm() is called:
private static void Main()
{
Application.Run(new oForm());
}
The constructor oForm() is a subclass of Form, indicating that the application at hand is a Form.
Restarting the Text
Since most of the program is identical to the text scroller, interested readers can learn the basic concepts in the first part. The full code is given below (see source code listing). Note that we now have a much longer text, spread across multiple lines. The crux of the program is where we restart the text:
if( x> (Width) ) // restart from left
{ x = 0 - (int)s_width ; } // with offset
Here, we obtain the string width string s_width = g.MeasureString(s, f).Width; and subtract it from the initial zero position, where s denotes a string, and f a font.
The full source code (Code 1)is given below:
| Code 1 |
| /* Marquee by Samar Abbas for DeveloperIQ > csc marquee.cs > marquee.exe (.NET Framework 1.1) Copyright Samar Abbas 26 Aug. 2003. All Rights Reserved. */ using System; using System.Windows.Forms; using System.Drawing; using System.Threading; public class oForm:Form { Thread t; // declare global vars int x = 10; string s = " Hello Everybody from this Marquee " + " Double Buffering is Used to prevent Flicker " + " Marquee resizes accordingly as Window is resized " + " Brought to You by Samar Abbas & Developer IQ " + " Press `q` to quit "; float s_width = 10; float s_height = 10; public oForm() { this.Size = new Size(400,200); this.Text = "Marquee by Samar Abbas"; this.KeyPress += new KeyPressEventHandler(f_keypress); SetStyle(ControlStyles.UserPaint, true); // enable dbl bufferg SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); t = new Thread(new ThreadStart(this.f_thread)); t.Start(); } void f_keypress(Object o, KeyPressEventArgs e) { // key press event handler if(e.KeyChar == 'q') f_quit(); // press q to quit e.Handled = true; } public void f_quit() { t.Abort(); this.Dispose(); // clean up system resources } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush sb = new SolidBrush(Color.Black); g.FillRectangle(sb, 0, 0, Width, Height); // fill bg SolidBrush b = new SolidBrush(Color.Yellow); Font f = new Font("Arial", 15); s_width = g.MeasureString(s, f).Width; // find strg width s_height = g.MeasureString(s, f).Height; Rectangle r = new Rectangle(0, this.Height/2 - (int)s_height, Width, (int) s_height ); g.FillRectangle(Brushes.Purple, r); // fill the marquee line g.DrawString(s, f, b, x, Height/2 - (int)s_height); // draw string } public void f_thread() { while(true) { if( x> (Width) ) // restart from left { x = 0 - (int)s_width ; } // with offset x = x + 1; Refresh(); // repaint usg new x value Thread.Sleep(30); // oThread stops 30 msecs before next loop } } private static void Main() { Application.Run(new oForm()); } } |
Execution
Next, compile and execute the program:
> csc textscroll.cs
> textscroll.exe
The output is a marquee as follows:
![]() |
As we have used the predefined Width and Height variables instead of absolute values, the application also works properly when resized. The direction of scroll is opposite to the conventional one, as the next article shall in any case be demonstrating the correct direction. Interested readers can try to reverse the direction of the marquee to the more correct direction.
In the next part, we will extend these ideas to create an XML newsreader capable of reading newsfeeds from the internet.

