Flicker-free Ticker / Text Scroller

T

trondhuso

Hi Group,

I'm to write a Ticker application that will get it's content from RSS-
feed(s), the RSS-part is not part of the problem though.
I've searched the web and found a few solutions here and there that
creates the scrolling text, but not without flickering. I then tried
to move the text into the paint sub in the form which results in
neither a scrolling text or flickering (last thing is good, but I
guess that is because there isn't anything to flicker)...

To generate this project you start up a new project and add a timer1-
control.
Project is coded in Visual Studio / Visual Basic 2005
Code attached is from devcenter which is the one I've started to use
as a base for this code:

Imports System.Xml
Imports System.Text
Public Class Form1
Dim widthX As Single
Dim heightY As Single = 0
Dim g As Graphics
Dim xmlst As String
Dim fo As Font
Dim str As String
Dim strWidth As SizeF
Dim douX As Double
Dim lblTicker As Label

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' valWidth = Me.Width
g = Me.CreateGraphics()
widthX = Me.Width
Me.loadthenews()

Timer1.Interval = 30
Timer1.Start()
fo = New Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point)
strWidth = g.MeasureString(str, fo)
douX = strWidth.Width

SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.UserPaint, True)
End Sub


Private Sub loadthenews()
Dim readXML As New XmlTextReader("C:\utvikling\news.xml")
While readXML.Read()
If readXML.NodeType = XmlNodeType.Text Then
Str += " " & readXML.Value

End If
End While
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
' g.Clear(Me.BackColor)

' Me.Update()
End Sub

Private Sub form1_hover(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseHover
Timer1.Stop()

End Sub

Private Sub form1_leave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseLeave
Timer1.Start()
End Sub



Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
g.DrawString(str, fo, Brushes.Black, widthX, heightY)
If widthX <= (0 - douX) Then
widthX = Me.Width
Else
widthX -= 0.5

End If
End Sub

End Class


content of news.xml
<?xml version="1.0" encoding="utf-8"?>
<ticker>
<news>This article might appear on w.devarticles.com</news>
<news>This article was authored by Vikrant</news>
<news>Hover the mouse over this app</news>
<news>The timer just stopped</news>
<news>Thanx to devarticle fans</news>
<news>this ticker app is meant only for educational purpose</news>
</ticker>
 
P

Phillip Taylor

Hi Group,

I'm to write a Ticker application that will get it's content from RSS-
feed(s), the RSS-part is not part of the problem though.
I've searched the web and found a few solutions here and there that
creates the scrolling text, but not without flickering. I then tried
to move the text into the paint sub in the form which results in
neither a scrolling text or flickering (last thing is good, but I
guess that is because there isn't anything to flicker)...

To generate this project you start up a new project and add a timer1-
control.
Project is coded in Visual Studio / Visual Basic 2005
Code attached is from devcenter which is the one I've started to use
as a base for this code:

Imports System.Xml
Imports System.Text
Public Class Form1
Dim widthX As Single
Dim heightY As Single = 0
Dim g As Graphics
Dim xmlst As String
Dim fo As Font
Dim str As String
Dim strWidth As SizeF
Dim douX As Double
Dim lblTicker As Label

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' valWidth = Me.Width
g = Me.CreateGraphics()
widthX = Me.Width
Me.loadthenews()

Timer1.Interval = 30
Timer1.Start()
fo = New Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point)
strWidth = g.MeasureString(str, fo)
douX = strWidth.Width

SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.UserPaint, True)
End Sub

Private Sub loadthenews()
Dim readXML As New XmlTextReader("C:\utvikling\news.xml")
While readXML.Read()
If readXML.NodeType = XmlNodeType.Text Then
Str += " " & readXML.Value

End If
End While
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
' g.Clear(Me.BackColor)

' Me.Update()
End Sub

Private Sub form1_hover(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseHover
Timer1.Stop()

End Sub

Private Sub form1_leave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseLeave
Timer1.Start()
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
g.DrawString(str, fo, Brushes.Black, widthX, heightY)
If widthX <= (0 - douX) Then
widthX = Me.Width
Else
widthX -= 0.5

End If
End Sub

End Class

content of news.xml
<?xml version="1.0" encoding="utf-8"?>
<ticker>
<news>This article might appear on w.devarticles.com</news>
<news>This article was authored by Vikrant</news>
<news>Hover the mouse over this app</news>
<news>The timer just stopped</news>
<news>Thanx to devarticle fans</news>
<news>this ticker app is meant only for educational purpose</news>
</ticker>

I got flicker free text scrolling working by using a series of labels
in a panel that have their "left" property reduced. Instead of using a
timer I used a second thread and that's what made it not flicker for
me. When left + width < 0 then I move it back to the right hand side
of the screen.

To each his own!

Phill
 
T

trondhuso

Hi Group,

I'm to write a Ticker application that will get it's content from RSS-
feed(s), the RSS-part is not part of the problem though.
I've searched the web and found a few solutions here and there that
creates the scrolling text, but not without flickering. I then tried
to move the text into the paint sub in the form which results in
neither a scrolling text or flickering (last thing is good, but I
guess that is because there isn't anything to flicker)...

To generate this project you start up a new project and add a timer1-
control.
Project is coded in Visual Studio / Visual Basic 2005
Code attached is from devcenter which is the one I've started to use
as a base for this code:

Imports System.Xml
Imports System.Text
Public Class Form1
Dim widthX As Single
Dim heightY As Single = 0
Dim g As Graphics
Dim xmlst As String
Dim fo As Font
Dim str As String
Dim strWidth As SizeF
Dim douX As Double
Dim lblTicker As Label

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' valWidth = Me.Width
g = Me.CreateGraphics()
widthX = Me.Width
Me.loadthenews()

Timer1.Interval = 30
Timer1.Start()
fo = New Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point)
strWidth = g.MeasureString(str, fo)
douX = strWidth.Width

SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.UserPaint, True)
End Sub

Private Sub loadthenews()
Dim readXML As New XmlTextReader("C:\utvikling\news.xml")
While readXML.Read()
If readXML.NodeType = XmlNodeType.Text Then
Str += " " & readXML.Value

End If
End While
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
' g.Clear(Me.BackColor)

' Me.Update()
End Sub

Private Sub form1_hover(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseHover
Timer1.Stop()

End Sub

Private Sub form1_leave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseLeave
Timer1.Start()
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
g.DrawString(str, fo, Brushes.Black, widthX, heightY)
If widthX <= (0 - douX) Then
widthX = Me.Width
Else
widthX -= 0.5

End If
End Sub

End Class

content of news.xml
<?xml version="1.0" encoding="utf-8"?>
<ticker>
<news>This article might appear on w.devarticles.com</news>
<news>This article was authored by Vikrant</news>
<news>Hover the mouse over this app</news>
<news>The timer just stopped</news>
<news>Thanx to devarticle fans</news>
<news>this ticker app is meant only for educational purpose</news>
</ticker>

Found a small "bug" in the pasted code.
This is the part that makes it scroll. I moved it to the paint sub in
this code as well.
This one flickers...


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
g.Clear(Me.BackColor)
g.DrawString(str, fo, Brushes.Black, widthX, heightY)
If widthX <= (0 - douX) Then
widthX = Me.Width
Else
widthX -= 0.5

End If
Me.Update()
End Sub
 
T

trondhuso

After doing some more work to it I finally got this code working
without flickering and it also scrolls smooth. Code below attached.

Imports System.Xml
Imports System.Text
Public Class Form1
Dim txtTicker As String = "NTB er verdens beste sted å jobbe!"
Dim widthX As Single
Dim heightY As Single = 0
Dim g As Graphics
Dim xmlst As String 'string from the xml file
Dim fo As Font
Dim str As String
Dim strwidth As SizeF 'gets the xml string's width and height
Dim douX As Double 'stores the xmlstring's width
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'Debug.Write(vbCr & strwidth.Width & vbCr)
'Debug.Write(String.Format("widthX = {0}", CStr(widthX) &
vbCr))
' Debug.Write("Dette er str: " & str)
SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.UserPaint, True)

e.Graphics.Clear(Me.BackColor)
e.Graphics.DrawString(str, fo, Brushes.Black, widthX, heightY)
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
' e.Graphics.SmoothingMode =
Drawing2D.SmoothingMode.HighQuality
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed


' Debug.Write(String.Format("the string width is {0}",
CStr(strwidth.Width)))
If widthX <= (0 - douX) Then
widthX = Me.Width
Else
widthX -= 1
End If

e.Dispose()

End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Me.Width = Screen.PrimaryScreen.WorkingArea.Width
Me.Location = New Point(0, 20)
Me.MaximumSize = New
Point(Screen.PrimaryScreen.WorkingArea.Width, 50)
Me.Height = 50
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Text = ""
Me.Icon = Nothing

g = Me.CreateGraphics() 'get the graphics object of the form
widthX = Me.Width ' x co- ordinate or width
' Debug.Write(String.Format("The width is {0}",
'CStr(Me.Width)))
Me.loadthenews()
' Dim gr As Graphics
Timer1.Interval = 1
Timer1.Start()

fo = New Font("ARIAL", 10, FontStyle.Bold, GraphicsUnit.Point)
strwidth = g.MeasureString(str, fo)
douX = strwidth.Width


End Sub


Private Sub loadthenews()
Dim readXML As New XmlTextReader("C:\utvikling\news.xml")
While readXML.Read()
If readXML.NodeType = XmlNodeType.Text Then
str += " " & readXML.Value

End If
End While
End Sub



Private Sub form1_hover(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseHover
Timer1.Stop()
Debug.Write("In hover!")

End Sub

Private Sub form1_leave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.MouseLeave
Timer1.Start()
Debug.Write("In leave!")
End Sub



Private Sub tickerTypeWriter()
Dim a

Dim lblTicker As New Label
Me.Controls.Add(lblTicker)



For a = 1 To txtTicker.Length
lblTicker.Text = txtTicker.Substring(0, a) & "_"
System.Threading.Thread.Sleep(100)
lblTicker.Update()
Next
End Sub






Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Me.Refresh()
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top