|
-
Feb 9th, 2014, 01:44 PM
#1
Thread Starter
New Member
[RESOLVED] Drawing gdi+ flickering??? Heeeelp!
I have a Class which inherits a UserControl. The aim is to graphically display the sound input/output. The Property "Value" of Animation Class, receives the current value of the sound in realtime between 0 and 100, which i update from an outside Class via a Timer(50MSec.)
I managed it all to work but i am facing an annoying problem, the animation flickers. I raised the Thread.Priority into Highest, no effect, so i dont believe a separate Thread would do better. I researched and know now basically where the problem lies, but still cant figure out how to handle it. I have tried as in the beginning the ME.CreateGraphics Method, it flickered, then overrided OnPaint Method and painted with e.Graphics. The result was a blank control! Thanks for any help!
Here is a small demo of the code, an example
Code:
Public Class Form1
Private [DISPLAY] As Form 'A SEPARATE FORM THAT CONTAINS THE USERCONTROL "ANIMATION" & DISPLAYS IT
Private _Animation As Animation
Private Shadows Sub [load]() Handles MyBase.Load
DISPLAY = New Form
_Animation = New Animation
DISPLAY.Controls.Add(_Animation)
_Animation.Dock = DockStyle.Fill
DISPLAY.Show()
Dim tmr As New Timer(New System.ComponentModel.Container)
tmr.Interval = 50
AddHandler tmr.Tick, AddressOf Looper
tmr.Start()
End Sub
Private Sub Looper()
_Animation.Value = Now.Second 'AN EXAMPLE
End Sub
End Class
Public Class Animation
Inherits UserControl
Private _Value As UInt32
'EVERYTIME VALUE IS CHANGED IT WILL REPAINT THE CONTROL
Public Property Value As UInt32
Get
Return _Value
End Get
Set(ByVal value As UInt32)
_Value = value
[Refresh]()
End Set
End Property
Sub New()
' InitilizeComponent()
Countleds()
End Sub
Private LEDs As Integer
'COUNT THE AMOUNT OF LEDS POSSIBLE TO FIT IN CONTROL
Private Sub Countleds() Handles Me.SizeChanged
dictOfLeds.Clear()
LEDs = 0
If Me.Width < SizeCurrent Then
LEDs = 1
Else
Dim _MeWidth As Integer = Me.Width - SizeCurrent
LEDs = (_MeWidth \ (SizeOld + Space)) + 1
End If
For i As UInteger = 1 To LEDs Step +1
dictOfLeds.Add(i, 0)
Next
'MsgBox(LEDs)
End Sub
Private dictOfLeds As New Dictionary(Of UInt32, UInt16)
Private Space As Integer = 2
Private SizeCurrent = 7
Private SizeOld As Integer = 3
Private ColorOld As Brush = New SolidBrush(Color.Aqua)
Private ColorCurrent As Brush = New SolidBrush(Color.Blue)
Private _Pen As New Pen(Brushes.AliceBlue, 5.0F)
Private _StartPoint As New Point(0, 0)
Private _EndPoint As New Point(0, 0)
'THIS SUB WILL BE CALLED WHENEVER THE "[Value]" Property is changed
Private Shadows Sub [Refresh]()
For i = LEDs To 2 Step -1
If i = 2 Then dictOfLeds(1) = Value
dictOfLeds(i) = dictOfLeds(i - 1)
Next
Me.CreateGraphics.Clear(Color.Green)
Dim int As Integer
For i As Integer = 1 To LEDs Step +1
If i = 1 Then
int = (SizeCurrent \ 2) + Space
_Pen.Width = SizeCurrent
_Pen.Brush = ColorCurrent
ElseIf i = 2 Then
int = (SizeCurrent + Space) + ((SizeOld \ 2) + Space)
_Pen.Width = SizeOld
_Pen.Brush = ColorOld
ElseIf i > 2 Then
int += SizeOld + Space
_Pen.Width = SizeOld
_Pen.Brush = ColorOld
End If
Dim _location As New Point(0, CInt(Me.Height \ 2))
_StartPoint.X = (Me.Width) - int
_StartPoint.Y = (_location.Y - (Me.Height / 2) + dictOfLeds.Item(i))
_EndPoint.X = (Me.Width) - int
_EndPoint.Y = (_location.Y + (Me.Height \ 2) + (Me.Height / 2) + dictOfLeds.Item(i))
'Me.Invalidate() to raise Onpaint when painting via Onpaint
Me.CreateGraphics.DrawLine(_Pen, _StartPoint, _EndPoint)
Next
End Sub
Protected Overrides Sub Onpaint(Byval sender as Object, Byval e as painteventargs)
'WHEN PASSED THE e.graphics.drawline(_pen,_startpoint, _endpoint) result was nothing
End Sub
End Class
The result looks like this in my project (not the above code). the capture looks good but in reality it flickers while refreshing!
Last edited by Begi; Feb 9th, 2014 at 01:55 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|