Color Transition Effect in .NET Windows Forms (Progressbar or something else)

30.10.2006

Okuyucu : 2.085
Günlük Okuyucu : 3,7

Yesterday I was fiddling with a progress bar, it was performing step in every single action to show current status.

Worker had several phases so I decided to change the color in every phase which is more eye candy and useful to the user. I implemented color changes but it was not enough cool and then I decided develop a small color transition effect for every phase changes. Here is the code how I accomplish the task;

I used 3 main functions;
StepCloser() : One step increase or decrease first number to target number. Allows me to make transition of R,G,B numbers easily.

TransColors() : Apply transition effect to progress bar. Apply StepCloser() to every R,G,B color in startingColor and endingColor. Trans color has a SyncLock because I want to see every color transition even there is another phase started.

ChangeColor() : Simply checks invoke and apply color to progressbar.

This process may can slow your application a bit, be careful! and disable lock if you want it faster.

Kaynak Kodunu Download Edin (Download Source Code);

/opensource/source-code/color-transition.txt

Kaynak Kod;

1 : <%

3 : Dim LockObj As New Object
4 : Public Sub PhaseChangedHandler(ByVal sender As Object, ByVal e As EventArguments.PhaseChangedEventArgs)
5 : SyncLock LockObj
6 : Select Case DirectCast(e, EventArguments.PhaseChangedEventArgs).Phase
7 : Case DilemmaManager.Phase.PhaseX
8 : TransColors(PrgMain.ForeColor, Color.DodgerBlue, PrgStatus)

10 : Case DilemmaManager.Phase.Y, DilemmaManager.Phase.Z
11 : TransColors(PrgMain.ForeColor, Color.DeepSkyBlue, PrgStatus)

13 : Case DilemmaManager.Phase.D
14 : TransColors(PrgMain.ForeColor, Color.DeepPink, PrgStatus)

16 : End Select
17 : End SyncLock

19 : End Sub


22 : ''' <summary>
23 : ''' Apply transition effect to progress bar color.
24 : ''' </summary>
25 : ''' <param name="startingColor">Color of the starting.</param>
26 : ''' <param name="endingColor">Color of the ending.</param>
27 : ''' <param name="progressBar">The progress bar.</param>
28 : Private Sub TransColors(ByVal startingColor As Color, ByVal endingColor As Color, ByVal progressBar As ToolStripProgressBar)

30 : Dim R As Integer = startingColor.R
31 : Dim B As Integer = startingColor.B
32 : Dim G As Integer = startingColor.G

34 : While Not (R = endingColor.R AndAlso B = endingColor.B AndAlso G = endingColor.G)
35 : R = StepCloser(R, endingColor.R)
36 : B = StepCloser(B, endingColor.B)
37 : G = StepCloser(G, endingColor.G)

39 : ChangeColor(Color.FromArgb(R, G, B), PrgStatus)
40 : Threading.Thread.Sleep(1)
41 : End While

43 : End Sub


46 :
47 : Const StepSpeed As Integer = 1

49 :
50 : ''' <summary>
51 : ''' One step increase or decrease first number to target number.
52 : ''' </summary>
53 : ''' <param name="firstNumber">The first number.</param>
54 : ''' <param name="targetNumber">The target number.</param>
55 : ''' <returns>New value of first number</returns>
56 : Private Function StepCloser(ByVal firstNumber As Integer, ByVal targetNumber As Integer) As Integer

58 : If firstNumber < targetNumber Then
59 : firstNumber += StepSpeed
60 : If firstNumber > targetNumber Then firstNumber = targetNumber

62 : ElseIf firstNumber > targetNumber Then
63 : firstNumber -= StepSpeed
64 : If firstNumber < targetNumber Then firstNumber = targetNumber

66 : End If

68 : Return firstNumber
69 : End Function


72 : Private Delegate Sub DelegateChangeColor(ByVal Color As Color, ByVal progressBar As ToolStripProgressBar)
73 : ''' <summary>
74 : ''' Changes the progressbar color.
75 : ''' </summary>
76 : ''' <param name="color">The color.</param>
77 : ''' <param name="progressBar">The progress bar.</param>
78 : Private Sub ChangeColor(ByVal color As Color, ByVal progressBar As ToolStripProgressBar)
79 : If InvokeRequired Then
80 : Invoke(New DelegateChangeColor(AddressOf ChangeColor), color, progressBar)

82 : Else
83 : progressBar.ForeColor = color
84 : End If
85 : End Sub

87 : %>
Source Script is modified version of HiLiter
Ferruh Mavituna
© 2002-2007, Ferruh Mavituna

Sabit IP Adresi : 81.22.99.133, SSL Erişimi, Hakkında