Torna al Thread
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Namespace YourNamespace
Partial Public Class MyControl
Inherits Control
#Region "Declarations"
''' <summary>
''' Determina la colorazione dello sfondo
''' </summary>
Dim backBrush As LinearGradientBrush
''' <summary>
''' Usato per disegnare le linee e il testo [testo da implementare]
''' </summary>
Dim linesPen As Pen
''' <summary>
''' La linea superiore che determina il range di selezione
''' </summary>
Dim upperLine() As PointF
''' <summary>
''' L'altezza della linea in alto
''' </summary>
Dim upperLineHeight_field As Integer = 5
''' <summary>
''' Il punto di partenza (in percentuale) del range di selezione
''' </summary>
Dim startRangePercent_field As Single = 0
''' <summary>
''' Il termine del range di selezione(in percentuale)
''' </summary>
Dim endRangePercent_field As Single = 100
''' <summary>
''' L'altezza dello spazio riservato al disegno dei segnaposti
''' </summary>
Dim bottomSpace As Integer = 10
''' <summary>
''' L'altezza dei triangoli segnaposto
''' </summary>
Dim triangleH As Integer = 7
''' <summary>
''' Lo spazio riservato sulla sinistra
''' </summary>
Dim spaceLeft_field As Integer = 10
''' <summary>
''' Lo spazio riservato sulla destra
''' </summary>
Dim spaceRight_field As Integer = 10
''' <summary>
''' Il rettangolo nel quale viene disegnato il background
''' </summary>
Dim backgroundRect As Rectangle
''' <summary>
''' Usato per colorare lo sfondo
''' </summary>
Dim colorBlend As ColorBlend
''' <summary>
''' Il valore minimo sulla scala
''' </summary>
Dim minimum_field As Single = 0
''' <summary>
''' Il valore massimo sulla scala
''' </summary>
Dim maximum_field As Single = 100
''' <summary>
''' Il valore corrente in percentuale
''' </summary>
Dim valuePercent_field As Single
''' <summary>
''' Il valore corrente
''' </summary>
Dim value_ As Single
''' <summary>
''' Il range coperto fra Minimum e Maximum
''' </summary>
Private hLen As Single
Private leftPxBeforeStart As Integer, rightPxToEnd As Integer
#End Region
Public Sub New()
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.ResizeRedraw, True)
colorBlend = New ColorBlend(7)
colorBlend.Colors(0) = Color.Red
colorBlend.Colors(1) = Color.Yellow
colorBlend.Colors(2) = Color.FromArgb(0, 255, 64)
colorBlend.Colors(3) = Color.FromArgb(115, 240, 158)
colorBlend.Colors(4) = Color.FromArgb(0, 255, 64)
colorBlend.Colors(5) = Color.Yellow
colorBlend.Colors(6) = Color.Red
Dim i As Integer
For i = 0 To colorBlend.Positions.Length - 1 Step i + 1
colorBlend.Positions(i) = i / CType((colorBlend.Positions.Length - 1), Single)
Next
linesPen = New Pen(Color.Gray, 1.0F)
setUpperLine()
setBackgroundInfo()
'InitializeComponent()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'Disegno la linea superiore
e.Graphics.DrawLines(linesPen, upperLine)
Dim width As Integer = Me.Width - spaceLeft_field - spaceRight_field - 1
Dim startx As Single = width * startRangePercent_field / 100 + spaceLeft_field
Dim endx As Single = width * endRangePercent_field / 100 + spaceLeft_field
'Disegno i triangoli che determinano il range di selezione iniziale e finale
e.Graphics.DrawPolygon(linesPen, getTriangle(startx, Me.Height - bottomSpace))
e.Graphics.DrawPolygon(linesPen, getTriangle(endx, Me.Height - bottomSpace))
Dim x As Single = width * valuePercent_field / 100 + spaceLeft_field
'Disegno il triangolo che determina la posizione attuale
e.Graphics.DrawPolygon(linesPen, getTriangle(x, Me.Height - bottomSpace))
'Disegno il background
e.Graphics.FillRectangle(backBrush, backgroundRect)
End Sub
''' <summary>
''' Ottiene o imposta il valore corrente
''' </summary>
Public Property Value() As Single
Get
Return value_
End Get
Set(ByVal Value As Single)
If Value < minimum_field Then
Me.value_ = minimum_field
ElseIf Value > maximum_field Then
Me.value_ = maximum_field
Else
Me.value_ = Value
End If
validateValuePercent()
Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta il valore corrente in percentuale
''' </summary>
Public Property ValuePercent() As Single
Get
Return valuePercent_field
End Get
Set(ByVal Value As Single)
If Value < startRangePercent_field Then
Me.valuePercent_field = startRangePercent_field
ElseIf Value > endRangePercent_field Then
Me.valuePercent_field = endRangePercent_field
Else
Me.valuePercent_field = Value
End If
validateValue()
Invalidate()
End Set
End Property
''' <summary>
''' Convalida il valore di value_ in base a valuePercent
''' </summary>
Private Sub validateValue()
value_ = hLen * valuePercent_field / 100 + minimum_field
End Sub
''' <summary>
''' Convalida il valore di valuepercent in base a value_
''' </summary>
Private Sub validateValuePercent()
valuePercent_field = ((value_ - minimum_field) * 100) / hLen
End Sub
''' <summary>
''' Il valore minimo sulla scala
''' </summary>
Public Property Minimum() As Single
Get
Return Me.minimum_field
End Get
Set(ByVal Value As Single)
If Value > maximum_field Then
maximum_field = Value
End If
Me.minimum_field = Value
hLen = maximum_field - minimum_field
validateValue()
setUpperLine()
Invalidate()
End Set
End Property
''' <summary>
''' Il valore massimo sulla scala
''' </summary>
Public Property Maximum() As Single
Get
Return Me.maximum_field
End Get
Set(ByVal Value As Single)
If Value < minimum_field Then
minimum_field = Value
End If
Me.maximum_field = Value
hLen = maximum_field - minimum_field
validateValue()
setUpperLine()
Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta il valore percentuale in cui inizia il range di selezione
''' </summary>
Public Property StartRangePercent() As Single
Get
Return Me.startRangePercent_field
End Get
Set(ByVal Value As Single)
If Value < 0 Or Value > 100 Then
Throw New ArgumentException("StartRangePercent deve essere compreso fra 0 e 100")
End If
If Value > endRangePercent_field Then
endRangePercent_field = Value
End If
Me.startRangePercent_field = Value
validateValue()
setUpperLine()
Me.Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta il valore percentuale in cui finisce il range di selezione
''' </summary>
Public Property EndRangePercent() As Single
Get
Return Me.endRangePercent_field
End Get
Set(ByVal Value As Single)
If Value < 0 Or Value > 100 Then
Throw New ArgumentException("EndRangePercent deve essere compreso fra 0 e 100")
End If
If Value < startRangePercent_field Then
startRangePercent_field = Value
End If
Me.endRangePercent_field = Value
validateValue()
setUpperLine()
Me.Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta l'altezza in pixel della linea di selezione range
''' </summary>
Public Property UpperLineHeight() As Integer
Get
Return upperLineHeight_field
End Get
Set(ByVal Value As Integer)
If Value < 0 Then
Throw New ArgumentException("UpperLineHeight deve essere un numero positivo")
End If
If Value > (Me.Height - bottomSpace - 5) Then
Return
End If
upperLineHeight_field = Value
setUpperLine()
setBackgroundInfo()
setMinimumSize()
Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta l'altezza in pixel dell'area di disegno dei segnaposti
''' </summary>
Public Property BottomSpaceHeight() As Integer
Get
Return bottomSpace
End Get
Set(ByVal Value As Integer)
If Value < 0 Then
Throw New ArgumentException("BottomSpaceHeight deve essere un numero positivo")
End If
If Value > (Me.Height - upperLineHeight_field - 5) Then
Return
End If
bottomSpace = Value
setBackgroundInfo()
setMinimumSize()
Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta l'altezza in pixel dei triangoli segnaposto
''' </summary>
Public Property TriangleHeight() As Integer
Get
Return triangleH
End Get
Set(ByVal Value As Integer)
If Value < 0 Then
Throw New ArgumentException("TriangleHeight deve essere un numero positivo")
End If
triangleH = Value
Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta la distanza in pixel dal bordo sinistro del controllo
''' alla quale viene iniziato il disegnamento
''' </summary>
Public Property SpaceLeft() As Integer
Get
Return spaceLeft_field
End Get
Set(ByVal Value As Integer)
If Value < 0 Then
Throw New ArgumentException("SpaceLeft deve essere un numero positivo")
End If
If Value > (Me.Width - spaceRight_field - 5) Then
Return
End If
spaceLeft_field = Value
setBackgroundInfo()
setMinimumSize()
Invalidate()
End Set
End Property
''' <summary>
''' Ottiene o imposta la distanza in pixel dal bordo destro del controllo
''' alla quale viene iniziato il disegnamento
''' </summary>
Public Property SpaceRight() As Integer
Get
Return spaceRight_field
End Get
Set(ByVal Value As Integer)
If Value < 0 Then
Throw New ArgumentException("SpaceRight deve essere un numero positivo")
End If
If Value > (Me.Width - spaceLeft_field - 5) Then
Return
End If
spaceRight_field = Value
setBackgroundInfo()
setMinimumSize()
Invalidate()
End Set
End Property
Private Function getTriangle(ByVal xPos As Single, ByVal yPos As Single) As PointF()
Return getTriangle(xPos, yPos, triangleH)
End Function
Private Function getTriangle(ByVal xPos As Single, ByVal yPos As Single, ByVal height As Single) As PointF()
Dim upPoint As PointF = New PointF(xPos, yPos)
Dim mezzoLato As Single = CType((height / Math.Sqrt(3)), Single)
Dim leftPt As PointF = New PointF(xPos - mezzoLato, yPos + height)
Dim rightPt As PointF = New PointF(xPos + mezzoLato, yPos + height)
Return New PointF() {upPoint, leftPt, rightPt}
End Function
Private Sub setUpperLine()
Dim width As Integer = Me.Width - spaceLeft_field - spaceRight_field - 1
Dim startx As Single = width * startRangePercent_field / 100 + spaceLeft_field
Dim endx As Single = width * endRangePercent_field / 100 + spaceLeft_field
leftPxBeforeStart = CType(startx, Integer)
rightPxToEnd = CType(endx, Integer)
upperLine = New PointF() {New PointF(startx, upperLineHeight_field), New PointF(startx, 0), New PointF(endx, 0), New PointF(endx, upperLineHeight_field)}
End Sub
Private Sub setBackgroundInfo()
Dim width As Integer = Me.Width - 1
backgroundRect = New Rectangle(spaceLeft_field, upperLineHeight_field, Me.Width - spaceLeft_field - spaceRight_field, Me.Height - upperLineHeight_field - bottomSpace)
If backgroundRect.Width <= 0 Or backgroundRect.Height <= 0 Then
Return
End If
backBrush = New LinearGradientBrush(backgroundRect, colorBlend.Colors(0), colorBlend.Colors(4), LinearGradientMode.Horizontal)
backBrush.InterpolationColors = colorBlend
End Sub
''' <summary>
''' Imposta la dimensione minima come dovuto se minore del necessario
''' </summary>
Private Sub setMinimumSize()
Dim w As Integer = MyBase.MinimumSize.Width
Dim h As Integer = MyBase.MinimumSize.Height
If w < spaceLeft_field + spaceRight_field + 5 Then
w = spaceLeft_field + spaceRight_field + 5
End If
If h < upperLineHeight_field + bottomSpace + 5 Then
h = upperLineHeight_field + bottomSpace + 5
End If
MyBase.MinimumSize = New Size(w, h)
End Sub
Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
setUpperLine()
setBackgroundInfo()
Invalidate()
End Sub
Public Overrides Property MinimumSize() As Size
Get
Return MyBase.MinimumSize
End Get
Set(ByVal Value As Size)
MyBase.MinimumSize = Value
setMinimumSize()
End Set
End Property
''Rimuovi questo commento se vuoi poter spostare il segnaposto col muose
'protected override void OnMouseMove(MouseEventArgs e)
'{
' if (e.Button == MouseButtons.Left && e.X >= leftPxBeforeStart - 3 && e.X <= rightPxToEnd + 3)
' {
' int tmp = e.X - spaceLeft;
' this.ValuePercent = (100 * tmp) / (float)(this.Width - spaceLeft - spaceRight);
' Invalidate();
' }
' base.OnMouseMove(e);
'}
End Class
End Namespace
'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net)
'----------------------------------------------------------------