Click here to Skip to main content
15,881,092 members
Articles / Desktop Programming / Windows Forms
Article

A simple C# Scrolling AboutBox Control

Rate me:
Please Sign up or sign in to vote.
3.22/5 (12 votes)
26 Jan 20064 min read 187.6K   2.8K   64   30
Describes how to create a simple scrolling aboutbox usercontrol with C#. The control can manage its display object list including text or image.

Introduction

I wanted to have a scrolling about box in my project that could display text, images, and automatically scroll them as a credits dialog. This is an additional feature I'd rather add to some of my finished applications. After spending many hours searching on the Internet, I found a few articles and example codes, and especially in this site, I was impressed by the following articles:

Both of the above are done well, but they were not what I wanted. These programs were developed in Visual C++ 6.0 or Visual C++ 7.1 (.NET) but using MFC because of which I couldn't build a C# control based on their modules. And I had to change them to fit them to my work.

Background

As said earlier, these example programs used MFC for coding convenience. The functions CreateCompatibleDC, CreateCompatibleBitmap and SelectObject etc.. within MFC are common ways for image processing and operating with the device context of the drawing window. But, I now work in the .NET Framework, using either VB.NET or C#, so the above programs can't help me any longer.

I began searching on the Internet and looked in MSDN, and came to know that there was a workaround in C# to use the old Win32 API graphics functions for smooth drawing. The idea is to let C# know that you will be using a few functions from an unmanaged DLL, using the DllImport attribute. The detailed documentation of DllImport can be found in .NET documentation. I am a newbie in C#, so it's too difficult for me to try solving the problem in that way.

Fortunately, we can achieve the same goal with graphics' System.Drawing namespace in .NET. Image rendering in .NET is much simple and efficient when compared to MFC. There are two functions in Graphics class to render your Image object on the screen, these are DrawImage and DrawImageUnscaled. So, we would do our off-screen drawing in an Image object. After that, we can use these functions to render this object directly to DC and have the smooth animated effects.

As in my code, I'll try to draw all the display objects within the OnPaint() event of the control:

C#
private void ctlScrollAbout_Paint(object sender, 
             System.Windows.Forms.PaintEventArgs e)
{
    using (System.Drawing.Graphics objGraphics = e.Graphics)
    {
        // draw off-screen m_TempDrawing bitmap on control screen
        objGraphics.DrawImage(m_TempDrawing, m_XMargin, m_YMargin, 
                    m_TempDrawing.Width, m_TempDrawing.Height);
    }
}

It is not surprising if you are wondering when the off-screen was drawn. Usually, it is drawn just before we call the DrawImage function. In this case, our control is an autoscroll aboutbox, so we must have a timer to operate the speed of scrolling. It's OK to draw the off-screen along with timer tick-event and call the OnPaint() function manually. Look at this:

C#
// timer tick event
private void tmeScrolling_Tick(object sender, System.EventArgs e)
{
    BuildScrollingBitMap();  // build offline screen
    this.Invalidate();        // call to OnPaint() event
}

The next session will describe how the scrolling AboutBox administers its display objects. There are two kinds of display objects for this control: a text object and an image object. It is possible to use a common way that holds all these objects in a list. .NET supports some collection base objects such as ArrayList, HashTable, Queue, Stack, SortedList etc., which can be used easily. But, we want our own list for the specific display objects, so we should inherit a new class from the available ones. We have clsDisplayList to manage the display objects as shown below:

C#
// inherits a new custom list object from CollectionBase class 
public class clsDisplayList : System.Collections.CollectionBase
{
    // add new display object to list
    public int Add(clsDisplayObject value)
    {
        return List.Add(value);
    }

    // provide array-index access to display object list
    public clsDisplayObject this[int index]
    {
        get
        {
            return (clsDisplayObject)List[index];
        }
        set
        {
            List[index] = value;
        }
    }
}

For the display object, it is installed with a class which has all the necessary member variables and corresponding properties. The scrolling AboutBox would use these properties to draw the display object correctly on the screen:

C#
// a instance of the display object 
public class clsDisplayObject
{
    // for text object
    private string _displayText = "";
    // for text object
    private System.Drawing.Font _objFont;
    // for text object
    private System.Drawing.FontStyle _fontStyle = 
                           System.Drawing.FontStyle.Bold;
    // for text object
    private string _fontName = "Tahoma";
    // for text object
    private int _fontSize = 10;
    // for text object
    private System.Drawing.Brush _textColor = 
            System.Drawing.Brushes.White;

    // determine if current display object is bitmap or text
    private bool _isBitMap = false;
    // the height of display object
    private int _displayHeight = 0;
    // offset in vertical of display object
    private int _displayYOffset = 0;

    // bitmap display object
    private System.Drawing.Bitmap  _bitmap;
    private int _bitmapHeight = 100;
    private int _bitmapWidth = 100;

    .....
}

In the control's class, we declare an instance of clsDisplayList() and provide some methods to add a new display object:

C#
public class ctlScrollAbout : System.Windows.Forms.UserControl
{
    ....
    // display object list
    private clsDisplayList m_DisplayObject = new clsDisplayList();
    ...

    /// <summary>
    /// Add a text display object.
    /// </summary>
    public void AddDisplayText(string text, string fontName, ?
           int fontSize, System.Drawing.FontStyle fontStyle, 
           System.Drawing.Brush textColor)
    {
        m_DisplayObject.Add(new clsDisplayObject(text, fontName, 
                                fontSize, fontStyle, textColor));
        BuildScrollingBitMap();
    }

    /// <summary> 
    /// Add a bitmap display object.
    /// </summary>
    public void AddDisplayBitmap(string fileName, 
                                 int bmpHeight, int bmpWidth)
    {
        m_DisplayObject.Add(new clsDisplayObject(fileName, 
                                bmpHeight, bmpWidth));
        BuildScrollingBitMap();
    }
}

Now that everything is clear you can integrate them to make your own control. For a step by step guide on creating a C# control, you can refer to the article: Simple introduction to writing your first .NET control.

Using the code

To use the above control in your application is very simple. Download the control source code and build it to make a C# control. It would be a DLL file in the compile output directory; in my case, it was .\bin\Debug\ScrollerAbout.dll.

After having the DLL file, you create a new Windows Application project. In the design window, please open the Toolbox tab and right click and choose Add/Remove Items. Then browse to our DLL file, select it and press OK. There is a scrolling aboutbox control in your Toolbox.

You can now drag the about control to any form in your .NET application and start using it. The following code shows how to initialize and pass parameters to the about control when a form loads:

VB
' VB.Net example code
Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    ctrAbout.LineSpacing = 20
    ctrAbout.XMargin = 20
    ctrAbout.YMargin = 20
    ctrAbout.ScrollingStep = 1
    ctrAbout.ScrollingTime = 100

    ctrAbout.AddDisplayText("Scrolling AboutBox", "Arial Black", _
       15, System.Drawing.FontStyle.Bold, System.Drawing.Brushes.White)
    ctrAbout.AddDisplayText("Developed by", "Arial Black", 10, _
       System.Drawing.FontStyle.Italic, System.Drawing.Brushes.Olive)
    ctrAbout.AddDisplayText("Man_Man2612", "Arial Black", 10, _
      System.Drawing.FontStyle.Strikeout, System.Drawing.Brushes.Violet)

    ctrAbout.AddDisplayBitmap("images\test.gif", 115, 115)
    ctrAbout.AddDisplayBitmap("images\pic0.BMP", 100, 100)
    ctrAbout.StartScrolling()
End Sub


// C# example code
private void Form1_Load(object sender, System.EventArgs e)
{
    ctrAbout.LineSpacing = 20;
    ctrAbout.XMargin = 20;
    ctrAbout.YMargin = 20;
    ctrAbout.ScrollingStep = 1;
    ctrAbout.ScrollingTime = 500;

    ctrAbout.AddDisplayText("Scrolling AboutBox", "Arial Black", _
      15, System.Drawing.FontStyle.Bold, System.Drawing.Brushes.White); 
    ctrAbout.AddDisplayText("Developed by", "Arial Black", 10, _
      System.Drawing.FontStyle.Italic, System.Drawing.Brushes.Olive);
    ctrAbout.AddDisplayText("Man_Man2612", "Arial Black", 10, _
      System.Drawing.FontStyle.Strikeout, System.Drawing.Brushes.Violet);

    ctrAbout.AddDisplayBitmap(@"images\test.gif",115,115); 
    ctrAbout.AddDisplayBitmap(@"images\pic0.BMP",320,240); 
         ctrAbout.StartScrolling()
}

Summary

We now know how to create a custom user control for our specific purpose in .NET Framework. We also saw that making a new inherited collection class that is used to manage our own objects is very simple. This scrolling AboutBox has some advanced features that I would like to improve such as text wrapping, hyperlink object, animation image etc. But due to lack of time, I just created this article, and hope that with this open source code someone can help me make a new useful scrolling AboutBox version.

History

  • 25th Oct, 2004 - Version 1.0.
    • Article submitted.
  • 20th Jan, 2006
    • Updated by Dan Morphis.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Vietnam Vietnam
I am a programmer now.

Comments and Discussions

 
GeneralThe scroll is flickering Pin
Member 114705017-Aug-08 12:47
professionalMember 114705017-Aug-08 12:47 
GeneralBrilliant! Pin
User 167325225-Mar-08 13:12
User 167325225-Mar-08 13:12 
GeneralBackground image color Pin
LGAP6-Mar-08 2:25
LGAP6-Mar-08 2:25 
GeneralRe: Background image color Pin
User 167325225-Mar-08 17:23
User 167325225-Mar-08 17:23 
GeneralFlicker Pin
pk_fox23-Nov-06 4:50
pk_fox23-Nov-06 4:50 
GeneralRe: Flicker Pin
Man_Man261226-Nov-06 15:40
Man_Man261226-Nov-06 15:40 
GeneralRe: Flicker Pin
pk_fox26-Nov-06 23:29
pk_fox26-Nov-06 23:29 
GeneralParameter is not valid ? [modified] Pin
K32126-Aug-06 13:25
K32126-Aug-06 13:25 
GeneralFlickering Pin
smcneese27-Jun-06 9:54
smcneese27-Jun-06 9:54 
GeneralImporting key file "RedFox Consulting.pfx" was canceled. Pin
SupermanDT6-Jun-06 9:05
SupermanDT6-Jun-06 9:05 
QuestionNET 1.1 available? Pin
mathewww24-Apr-06 14:17
mathewww24-Apr-06 14:17 
QuestionVS .NET 2005 or VS .NET 2003? Pin
User 17912930-Jan-06 22:07
professionalUser 17912930-Jan-06 22:07 
Hi, you should mention that the project is for .NET 2.0/VS .NET 2005 because in the upper right corner of the article it says:
"
C#
Windows (Win2K, WinXP), .NET (.NET 1.1)
Win32, VS (VS.NET2003), WinForms
Dev
"

-- modified at 4:07 Tuesday 31st January, 2006

modified 2-Aug-18 21:02pm.

AnswerRe: VS .NET 2005 or VS .NET 2003? Pin
Man_Man26125-Feb-06 16:11
Man_Man26125-Feb-06 16:11 
GeneralIs there a reason Pin
fwsouthern28-Jan-06 11:03
fwsouthern28-Jan-06 11:03 
GeneralRe: Is there a reason Pin
Man_Man261230-Jan-06 16:48
Man_Man261230-Jan-06 16:48 
GeneralRe: Is there a reason Pin
dmstrat7-Feb-06 15:25
dmstrat7-Feb-06 15:25 
GeneralRe: Is there a reason Pin
Man_Man26127-Feb-06 16:47
Man_Man26127-Feb-06 16:47 
AnswerRe: Is there a reason Pin
TimonAK10-Mar-06 18:58
TimonAK10-Mar-06 18:58 
QuestionCode modifications - where/who to send? Pin
TimonAK20-Jan-06 16:17
TimonAK20-Jan-06 16:17 
AnswerRe: Code modifications - where/who to send? Pin
Man_Man261223-Jan-06 14:43
Man_Man261223-Jan-06 14:43 
GeneralRe: Code modifications - where/who to send? Pin
LatencyXXX13-Apr-08 14:43
LatencyXXX13-Apr-08 14:43 
GeneralDLL Error Pin
Dys29-Mar-05 9:21
Dys29-Mar-05 9:21 
GeneralRe: DLL Error Pin
Man_Man261229-Mar-05 14:53
Man_Man261229-Mar-05 14:53 
GeneralRe: DLL Error Pin
Dys30-Mar-05 11:20
Dys30-Mar-05 11:20 
GeneralFlickering Pin
WillemM5-Nov-04 8:23
WillemM5-Nov-04 8:23 

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.