Graphics – Scrolling List

Graphics – Scrolling List

The Scrolling List example shows how to draw formatted text to a bitmap and then scroll that bitmap vertically in a panel, a pixel at a time.

The example creates a scrolling list like movie credits.  The example demonstrates:

  – Creating a bitmap that is then displayed as required (the preferred graphics technique)
  – Drawing formatted text to a bitmap
  – Scrolling a bitmap in a panel

To create this example, create a form that has a timer (Timer1) and a Panel (Panel1).  Size the panel to any width, but set the height to about 120 so there is enough text to create a proper scrolling effect.  Paste this code into the form:

Public Class Form1

Dim BMP As Bitmap
Dim ScrollPos As Integer = 0
Dim ScrollLimit As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim Names() As String = {"Starring", "John Smith", "Brenda Adams", " ", _
    "and introducing", "Joe Brown", "Maggie Thompson", _
    " ", "with", "Mark Abrahams", "Jake Adamson", _
    "Will Brown", "Mary Corcoran", " ", "Produced By", _
    "Mike Newman", " ", "Titles By", "Allan Franklin", " ", "-"}
  Dim Formats() As Integer = {0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 
    1, 1, 0, 1, 0, 0}
  BMP = New Bitmap(Panel1.Width, 800, 
  Dim G As Graphics = Graphics.FromImage(BMP)
  Panel1.BackColor = Color.White
  Dim F() As Font = {New Font(Me.Font, System.Drawing.FontStyle.Bold), Me.Font}
  Dim B() As Brush = {Brushes.DarkGray, Brushes.Black}
  Dim y As Integer = 0
  Dim x As Integer = 0
  For J As Integer = 0 To Names.Count - 1
    Dim StringSize As SizeF = G.MeasureString(Names(J), F(Formats(J)),Panel1.Width)
    x = Panel1.Width / 2 - StringSize.Width / 2
    G.DrawString(Names(J), F(Formats(J)), B(Formats(J)), x, y)
    y += StringSize.Height * 1.4
    Next J
  ScrollPos = -Panel1.Height
  ScrollLimit = y
  Timer1.Enabled = True
  Me.DoubleBuffered = True
End Sub

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
  If Not BMP Is Nothing Then
    e.Graphics.DrawImage(BMP, Panel1.Bounds, New Rectangle(0, ScrollPos, Panel1.Width, Panel1.Height), GraphicsUnit.Pixel)
  End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  ScrollPos += 1
  If ScrollPos = ScrollLimit Then ScrollPos = -Panel1.Height
End Sub
End Class


  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s