"OnPaint" flickering on text lines. can it be avoided somehow? [source code included]

jari_stockholm

Newcomer
Joined
May 4, 2009
"OnPaint" flickering on text lines. can it be avoided somehow? [source code included]

Hi There
Anyone got a idea how to get rid of the flickering?

Yes, complete source code is pasted also.
copy and paste the code and RUN it.
now move the mouse up and down...

Visual Basic:
Option Explicit On

Public Class Form1
    Dim iLine As Integer
    Dim iTab As Integer

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        Dim myGraphics As Graphics = MyBase.CreateGraphics()
        Dim myPen As New Pen(Color.FromArgb(45, 45, 45))
        Dim myBrush As New SolidBrush(Color.FromArgb(230, 230, 230))
        Dim myBrush2 As New SolidBrush(Color.FromArgb(80, 80, 80))
        Dim g As Graphics = e.Graphics

        ' [1] Draw the rectangle behind text line
        myGraphics.FillRectangle(myBrush, iTab, iLine, 300, 20)

        ' [2] Draw 20 lines with some text
        For i = 0 To 20
            g.DrawString("Textline_" & (i), New Font("Verdena", 12), myBrush2, iTab, i * 20)
        Next i

        MyBase.OnPaint(e)
    End Sub
  
    Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

        ' iline is used to -> Dont update screen while mouse at same line as last time
        ' my idea to avoid unnessesary screen updates

        Select Case e.Y
            Case 0 To 19 And iLine <> 0
                iLine = 0
                Me.Invalidate()
            Case 20 To 39 And iLine <> 20
                iLine = 20
                Me.Invalidate()
            Case 40 To 59 And iLine <> 40
                iLine = 40
                Me.Invalidate()
            Case 60 To 79 And iLine <> 60
                iLine = 60
                Me.Invalidate()
            Case 80 To 99 And iLine <> 80
                iLine = 80
                Me.Invalidate()
            Case 100 To 119 And iLine <> 100
                iLine = 100
                Me.Invalidate()
            Case 120 To 139 And iLine <> 120
                iLine = 120
                Me.Invalidate()
            Case 140 To 159 And iLine <> 140
                iLine = 140
                Me.Invalidate()
            Case 160 To 179 And iLine <> 160
                iLine = 160
                Me.Invalidate()
            Case 180 To 199 And iLine <> 180
                iLine = 180
                Me.Invalidate()
        End Select

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

End Class
 
Last edited by a moderator:

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
Re: "OnPaint" flickering on text lines. can it be avoided somehow? [source code inclu

Try adding
Visual Basic:
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DoubleBuffered = True
    End Sub
to the form and see if that helps. Also is there a reason you are doing the call to base.CreateGraphics in the OnPaint as you could just use e.Graphics for all the drawing.
 

jari_stockholm

Newcomer
Joined
May 4, 2009
Re: "OnPaint" flickering on text lines & rectangle. [sollution included]

well... seems flickerfree now.
no flickering text and a flickerfree progressbar added
just add a timer and run code. hope it helps someone out there.


Option Explicit On
Imports System.Drawing.Drawing2D

Public Class Form1
Dim iWidth As Integer
Dim iLine As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 10
Timer1.Enabled = True
Me.DoubleBuffered = True
iWidth = 1
iLine = 20
End Sub

Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
Dim g As Graphics = pe.Graphics
Dim fnt As New Font("Verdana", 10)
Dim rect As New Rectangle(20, iLine, iWidth, 20)
Dim lBrush As New LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal)

g.FillRectangle(lBrush, rect)
For i = 1 To 20
g.DrawString("GDI+ World", fnt, New SolidBrush(Color.Red), 20, i * 20)
Next i
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Select Case e.Y
Case 0 To 19 And iLine <> 0
iLine = 0
Case 20 To 39 And iLine <> 20
iLine = 20
Case 40 To 59 And iLine <> 40
iLine = 40
Case 60 To 79 And iLine <> 60
iLine = 60
Case 80 To 99 And iLine <> 80
iLine = 80
Case 100 To 119 And iLine <> 100
iLine = 100
Case 120 To 139 And iLine <> 120
iLine = 120
Case 140 To 159 And iLine <> 140
iLine = 140
Case 160 To 179 And iLine <> 160
iLine = 160
Case 180 To 199 And iLine <> 180
iLine = 180
End Select
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
iWidth = iWidth + 1
Me.Text = iWidth
If iWidth = 400 Then iWidth = 1
Me.Refresh()
End Sub
End Class
 
Top Bottom