1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| Public Class PlagesHoraires
Inherits Control
Public Sub New()
For i As Integer = 1 To 24 * 4
_QuartsDheures.Add(False)
Next
Me.DoubleBuffered = True
Me.MinimumSize = New System.Drawing.Size(730, 34)
_BrushOn = New SolidBrush(_ColorOn)
_BrushOff = New SolidBrush(_ColorOff)
End Sub
Private _rectsQH As New List(Of Rectangle)
Private _BrushOn As SolidBrush
Private _BrushOff As SolidBrush
Private _borderPen As New Pen(Color.Black)
Private _brushHours As New SolidBrush(Color.Black)
' properties
Private _QuartsDheures As New List(Of Boolean)
Public ReadOnly Property QuartsDheures As IEnumerable(Of Boolean)
Get
Return _QuartsDheures
End Get
End Property
Public Property QuartDheureValue(index As Integer) As Boolean
Get
Return _QuartsDheures(index)
End Get
Set(value As Boolean)
_QuartsDheures(index) = value
Me.Invalidate()
End Set
End Property
Private _ColorOn As Color = Color.Green
Public Property ColorOn As Color
Get
Return _ColorOn
End Get
Set(value As Color)
_ColorOn = value
_BrushOn = New SolidBrush(value)
Me.Invalidate()
End Set
End Property
Private _ColorOff As Color = Color.DarkGray
Public Property ColorOff As Color
Get
Return _ColorOff
End Get
Set(value As Color)
_ColorOff = value
_BrushOff = New SolidBrush(value)
Me.Invalidate()
End Set
End Property
Private _TextWidth As Integer = 40
Public Property TextWidth As Integer
Get
Return _TextWidth
End Get
Set(value As Integer)
_TextWidth = value
RecalcRects()
Me.Invalidate()
End Set
End Property
' calc and draw
Protected Overrides Sub OnSizeChanged(e As EventArgs)
MyBase.OnSizeChanged(e)
RecalcRects()
End Sub
Private Sub RecalcRects()
_rectsQH.Clear()
Dim widthQh = (Me.Width - Me.Margin.Horizontal - Me.TextWidth) \ 24 * 4
Dim heightRect = Math.Max(widthQh, 16)
For i As Integer = 0 To _QuartsDheures.Count - 1
Dim b = Me._BrushOff
If _QuartsDheures(i) Then b = Me._BrushOn
_rectsQH.Add(New System.Drawing.Rectangle(Me.TextWidth + Me.Margin.Left + i * widthQh, Me.Height - heightRect - 1, widthQh, heightRect))
Next
End Sub
Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
pevent.Graphics.Clear(Me.BackColor)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
If Me.Text <> "" Then
Using b As New SolidBrush(Me.ForeColor)
Dim p As New System.Drawing.Point(Me.Margin.Left, _rectsQH(0).Y + 1)
e.Graphics.DrawString(Me.Text, Me.Font, b, p)
End Using
End If
For i As Integer = 0 To _rectsQH.Count - 1
Dim b = Me._BrushOff
If _QuartsDheures(i) Then b = Me._BrushOn
Dim rect = _rectsQH(i)
e.Graphics.FillRectangle(b, rect)
e.Graphics.DrawRectangle(_borderPen, rect)
If i / 4 = i \ 4 Then
e.Graphics.DrawLine(_borderPen, rect.X, rect.Y - 4, rect.X, rect.Y + 4)
End If
If i / 12 = i \ 12 Then
Dim midWidth = 4
If (i / 4) > 9 Then midWidth = 8
Dim p As New System.Drawing.Point(rect.X - midWidth, rect.Y - 18)
e.Graphics.DrawString((i / 4).ToString, Me.Font, _brushHours, p)
End If
Next
Dim p24 As New System.Drawing.Point(_rectsQH.Last.X + _rectsQH.Last.Width - 8, _rectsQH.Last.Y - 18)
e.Graphics.DrawString("24", Me.Font, _brushHours, p24)
End Sub
' change color
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
GestionClick(e)
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
GestionClick(e)
End Sub
Private Sub GestionClick(e As MouseEventArgs)
Dim newValue As Boolean
Select Case e.Button
Case MouseButtons.Left
newValue = True
Case MouseButtons.Right
newValue = False
Case Else
Exit Sub
End Select
For i As Integer = 0 To _QuartsDheures.Count - 1
If _rectsQH(i).Contains(e.Location) Then
If _QuartsDheures(i) = newValue Then Continue For
_QuartsDheures(i) = newValue
Me.Invalidate()
End If
Next
End Sub
End Class |
Partager