IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Rectangle sur DGV, problème latence


Sujet :

VB.NET

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Analyste programmeur
    Inscrit en
    Juillet 2016
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Analyste programmeur
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juillet 2016
    Messages : 32
    Points : 30
    Points
    30
    Par défaut Rectangle sur DGV, problème latence
    Bonsoir la communauté,
    J'ai un problème de mise à jour d'affichage sur un DGV. Sur le DGV en question je dessine un rectangle autour des cellules sélectionné, lorsque ce rectangle est dessiné, l'ancien rectangle (celui qui entoure la sélection précédente) reste affiché 1/2 seconde se qui fait vraiment moche !
    Auriez-vous une solution miracle pour que la mise à jour de l'affichage soit plus propre ? Cela fait quelques jour que je cherche sur le web sans succès.

    Voilà le bout de code qui me permet de réaliser mon rectangle :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
        Private Sub DGV_Work_CellLeave(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DGV_Work.CellLeave
            DGV_Work.Invalidate()
        End Sub
     
        Private Sub DGV_Work_RowPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DGV_Work.RowPostPaint
            RectCellSelect()
        End Sub
     
    Private Sub RectCellSelect()
     
            If IsNothing(DGV_Work.CurrentRow) Then Return
     
     
            Using p As Pen = New Pen(Color.FromArgb(0, 176, 240), 3)
     
                Using myGraphics As Graphics = Graphics.FromHwnd(DGV_Work.Handle)
     
                    Dim Rect, RectRow As Rectangle
                    Dim SelCol, SelRow, TmpWidth, TmpHeight As Int16
     
                    SelCol = DGV_Work.ColumnCount
                    SelRow = DGV_Work.RowCount
     
                    'Détermination du plus petit index cellule
                    For Each Cell As DataGridViewCell In DGV_Work.SelectedCells
                        If Cell.ColumnIndex < SelCol Then SelCol = Cell.ColumnIndex
                        If Cell.RowIndex < SelRow Then SelRow = Cell.RowIndex
                    Next
     
                    'Taille de la zone sélectionné
                    For Each Cell As DataGridViewCell In DGV_Work.SelectedCells
                        If Cell.ColumnIndex = SelCol Then
                            TmpHeight += DGV_Work.Rows(Cell.RowIndex).Height
                        End If
     
                        If Cell.RowIndex = SelRow And DGV_Work.Columns(Cell.ColumnIndex).Visible = True Then
                            TmpWidth += DGV_Work.Columns(Cell.ColumnIndex).Width
                        End If
     
                    Next
     
                    'Récupération des X/Y de la zone pointé
                    Rect = DGV_Work.GetCellDisplayRectangle(SelCol, SelRow, False)
                    If Rect.X = 0 And Rect.Y = 0 Then
                        RectRow = DGV_Work.GetRowDisplayRectangle(SelRow, False)
                        RectRow.X += DGV_Work.RowHeadersWidth
                    End If
     
                    'Calcul offset rectangle
                    Rect.X += RectRow.X - 1
                    Rect.Y += RectRow.Y - 1
                    Rect.Width = TmpWidth + 1
                    Rect.Height = TmpHeight + 1
     
                    'Dessine rectangle bleu
                    myGraphics.DrawRectangle(p, Rect)
     
                    'Calcul offset 2éme rectangle dans le premier
                    p.Color = Color.White
                    p.Width = 1
                    Rect.X += 2
                    Rect.Y += 2
                    Rect.Width -= 4
                    Rect.Height -= 4
     
                    'Dessine 2éme rectangle
                    myGraphics.DrawRectangle(p, Rect)
     
                End Using
            End Using
     
        End Sub
    Nom : Capture.PNG
Affichages : 180
Taille : 5,9 Ko

    J'ai essayé le dgv.invalidate(rectangle) mais ça n'as pas l'air de fonctionner.

  2. #2
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 154
    Points : 25 072
    Points
    25 072
    Par défaut
    en théorie il faudrait déjà calculer le temps que met ta méthode à s'exécuter une fois
    pour ca il y a le system.diagnostics.stopwatch (.start, .stop, .ellapsedmilliseconds avec un point d'arrêt après le stop)
    => un temps supérieur à 100ms sur le thread graphique ca commence déjà à faire beaucoup

    on peut aussi vérifier combien de fois cette méthode est appelée (c'est surtout utile avec les events dont on ne sait pas d'où ca vient)
    pour ça moi je m’embête pas je mets un compteur dans la classe, je l'incrémente dans la méthode, puis je fais me.findform.text = i.tostring, donc j'ai le compteur en temps réel dans le titre de la fenetre
    => un nombre d'appels trop grand sur de l'affichage c'est pas normal, il faut éviter de faire les choses plusieurs fois par seconde

    m'enfin à vu de nez c'est l'event RowPostPaint qui est surement mal choisi, je ne le connais pas mais vu son nom si tu as 10 lignes il va être levé 10x, alors qu'un seul redessin à la fin de la "déselection" devrait suffire

    dis nous donc où ca cloche et on pourra t'aider à chercher des pistes (si tu n'as pas trouvé de solution tout seul ^^)
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  3. #3
    Expert éminent sénior

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 757
    Points : 10 697
    Points
    10 697
    Billets dans le blog
    21
    Par défaut
    Bonjour,

    Dans la mesure où le rectangle n'est pas propre à une ligne, mais plutôt au DGV, l'événement le plus approprié pour dessiner le rectangle serait OnPaint (et qui sera appelé moins souvent que RowPaint)
    François DORIN
    Consultant informatique : conception, modélisation, développement (C#/.Net et SQL Server)
    Site internet | Profils Viadéo & LinkedIn
    ---------
    Page de cours : fdorin.developpez.com
    ---------
    N'oubliez pas de consulter la FAQ C# ainsi que les cours et tutoriels

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Analyste programmeur
    Inscrit en
    Juillet 2016
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Analyste programmeur
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juillet 2016
    Messages : 32
    Points : 30
    Points
    30
    Par défaut
    Bonjour,
    Merci Pol63 et François DORIN pour vos conseils.

    En mettant quelques compteurs, on s'aperçoit effectivement que les évents RowPostPaint/RowPrePaint/CellPaint ainsi que le paint sont en permanence appelés, à minima 2 fois par clic. Le passage de la souris sur les headers ou certaines cellules suffit à les déclencher (de manière plutôt aléatoire).

    Ce qu'il se passe, les évents RowPostPaint/RowPrePaint/CellPaint sont déclenchés une dizaine de fois dès que l'on sélectionne plusieurs cellules, le DGV_Work.Paint est constant (2fois).

    J'ai positionné mon RectCellSelect() dans l'event DGV_Work.Paint

    je mesure 1 milliseconde pour l’exécution de RectCellSelect().

    J'ai quand même réussi à optimiser le temps d’exécution du code en remplaçant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
        Private Sub DGV_Work_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DGV_Work.CellLeave
            DGV_Work.InvalidateRow()
        End Sub
    par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
        Private Sub DGV_Work_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DGV_Work.CellLeave
            If e.RowIndex - 1 >= 0 Then DGV_Work.InvalidateRow(e.RowIndex - 1)
            DGV_Work.InvalidateRow(e.RowIndex)
            If e.RowIndex + 1 < DGV_Work.RowCount Then DGV_Work.InvalidateRow(e.RowIndex + 1)
        End Sub
    Mais lorsque plusieurs cellules sont sélectionnées, la latence est de retour....

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Analyste programmeur
    Inscrit en
    Juillet 2016
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Analyste programmeur
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juillet 2016
    Messages : 32
    Points : 30
    Points
    30
    Par défaut Solution
    Après des jours de recherche et de test, j'ai complètement changé ma stratégie. Voici une solution qui tient la route, seul bémol une latence (clignotement) sur le défilement du DGV apparaît.
    En quelques mots, la solution consiste à positionner un panel sur le DGV et le déclarer en tant qu'enfant de celui-ci et lui mettre un fond transparent. Pour le reste, je joue sur la couleur de la selectionbackcolor des cellules.
    Le but était de récréer un curseur ressemblant à celui d'Excel 2013. Vous trouverez ci-dessous le code pour la solution, peut être que certain d'entre vous aurons de meilleures idées pour y arriver, si tel est le cas je suis preneur.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
        'Variables partagées pour curseur
        Private Shared G_Cursor_NotContigu, G_Cursor_NTop, G_Cursor_NBottom, G_Cursor_NLeft, G_Cursor_NRight As Boolean 'Représentation des contours
        Private Shared G_Cursor_Ep As Int16 'Epaisseur du curseur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Bonus d'optimisation lors de la suppression de lignes dans le DGV

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
        Private Sub Supp_Line(ByVal sender As Object, ByVal e As DataGridViewRowCancelEventArgs) Handles DGV_Work.UserDeletingRow
            '----------------------------------------------------------------------------------------------------
            'DECLARATIONS VARIABLES
            '----------------------------------------------------------------------------------------------------
     
            '----------------------------------------------------------------------------------------------------
            'EXECUTION
            '----------------------------------------------------------------------------------------------------
            DGV_Work.Visible = False
     
            If DGV_Work.SelectedRows.Count = 1 Then
                DGV_Work.Visible = True
            End If
     
        End Sub

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. PC sur TV : Problème de N&B
    Par tranbert84 dans le forum Périphériques
    Réponses: 4
    Dernier message: 30/10/2005, 15h28
  2. Dessiner un rectangle sur une forme
    Par Neo41 dans le forum MFC
    Réponses: 6
    Dernier message: 13/08/2005, 01h08
  3. Avis sur gros problème BDE en Reseau
    Par tipiweb dans le forum Bases de données
    Réponses: 4
    Dernier message: 29/04/2005, 09h25
  4. [VB.NET] [WinForms] Afficher un rectangle sur une image
    Par Noodles dans le forum Windows Forms
    Réponses: 3
    Dernier message: 20/12/2004, 10h36
  5. Question sur les problèmes d'allocation dynamique
    Par slylafone dans le forum C++
    Réponses: 23
    Dernier message: 25/10/2004, 14h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo