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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
| Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles
Imports System.ComponentModel
Public Class DataGridViewStarRateColumn
Inherits DataGridViewTextBoxColumn
Public Sub New()
Me.CellTemplate = New DataGridViewStarRateCell()
End Sub
<Category("Design")>
<Description("Largeur de l'étoile")>
Public Property StarWidth() As Integer
Get
If DirectCast(CellTemplate, DataGridViewStarRateCell) Is Nothing Then
Throw New InvalidOperationException("cell template required")
End If
Return DirectCast(CellTemplate, DataGridViewStarRateCell).StarWidth
End Get
Set(ByVal value As Integer)
If Me.StarWidth <> value Then
DirectCast(CellTemplate, DataGridViewStarRateCell).StarWidth = value
If Me.DataGridView IsNot Nothing Then
Dim cell As DataGridViewStarRateCell
For rowIndex As Integer = 0 To Me.DataGridView.Rows.Count - 1
cell = TryCast(Me.DataGridView.Rows.SharedRow(rowIndex).Cells(Me.Index), DataGridViewStarRateCell)
If cell IsNot Nothing Then cell.StarWidth = value
Next
Me.DataGridView.Refresh()
End If
End If
End Set
End Property
<Category("Design")>
<Description("Hauteur de l'étoile")>
Public Property StarHeight() As Integer
Get
If DirectCast(CellTemplate, DataGridViewStarRateCell) Is Nothing Then
Throw New InvalidOperationException("cell template required")
End If
Return DirectCast(CellTemplate, DataGridViewStarRateCell).StarHeight
End Get
Set(value As Integer)
If Me.StarHeight <> value Then
DirectCast(CellTemplate, DataGridViewStarRateCell).StarHeight = value
If Me.DataGridView IsNot Nothing Then
Dim cell As DataGridViewStarRateCell
For rowIndex As Integer = 0 To Me.DataGridView.Rows.Count - 1
cell = TryCast(Me.DataGridView.Rows.SharedRow(rowIndex).Cells(Me.Index), DataGridViewStarRateCell)
If cell IsNot Nothing Then cell.StarHeight = value
Next
Me.DataGridView.Refresh()
End If
End If
End Set
End Property
<Category("Design")>
<Description("Alignement des étoiles dans la cellule")>
Public Property StarAlignment() As DataGridViewContentAlignment
Get
If DirectCast(CellTemplate, DataGridViewStarRateCell) Is Nothing Then
Throw New InvalidOperationException("cell template required")
End If
Return DirectCast(CellTemplate, DataGridViewStarRateCell).StarAlignment
End Get
Set(value As DataGridViewContentAlignment)
If Me.StarAlignment <> value Then
DirectCast(CellTemplate, DataGridViewStarRateCell).StarAlignment = value
If Me.DataGridView IsNot Nothing Then
Dim cell As DataGridViewStarRateCell
For rowIndex As Integer = 0 To Me.DataGridView.Rows.Count - 1
cell = TryCast(Me.DataGridView.Rows.SharedRow(rowIndex).Cells(Me.Index), DataGridViewStarRateCell)
If cell IsNot Nothing Then cell.StarHeight = value
Next
Me.DataGridView.Refresh()
End If
End If
End Set
End Property
<Category("Design")>
<Description("Image de l'étoile")>
Public Property StarImage() As Image
Get
If DirectCast(CellTemplate, DataGridViewStarRateCell) Is Nothing Then
Throw New InvalidOperationException("cell template required")
End If
Return DirectCast(CellTemplate, DataGridViewStarRateCell).StarImage
End Get
Set(value As Image)
If Me.StarImage IsNot value Then
DirectCast(CellTemplate, DataGridViewStarRateCell).StarImage = value
If Me.DataGridView IsNot Nothing Then
Dim cell As DataGridViewStarRateCell
For rowIndex As Integer = 0 To Me.DataGridView.Rows.Count - 1
cell = TryCast(Me.DataGridView.Rows.SharedRow(rowIndex).Cells(Me.Index), DataGridViewStarRateCell)
If cell IsNot Nothing Then cell.StarImage = value
Next
Me.DataGridView.Refresh()
End If
End If
End Set
End Property
Public Overrides Function GetPreferredWidth(autoSizeColumnMode As System.Windows.Forms.DataGridViewAutoSizeColumnMode, fixedHeight As Boolean) As Integer
GetPreferredWidth = MyBase.GetPreferredWidth(autoSizeColumnMode, fixedHeight)
Dim defaultcolumnwidth = GetPreferredWidth
Dim starwidth As Integer = Me.StarWidth
Dim maxvalue As Integer
For rowIndex As Integer = 0 To Me.DataGridView.Rows.Count - 1
If CInt(Me.DataGridView.Rows(rowIndex).Cells(Me.Index).Value) > maxvalue Then maxvalue = CInt(Me.DataGridView.Rows.SharedRow(rowIndex).Cells(Me.Index).Value)
Next
If maxvalue <> 0 Then
Select Case Me.DataGridView.ColumnHeadersDefaultCellStyle.WrapMode
Case DataGridViewTriState.True 'retour à la ligne du columnheadertext autorisé
GetPreferredWidth = maxvalue * starwidth 'on ne tient pas compte du header
Case Else 'retour à la ligne du columnheadertext interdit
GetPreferredWidth = Math.Max(maxvalue * starwidth, defaultcolumnwidth)
End Select
End If
End Function
End Class
Public Class DataGridViewStarRateCell
Inherits DataGridViewTextBoxCell
Private _starwidth As Integer = 20
Private _starheight As Integer = 20
Private _staralignment As DataGridViewContentAlignment = DataGridViewContentAlignment.TopLeft
Private _image As Image = My.Resources.star_xxl 'une étoile
Const KminValue As Integer = 5
Public Property StarWidth() As Integer
Get
Return _starwidth
End Get
Set(ByVal value As Integer)
If value < KminValue Then _starwidth = KminValue Else _starwidth = value
End Set
End Property
Public Property StarHeight() As Integer
Get
Return _starheight
End Get
Set(ByVal value As Integer)
If value < KminValue Then _starheight = KminValue Else _starheight = value
End Set
End Property
Public Property StarAlignment() As DataGridViewContentAlignment
Get
Return _staralignment
End Get
Set(ByVal value As DataGridViewContentAlignment)
_staralignment = value
End Set
End Property
Public Property StarImage() As Image
Get
Return _image
End Get
Set(value As Image)
_image = value
End Set
End Property
Public Overrides Function Clone() As Object
Dim Cell As DataGridViewStarRateCell = DirectCast(MyBase.Clone(), DataGridViewStarRateCell)
Cell.StarWidth = Me.StarWidth
Cell.StarHeight = Me.StarHeight
Cell.StarAlignment = Me.StarAlignment
Cell.StarImage = Me.StarImage
Return Cell
End Function
Protected Overrides Sub Paint(ByVal graphics As Graphics, _
ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, _
ByVal rowIndex As Integer, _
ByVal elementState As DataGridViewElementStates, _
ByVal value As Object, ByVal formattedValue As Object, _
ByVal errorText As String, _
ByVal cellStyle As DataGridViewCellStyle, _
ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
ByVal paintParts As DataGridViewPaintParts)
'Set the scaling mode to high quality (makes the stars look good at any size)
graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
' dessine le nombre d'étoiles correspondant à la valeur de la cellule
' le dessin est limité aux dimensions de la cellule
Dim starValue As Integer 'valeur de la note
Dim filledtouse As Image = _image
Dim height As Integer = filledtouse.Height
Dim width As Integer = filledtouse.Width
Dim destRectX, destRectY, destRectWidth, destRectHeight As Integer
Dim srcRectWidth, srcRectHeight As Integer
Dim destRect, srcRect As Rectangle
Dim oAlignment As DataGridViewContentAlignment
Dim sTronq As String = "..."
Dim sTronqSize As SizeF = graphics.MeasureString(sTronq, cellStyle.Font)
Dim iTronq As Single
starValue = CInt(value)
' réglage alignement des étoiles dans la cellule
Dim offsetX, offsetY As Integer
Select Case _staralignment
Case DataGridViewContentAlignment.BottomCenter
offsetX = CInt(Math.Max(0, (cellBounds.Width - starValue * _starwidth) / 2)) ' Center
offsetY = Math.Max(0, (cellBounds.Height - _starheight)) 'Bottom
oAlignment = DataGridViewContentAlignment.BottomRight
Case DataGridViewContentAlignment.BottomLeft
offsetX = 0 'left
offsetY = Math.Max(0, (cellBounds.Height - _starheight)) 'Bottom
oAlignment = DataGridViewContentAlignment.BottomRight
Case DataGridViewContentAlignment.BottomRight
offsetX = Math.Max(0, cellBounds.Width - starValue * _starwidth) 'Right
offsetY = Math.Max(0, (cellBounds.Height - _starheight)) 'Bottom
oAlignment = DataGridViewContentAlignment.BottomRight
Case DataGridViewContentAlignment.MiddleCenter
offsetX = CInt(Math.Max(0, (cellBounds.Width - starValue * _starwidth) / 2)) ' Center
offsetY = CInt(Math.Max(0, (cellBounds.Height - _starheight) / 2)) ' Middle
oAlignment = DataGridViewContentAlignment.MiddleRight
Case DataGridViewContentAlignment.MiddleLeft
offsetX = 0 'left
offsetY = CInt(Math.Max(0, (cellBounds.Height - _starheight) / 2)) ' Middle
oAlignment = DataGridViewContentAlignment.MiddleRight
Case DataGridViewContentAlignment.MiddleRight
offsetX = Math.Max(0, cellBounds.Width - starValue * _starwidth) 'Right
offsetY = CInt(Math.Max(0, (cellBounds.Height - _starheight) / 2)) ' Middle
oAlignment = DataGridViewContentAlignment.MiddleRight
Case DataGridViewContentAlignment.TopCenter
offsetX = CInt(Math.Max(0, (cellBounds.Width - starValue * _starwidth) / 2)) ' Center
offsetY = 0 'top
oAlignment = DataGridViewContentAlignment.TopRight
Case DataGridViewContentAlignment.TopLeft, DataGridViewContentAlignment.NotSet
offsetX = 0 'left
offsetY = 0 'top
oAlignment = DataGridViewContentAlignment.TopRight
Case DataGridViewContentAlignment.TopRight
offsetX = Math.Max(0, cellBounds.Width - starValue * _starwidth) 'Right
offsetY = 0 'top
oAlignment = DataGridViewContentAlignment.TopRight
End Select
If _starwidth * starValue > cellBounds.Width Then ' La taille de la cellule est insuffisante pour tout afficher
cellStyle.Alignment = oAlignment
iTronq = sTronqSize.Width + 3 'padding right estimé à 3
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, sTronq, sTronq, errorText, cellStyle, advancedBorderStyle, paintParts)
Else
iTronq = 0
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, Nothing, Nothing, errorText, cellStyle, advancedBorderStyle, paintParts)
End If
For i = 1 To starValue
destRectX = cellBounds.X + offsetX + (i - 1) * _starwidth
destRectY = cellBounds.Y + offsetY
destRectWidth = Math.Min(_starwidth, cellBounds.Width - (i - 1) * _starwidth - CInt(iTronq)) - 1
destRectHeight = Math.Min(cellBounds.Height, _starheight) - 1
srcRectWidth = CInt(width / _starwidth * destRectWidth)
srcRectHeight = CInt(height * Math.Min(cellBounds.Height / _starheight, 1))
destRect = New Rectangle(destRectX, destRectY, destRectWidth, destRectHeight)
srcRect = New Rectangle(0, 0, srcRectWidth, srcRectHeight)
graphics.DrawImage(filledtouse, destRect, srcRect, GraphicsUnit.Pixel)
Next
End Sub
End Class |
Partager