Voici comment réaliser un graduation couleur.
aller dans GraduationColor pour ajouter plusieurs couleur comme un rainbows.
exemple : Red,Orange,Yellow,Lime,Bleu,Indigo,Violet
voici ma classe :
Imports System.Drawing.Drawing2D
Public Class GradationSlider : Inherits Control
Public Event ValueChanged As EventHandler
Private _maximum As Integer = 100
Public Property Maximum As Integer
Set(ByVal value As Integer)
Me._maximum = value
End Set
Get
Return Me._maximum
End Get
End Property
Public Property Minimum As Integer = 0
Private _value As Integer
Public Property Value As Integer
Set(ByVal value As Integer)
Me._value = value
If ValueChangedEvent IsNot Nothing AndAlso value >= Minimum AndAlso value <= Maximum Then RaiseEvent ValueChanged(Me, New EventArgs())
Me.Refresh()
End Set
Get
Return Me._value
End Get
End Property
Private _gradationColor As Color() ' color rainbow : Red,Orange,Yellow,Lime,Bleu,Indigo,Violet
Public Property GradationColor As Color()
Set(ByVal value As Color())
Me._gradationColor = value
drawGradation()
Me.Refresh()
End Set
Get
Return Me._gradationColor
End Get
End Property
Public Sub New()
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
AddHandler Me.Resize, New System.EventHandler(AddressOf Me.CSlider_Resize)
AddHandler Me.MouseMove, New MouseEventHandler(AddressOf Me.CSlider_MouseMove)
AddHandler Me.mouseDown, New System.Windows.Forms.MouseEventHandler(AddressOf Me.CSlider_MouseDown)
AddHandler Me.MouseUp, New System.Windows.Forms.MouseEventHandler(AddressOf Me.CSlider_MouseUp)
AddHandler Me.MouseLeave, New System.EventHandler(AddressOf Me.CSlider_MouseLeave)
AddHandler Me.MouseEnter, New System.EventHandler(AddressOf Me.CSlider_MouseEnter)
drawTick()
Me.Size = New Size(300, 40)
End Sub
Private tick As Bitmap
Private Sub drawTick()
tick = New Bitmap(20, 20)
Dim g As Graphics = Graphics.FromImage(tick)
g.SmoothingMode = SmoothingMode.HighQuality
Dim point As Point() = {New Point(5, 1), New Point(9, 5), New Point(9, 7), New Point(8, 8), New Point(2, 8), New Point(1, 7), New Point(1, 5), New Point(5, 1)}
g.FillPolygon(New SolidBrush(Color.White), point)
g.DrawLines(New Pen(Color.Black, 1), point)
g.Dispose()
End Sub
Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
MyBase.OnPaint(pe)
drawGradation()
pe.Graphics.DrawImage(gradation, 5, 0, gradation.Width, gradation.Height)
pe.Graphics.DrawImage(tick, CInt((Value * gradation.Width) / (Maximum - Minimum)), gradation.Height)
End Sub
Private gradation As Bitmap
Private Sub drawGradation()
If Me.Width > 10 AndAlso Me.Height > 10 Then
Dim gradationSize As Size = New Size(Me.Width - 10, Me.Height - 10)
gradation = New Bitmap(gradationSize.Width, gradationSize.Height)
Dim g As Graphics = Graphics.FromImage(gradation)
Dim lgb As New LinearGradientBrush(New Rectangle(0, 0, gradationSize.Width, gradationSize.Height), Color.Black, Color.White, LinearGradientMode.Horizontal)
If GradationColor IsNot Nothing AndAlso GradationColor.Length >= 2 Then
Dim cb As ColorBlend = New ColorBlend()
cb.Colors = GradationColor
Dim Position As Single() = New Single(GradationColor.Length - 1) {}
For i As Integer = 0 To GradationColor.Length - 1
Position(i) = (1.0F / (GradationColor.Length - 1)) * (i)
Next
cb.Positions = Position
lgb.InterpolationColors = cb
End If
g.FillRectangle(lgb, New Rectangle(0, 0, gradationSize.Width, gradationSize.Height))
g.DrawRectangle(New Pen(Color.Black, 1), 0, 0, gradationSize.Width - 1, gradationSize.Height - 1)
lgb.Dispose()
g.Dispose()
End If
End Sub
Private Sub CSlider_Resize(ByVal sender As Object, ByVal e As EventArgs)
drawGradation()
Me.Refresh()
End Sub
Private _mouseDown As Boolean = False
Private Sub CSlider_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Value = CInt(((CSng((e.X - 5)) / (Me.Width - 10)) * (Maximum - Minimum)))
If Value <= Minimum Then Value = Minimum
If Value >= Maximum Then Value = Maximum
RaiseEvent ValueChanged(Me, New EventArgs())
_mouseDown = True
End Sub
Private Sub CSlider_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If _mouseDown = True Then
Value = CInt(((CSng((e.X - 5)) / (Me.Width - 10)) * (Maximum - Minimum)))
If Value <= Minimum Then Value = Minimum
If Value >= Maximum Then Value = Maximum
RaiseEvent ValueChanged(Me, New EventArgs())
End If
End Sub
Private Sub CSlider_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
_mouseDown = False
End Sub
Private Sub CSlider_MouseEnter(ByVal sender As Object, ByVal e As EventArgs)
drawGradation()
drawTick()
Me.Refresh()
End Sub
Private Sub CSlider_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
drawGradation()
drawTick()
Me.Refresh()
End Sub
End Class
Aucun commentaire:
Enregistrer un commentaire