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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
| '----------------------------------------------------------------------------------------------------
'CURSEUR CELLULE
'----------------------------------------------------------------------------------------------------
Private Sub Selector(DGV As DataGridView, PN As Panel)
'----------------------------------------------------------------------------------------------------
'DECLARATIONS VARIABLES
'----------------------------------------------------------------------------------------------------
Dim Ep As Int16 = 2 'Epaisseur contour
Dim Rect, RectRow, RectCol, RectArea As Rectangle
Dim SelCol, SelRow, TmpWidth, TmpHeight, TmpAreaRow, TmpAreaColumn, CalcColContigu(1), CalcRowContigu(2) As Integer
Dim NotContigu, NTop, NBottom, NLeft, NRight As Boolean
Dim Area As New DataTable
Dim Addcolumn As DataColumn
Dim Addrow As DataRow
'----------------------------------------------------------------------------------------------------
'EXECUTION
'----------------------------------------------------------------------------------------------------
If IsNothing(DGV.CurrentRow) OrElse DGV.Rows.Count = 0 Then Return
PN.Parent = DGV
PN.BackColor = Color.FromArgb(0, 198, 198, 198)
PN.Visible = True
SelCol = DGV.ColumnCount - 1
SelRow = DGV.RowCount - 1
'Détermination du plus petit index cellule
For Each Cell As DataGridViewCell In DGV.SelectedCells
If Cell.ColumnIndex < SelCol Then SelCol = Cell.ColumnIndex
If Cell.RowIndex < SelRow Then SelRow = Cell.RowIndex
'Mise en mémoire zone contigu
If Area.Columns.Contains(CStr(Cell.ColumnIndex)) = False Then
Addcolumn = New DataColumn
Addcolumn.DataType = System.Type.GetType("System.Double")
Addcolumn.DefaultValue = 0.0
Addcolumn.ColumnName = CStr(Cell.ColumnIndex)
Area.Columns.Add(Addcolumn)
If Area.Rows.Count = 0 Then
Area.Rows.Add()
End If
Addrow = Area.Rows(0)
Addrow(CStr(Cell.ColumnIndex)) += Cell.RowIndex
Else
Addrow(CStr(Cell.ColumnIndex)) += Cell.RowIndex
End If
If Cell.RowIndex > TmpAreaRow Then
TmpAreaRow = Cell.RowIndex
End If
Next
If IsNothing(Addrow) Then
PN.Visible = False
Return
End If
'Calcul zone contigu des lignes
For Each item In Addrow.ItemArray
If CalcRowContigu(0) = 0 Then
CalcRowContigu(0) = item
ElseIf CalcRowContigu(0) <> item Then
NotContigu = True
GoTo DrawRect
End If
Next
For i = SelRow To TmpAreaRow
Do Until DGV.Rows(i).Visible = True
CalcRowContigu(2) += 1
Loop
CalcRowContigu(1) += i + CalcRowContigu(2)
Next
If CalcRowContigu(0) <> CalcRowContigu(1) Then
NotContigu = True
GoTo DrawRect
End If
'Calcul taille zone colonne pour comparaison
For i = 0 To Area.Columns.Count - 1
Do Until SelCol + i + TmpAreaColumn >= DGV.Columns.Count OrElse DGV.Columns(SelCol + i + TmpAreaColumn).Visible = True _
Or Area.Columns.Contains(CStr(SelCol + i + TmpAreaColumn)) = True
TmpAreaColumn += 1
Loop
If i = 0 And TmpAreaColumn <> 0 Then TmpAreaColumn = 0
CalcColContigu(1) += SelCol + i + TmpAreaColumn
CalcColContigu(0) += CInt(Area.Columns(i).ColumnName)
Next
If CalcColContigu(0) <> CalcColContigu(1) Then
NotContigu = True
End If
'Taille de la zone sélectionné
For Each Cell As DataGridViewCell In DGV.SelectedCells
If Cell.ColumnIndex = SelCol Then
RectArea = DGV.GetRowDisplayRectangle(Cell.RowIndex, False)
TmpHeight += RectArea.Height
End If
If Cell.RowIndex = SelRow And DGV.Columns(Cell.ColumnIndex).Visible = True Then
RectArea = DGV.GetColumnDisplayRectangle(Cell.ColumnIndex, False)
If RectArea.Width <> DGV.Columns(Cell.ColumnIndex).Width Then
NLeft = True
End If
TmpWidth += RectArea.Width
End If
Next
'Contrôle première ligne afficher à l'écran
RectArea = DGV.GetRowDisplayRectangle(SelRow, False)
If DGV.Rows(SelRow).Height <> RectArea.Height Then
NTop = True
End If
DrawRect:
If NotContigu = False Then
'Récupération des coordonnées de la zone pointé
Rect = DGV.GetCellDisplayRectangle(SelCol, SelRow, False)
If Rect.X = 0 Then
RectRow = DGV.GetRowDisplayRectangle(SelRow, False)
If RectRow.Y = 0 Then RectRow.Y += DGV.ColumnHeadersHeight
End If
If Rect.Y = 0 Then
RectCol = DGV.GetColumnDisplayRectangle(SelCol, False)
If RectCol.X = 0 Then RectCol.X += DGV.RowHeadersWidth
End If
Rect.X += RectCol.X - Ep - 1
Rect.Y += RectRow.Y - Ep - 1
If DGV.SelectedCells.Count > 1 Then
Rect.Width = TmpWidth
Rect.Height = TmpHeight
End If
Else
'Récupération des coordonnées de la zone pointé
Rect = DGV.GetCellDisplayRectangle(DGV.CurrentCell.ColumnIndex, DGV.CurrentRow.Index, False)
If Rect.X = 0 And Rect.Y = 0 Then
RectRow = DGV.GetRowDisplayRectangle(DGV.CurrentRow.Index, False)
RectRow.X += DGV.RowHeadersWidth
End If
'Calcul offset rectangle
Rect.X += RectRow.X - 1
Rect.Y += RectRow.Y - 1
Rect.Width += 1
Rect.Height += 1
End If
If Rect.Width <= 0 Or Rect.Height <= 0 Then PN.Visible = False
Rect.Width += (Ep * 2) + 1
If NTop = False Then
Rect.Height += (Ep * 2) + 1
Else
Rect.Height += (Ep * 2) + Ep
End If
'Mise à jour représentation contour
G_Cursor_NotContigu = NotContigu
G_Cursor_NTop = NTop
G_Cursor_NBottom = NBottom
G_Cursor_NLeft = NLeft
G_Cursor_NRight = NRight
'Mise à jour épaisseur contour
G_Cursor_Ep = Ep
'Mise en forme curseur
PN.Size = New System.Drawing.Size(Rect.Width, Rect.Height)
PN.Location = New System.Drawing.Point(Rect.X, Rect.Y)
PN.Refresh()
End Sub
Private Sub PN_Cursor_Paint(sender As Object, e As PaintEventArgs) Handles PN_Cursor.Paint
'----------------------------------------------------------------------------------------------------
'DECLARATIONS VARIABLES
'----------------------------------------------------------------------------------------------------
Dim Ep As Int16 = G_Cursor_Ep
Using p As Pen = New Pen(Color.FromArgb(0, 176, 240), Ep)
Dim G As Graphics = e.Graphics
Dim DGV As DataGridView = DGV_Work
Dim Rect As Rectangle = PN_Cursor.DisplayRectangle
Dim NotContigu As Boolean = G_Cursor_NotContigu
Dim NTop As Boolean = G_Cursor_NTop
Dim NBottom As Boolean = G_Cursor_NBottom
Dim NLeft As Boolean = G_Cursor_NLeft
Dim NRight As Boolean = G_Cursor_NRight
Dim TmpPoint As Point
Dim TmpSize As Integer
'----------------------------------------------------------------------------------------------------
'EXECUTION
'----------------------------------------------------------------------------------------------------
If NotContigu = False Then
'Trace ligne de contour bleu
TmpPoint = New Point(Rect.X, Rect.Y + (Ep / 2))
TmpSize = Rect.X + Rect.Width
If NTop = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpSize, TmpPoint.Y)
TmpPoint.Y = Rect.Y + Rect.Height - (Ep / 2)
If NBottom = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpSize, TmpPoint.Y)
TmpPoint = New Point(Rect.X, Rect.Y + (Ep / 2))
TmpSize = Rect.X + Rect.Height
If NLeft = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpPoint.Y, TmpSize)
TmpPoint.X = Rect.X + Rect.Width - (Ep / 2)
If NRight = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpPoint.X, TmpSize)
'Trace ligne de contour blanche
p.Color = Color.White
p.Width = 1
TmpPoint = New Point(Rect.X + Ep, Rect.Y + Ep)
TmpSize = Rect.X + Rect.Width - (Ep * 2)
If NTop = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpSize, TmpPoint.Y)
TmpPoint.Y = Rect.Y + Rect.Height - (Ep + p.Width)
If NBottom = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpSize, TmpPoint.Y)
TmpPoint = New Point(Rect.X + Ep, Rect.Y + Ep)
TmpSize = Rect.X + Rect.Height - (Ep * 2)
If NLeft = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpPoint.Y, TmpSize)
TmpPoint.X = Rect.X + Rect.Width - (Ep + p.Width)
If NRight = False Then G.DrawLine(p, TmpPoint.X, TmpPoint.Y, TmpPoint.X, TmpSize)
Else
PN_Cursor.Visible = False
End If
End Using
End Sub
Private Sub PN_Cursor_Click(sender As Object, ByVal e As MouseEventArgs) Handles PN_Cursor.Click
'----------------------------------------------------------------------------------------------------
'DECLARATIONS VARIABLES
'----------------------------------------------------------------------------------------------------
Dim DGV As DataGridView = DGV_Work
Dim RectCell As Rectangle
Dim Loc As Point
'----------------------------------------------------------------------------------------------------
'EXECUTION
'----------------------------------------------------------------------------------------------------
For Each Cells As DataGridViewCell In DGV.SelectedCells
'Récupération coordonnées des cellules dans la sélection
RectCell = DGV.GetCellDisplayRectangle(Cells.ColumnIndex, Cells.RowIndex, False)
'Récupération des coordonnées du click
Loc = e.Location
'Offset par rapport au DGV
Loc.X += PN_Cursor.Location.X
Loc.Y += PN_Cursor.Location.Y
'Comparaison des coordonnées
If Loc.X >= RectCell.X And Loc.X <= RectCell.X + RectCell.Width And Loc.Y >= RectCell.Y And Loc.Y <= RectCell.Y + RectCell.Height Then
DGV.CurrentCell = DGV(Cells.ColumnIndex, Cells.RowIndex)
Exit For
End If
Next
Call Selector(DGV, PN_Cursor)
End Sub
Private Sub PN_Cursor_DoubleClick(sender As Object, ByVal e As MouseEventArgs) Handles PN_Cursor.DoubleClick
'----------------------------------------------------------------------------------------------------
'DECLARATIONS VARIABLES
'----------------------------------------------------------------------------------------------------
Dim DGV As DataGridView = DGV_Work
Dim RectCell As Rectangle
Dim Loc As Point
'----------------------------------------------------------------------------------------------------
'EXECUTION
'----------------------------------------------------------------------------------------------------
For Each Cells As DataGridViewCell In DGV.SelectedCells
'Récupération coordonnées des cellules dans la sélection
RectCell = DGV.GetCellDisplayRectangle(Cells.ColumnIndex, Cells.RowIndex, False)
'Récupération des coordonnées du click
Loc = e.Location
'Offset par rapport au DGV
Loc.X += PN_Cursor.Location.X
Loc.Y += PN_Cursor.Location.Y
'Comparaison des coordonnées
If Loc.X >= RectCell.X And Loc.X <= RectCell.X + RectCell.Width And Loc.Y >= RectCell.Y And Loc.Y <= RectCell.Y + RectCell.Height Then
DGV.CurrentCell = DGV(Cells.ColumnIndex, Cells.RowIndex)
DGV.BeginEdit(True)
PN_Cursor.Visible = False
Exit For
End If
Next
End Sub
'----------------------------------------------------------------------------------------------------
'MISE A JOUR CURSEUR CELLULE
'----------------------------------------------------------------------------------------------------
Private Sub DGV_Work_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles DGV_Work.ColumnWidthChanged
Call Selector(DGV_Work, PN_Cursor)
End Sub
Private Sub DGV_Work_Scroll(sender As Object, e As ScrollEventArgs) Handles DGV_Work.Scroll
G_MajCursor = True
End Sub
Private Sub DGV_Work_RowHeightChanged(sender As Object, e As DataGridViewRowEventArgs) Handles DGV_Work.RowHeightChanged
Call Selector(DGV_Work, PN_Cursor)
End Sub
Private Sub DGV_Work_SelectionChanged(sender As Object, e As EventArgs) Handles DGV_Work.SelectionChanged
Call Selector(DGV_Work, PN_Cursor)
End Sub
Private Sub DGV_Work_RowHeadersWidthChanged(sender As Object, e As EventArgs) Handles DGV_Work.RowHeadersWidthChanged
Call Selector(DGV_Work, PN_Cursor)
End Sub
Private Sub DGV_Work_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV_Work.CellEndEdit
Call Selector(DGV_Work, PN_Cursor)
End Sub
Private Sub DGV_Work_Paint(sender As Object, e As PaintEventArgs) Handles DGV_Work.Paint
If G_MajCursor = True Then
G_MajCursor = False
Call Selector(DGV_Work, PN_Cursor)
End If
End Sub
Private Sub DGV_Work_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DGV_Work.CellPainting
If Not IsNothing(DGV_Work.CurrentCell) Then
If e.ColumnIndex = DGV_Work.CurrentCell.ColumnIndex And e.RowIndex = DGV_Work.CurrentCell.RowIndex _
And G_Cursor_NotContigu = False Then
e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
Else
e.CellStyle.SelectionBackColor = Color.FromArgb(e.CellStyle.BackColor.R / 1.29, e.CellStyle.BackColor.G / 1.29, e.CellStyle.BackColor.B / 1.29)
End If
Else
e.CellStyle.SelectionBackColor = Color.FromArgb(e.CellStyle.BackColor.R / 1.29, e.CellStyle.BackColor.G / 1.29, e.CellStyle.BackColor.B / 1.29)
End If
End Sub
Private Sub Select_change(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs) Handles DGV_Work.CellBeginEdit
PN_Cursor.Visible = False
End Sub |
Partager