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

Langage Delphi Discussion :

Coordonnées exactes du bord d'une cellule d'un StringGrid


Sujet :

Langage Delphi

  1. #1
    Membre régulier

    Homme Profil pro
    Direction financière et formateur en gestion et contrôle de gestion
    Inscrit en
    Mai 2003
    Messages
    184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Direction financière et formateur en gestion et contrôle de gestion
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2003
    Messages : 184
    Points : 119
    Points
    119
    Par défaut Coordonnées exactes du bord d'une cellule d'un StringGrid
    Bonjour à tous,
    Je viens de m’apercevoir que quand je dessine dans le canvas d'un StringGrid, Rect.Left n'était pas le bord de la cellule mais décalé de quelques pixels.

    Je m'explique : dans l'évènement OnDrawCell d'un StringGrid, j'ai mis le code suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
        StringGrid.canvas.brush.color:=clBlue;
        StringGrid.canvas.FillRect(rect);
        StringGrid.canvas.font.color:=clWhite;
        StringGrid.canvas.font.style:=StringGrid.canvas.font.style+[fsbold];
        // on dessine en cellule 0 l'image n°4 
        if ACol=0 then ImageList1.Draw(StringGrid.Canvas,Rect.Left,Rect.Top,4);
    et je me rends compte que l'image n'est pas aux coordonnées 0,0 (le bord gauche) de la cellule mais qu'elle est décalée de quelques pixels (9 semble t'il) de ce bord et donc Rect.Left n'est pas le bord de la cellule et Rect.Top non plus.

    Comment je peux avoir les coordonnées exactes du bord d'une cellule ?
    Delphi 10.4.1 - Lazarus
    Utilisation de la VCL

  2. #2
    Rédacteur/Modérateur
    Avatar de Andnotor
    Inscrit en
    Septembre 2008
    Messages
    5 671
    Détails du profil
    Informations personnelles :
    Localisation : Autre

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 671
    Points : 13 065
    Points
    13 065
    Par défaut
    Propriété DefaultDrawing sur FALSE.

  3. #3
    Membre régulier

    Homme Profil pro
    Direction financière et formateur en gestion et contrôle de gestion
    Inscrit en
    Mai 2003
    Messages
    184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Direction financière et formateur en gestion et contrôle de gestion
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2003
    Messages : 184
    Points : 119
    Points
    119
    Par défaut
    Je ne connaissais pas cette propriété, mais même à false, le dessin (un simple carré) est toujours décalé
    Delphi 10.4.1 - Lazarus
    Utilisation de la VCL

  4. #4
    Membre confirmé Avatar de blonde
    Femme Profil pro
    Développeur Delphi
    Inscrit en
    Septembre 2003
    Messages
    278
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Delphi

    Informations forums :
    Inscription : Septembre 2003
    Messages : 278
    Points : 477
    Points
    477
    Par défaut
    Effectivement, après un essai chez moi, par défaut le Rect.Left est à 4 sur la première colonne.
    Mais avec la solution de Andnotor, cela fonctionne correctement, le Rect.left est bien à 0.
    Je suis en D10.


    Autrement, si tu es sûr de dessiner sur la première colonne, tu peux te permettre de faire un ImageList1.Draw(StringGrid.Canvas, 0 ,Rect.Top,4);

  5. #5
    Membre régulier

    Homme Profil pro
    Direction financière et formateur en gestion et contrôle de gestion
    Inscrit en
    Mai 2003
    Messages
    184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Direction financière et formateur en gestion et contrôle de gestion
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2003
    Messages : 184
    Points : 119
    Points
    119
    Par défaut
    Incompréhensible ... chez moi, en mettant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ImageList1.Draw(StringGrid.Canvas, 0 ,Rect.Top,4);
    ça me donne ça ...
    et j'ai mis DefaultDrawing sur FALSE dans le OnShow puis dans le OnDrawCells au cas où ... rien, j'ai bougé le CTL3D au cas où ... rien non plus
    Nom : Annotation 2020-02-11 210011.png
Affichages : 569
Taille : 807 octets
    on est loin du 0
    Quelqu'un aurait un truc que je n'ai pas vu ?
    Delphi 10.4.1 - Lazarus
    Utilisation de la VCL

  6. #6
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 021
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 021
    Points : 40 934
    Points
    40 934
    Billets dans le blog
    62
    Par défaut
    Bonjour,

    Même si cela n'a que peu d'importance, un rappel systématique (du moins dans le premier post) de la version de Delphi serait quand même un plus, idem pour ce qui est de la "saveur" VCL ou FMX car si pour la version cela n'a certes que peu d'importance pour ce sujet, je ne proposerai certes pas la même chose pour FMX.

    Cela étant, c'est peut-être l'image le problème ?
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  7. #7
    Membre confirmé Avatar de blonde
    Femme Profil pro
    Développeur Delphi
    Inscrit en
    Septembre 2003
    Messages
    278
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Delphi

    Informations forums :
    Inscription : Septembre 2003
    Messages : 278
    Points : 477
    Points
    477
    Par défaut
    Citation Envoyé par patrice@ Voir le message
    Incompréhensible ... chez moi, en mettant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ImageList1.Draw(StringGrid.Canvas, 0 ,Rect.Top,4);
    ça me donne ça ...
    et j'ai mis DefaultDrawing sur FALSE dans le OnShow puis dans le OnDrawCells au cas où ... rien, j'ai bougé le CTL3D au cas où ... rien non plus
    Nom : Annotation 2020-02-11 210011.png
Affichages : 569
Taille : 807 octets
    on est loin du 0
    Quelqu'un aurait un truc que je n'ai pas vu ?
    Ah mince, je n'avais pas vu que tu avais répondu. Effectivement, c'est peu compréhensible.
    Tu as moyen de reproduire dans un mini-exemple que je puisse exécuter sur ma machine histoire de voir si j'ai le même comportement.
    Tu es en VCL ? Et quelle version de Delphi ?

  8. #8
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Bonjour,
    j'ai fait le test avec D7 et effectivement, avec ce code l'image est à 4 pixels du haut et à 4 pixels du bord droit (avec DefautDrawing à True et aussi à False)

    J'avais déjà eu ce phénomène pour customiser une grille et j'avais décalé l'affichage d'autant !

    Je ne sais pas comment l'éviter. Je vais tester en VCL avec D10.3

    A+
    Charly

  9. #9
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Même chose avec D10.3, mais le décalage est de 5 pixels avec defaultDrawing à False et le décalage horizontal passe à 9 pixels avec DefaultDrawing à True .



    A+
    Charly

  10. #10
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 021
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 021
    Points : 40 934
    Points
    40 934
    Billets dans le blog
    62
    Par défaut
    Bonjour,

    je ne comprends pas ! Version 10.3.3 entreprise
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    procedure TForm97.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
    if (ACol=1) AND (ARow>0) then
     begin
        StringGrid1.canvas.brush.color:=clBlue;
        StringGrid1.canvas.FillRect(rect);
        ImageList1.Draw(StringGrid1.Canvas, Rect.Left ,Rect.Top,1);
        ImageList1.Draw(StringGrid1.Canvas, Rect.Left+24 ,Rect.Top,0);
     end;
    j'obtiens bien
    Nom : Capture.PNG
Affichages : 356
Taille : 3,5 Ko
    oui, il y a bien un décalage de la deuxième image mais c'est dû au pourtour transparent de celle-ci
    L'image rouge, pleine, n'a aucun décalage
    Toutefois, notez que mon imagelist est de taille 24x24
    j'aurais dû faire l'inverse, le fond en rouge et l'image 1 en bleu histoire de
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  11. #11
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Voici mon projet de test fait en D10.3.3 CE:

    ProjectGrid.zip

    Par contre je dessine en colonne 0 comme demandé

    A+
    Charly

  12. #12
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Voici le résultat dans mon projet avec ton code dans OnDrawCell :

    Nom : Image2.jpg
Affichages : 345
Taille : 18,5 Ko

  13. #13
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 021
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 021
    Points : 40 934
    Points
    40 934
    Billets dans le blog
    62
    Par défaut
    Bonsoir,

    Programme qui après quelques modifications
    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
     
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if (ACol=1) AND (Arow>0) then
       begin
        StringGrid1.canvas.brush.color:=clWhite;
        StringGrid1.canvas.FillRect(rect);
        // on dessine en cellule 0 l'image n°1
        ImageList1.Draw(StringGrid1.Canvas,Rect.Left,Rect.Top,0);
       end
       else begin
        StringGrid1.canvas.brush.color:=clWindow;
        StringGrid1.canvas.font.color:=clWindowText;
        StringGrid1.Canvas.TextRect(Rect,Rect.Left+2,Rect.Top+4,StringGrid1.Cells[Acol,ARow]);
       end;
    end;
    et Imagelist Height=24, width=24, ce qui permet de voir si l'image est bien aux bords
    Nom : Capture.PNG
Affichages : 349
Taille : 6,3 Ko
    pour moi le premier coupable est donc ImageList, le second (pour le décalage) DefaultDrawing qui doit être à false
    ce qui oblige le TextRect
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  14. #14
    Membre éprouvé
    Avatar de Cirec
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    467
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 467
    Points : 1 072
    Points
    1 072
    Par défaut
    Bonsoir,

    même constat que SergioMaster, après quelques modifications
    le tout s'affiche comme prévu.
    Le problème décrit par Charly910 vient en effet des images contenues dans l'ImageList
    elles sont de taille inférieure par rapport au Width/Heigh de l'ImageList

    pour mieux voir tout ça tu peux modifier le bkColor de l'ImageList en clRed

    pour les 4 pixels en mode DefaultDrawing c'est dans le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    procedure TStringGrid.DrawCell(ACol, ARow: Longint; ARect: TRect;
      AState: TGridDrawState);
     ...
    begin
      if DefaultDrawing then
      begin
        if StyleServices.Enabled then
        begin
          ARect.Left := ARect.Left + 4;   // <------------------ ICI --------------
     ...
    mais on peut en tenir compte comme ceci (par Ex.)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if StringGrid1.DefaultDrawing then
        Dec(Rect.Left, 4);                    // <------------------ ICI --------------
      StringGrid1.canvas.brush.color:=clBlue;
      StringGrid1.canvas.FillRect(rect);
      StringGrid1.canvas.font.color:=clWhite;
      StringGrid1.canvas.font.style:=StringGrid1.canvas.font.style+[fsbold];
        // on dessine en cellule 0 l'image n°1
        {if ACol=0 then} ImageList1.Draw(StringGrid1.Canvas,Rect.Left,Rect.Top,0);
      Inc(Rect.Left, 12);
      DrawText(StringGrid1.canvas.Handle, PChar(StringGrid1.Cells[aCol, aRow]), -1, Rect, DT_SINGLELINE or DT_VCENTER or DT_CENTER);
    end;
    en résumé impossible de recréer le "problème" décrit par patrice@ !

    ps: Testé D7 & 10.3
    Cordialement,

    @+

  15. #15
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 339
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 339
    Points : 3 107
    Points
    3 107
    Par défaut
    Bonjour,

    Ok pour le DefaultDrawing, que j'avais constaté, mais pour la taille de l'image dans le TImageList cela ressemble à un bug ? quelle est la taille mini de l'image d'un TImageList ?

    Sinon, on peut aussi calculer la position de l'image en fonction de la largeur des colonnes.

    je vais augmenter la taille des mes images pour voir.

    A+
    Charly

  16. #16
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 429
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 429
    Points : 24 794
    Points
    24 794
    Par défaut
    Bonjour, je me suis une unité pour tout les dessins de base
    J'utilise StringGrid.CellRect à la place du Rect fourni par OnDrawCell

    Tout a déjà été dit dans un sujet équivalent Texte dédoublé dans Stringgrid montrant parfaitement le décalage

    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
    //------------------------------------------------------------------------------
    (*                SoLuTions is an Versatile Library for Delphi                 -
     *                                                                             -
     *  Copyright "SLT Solutions", (©2006)                                         -
     *  contributeur : ShaiLeTroll (2012) - Gestion des Styles sous C++BuilderXE2  -
     *  contributeur : ShaiLeTroll (2012) - Documentation Insight                  -
     *  contributeur : ShaiLeTroll (2014) - Traduction du code C++Builder vers DelphiXE2
     *                                                                             -
     * ShaiLeTroll                                                                 -
     *                                                                             -
     * Ce logiciel est un programme informatique servant à aider les développeurs  -
     * Delphi avec une bibliothèque polyvalente, adaptable et fragmentable.        -
     *                                                                             -
     * Ce logiciel est régi par la licence CeCILL-C soumise au droit français et   -
     * respectant les principes de diffusion des logiciels libres. Vous pouvez     -
     * utiliser, modifier et/ou redistribuer ce programme sous les conditions      -
     * de la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA    -
     * sur le site "http://www.cecill.info".                                       -
     *                                                                             -
     * En contrepartie de l'accessibilité au code source et des droits de copie,   -
     * de modification et de redistribution accordés par cette licence, il n'est   -
     * offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons,   -
     * seule une responsabilité restreinte pèse sur l'auteur du programme,  le     -
     * titulaire des droits patrimoniaux et les concédants successifs.             -
     *                                                                             -
     * A cet égard  l'attention de l'utilisateur est attirée sur les risques       -
     * associés au chargement,  à l'utilisation,  à la modification et/ou au       -
     * développement et à la reproduction du logiciel par l'utilisateur étant      -
     * donné sa spécificité de logiciel libre, qui peut le rendre complexe à       -
     * manipuler et qui le réserve donc à des développeurs et des professionnels   -
     * avertis possédant  des  connaissances  informatiques approfondies.  Les     -
     * utilisateurs sont donc invités à charger  et  tester  l'adéquation  du      -
     * logiciel à leurs besoins dans des conditions permettant d'assurer la        -
     * sécurité de leurs systèmes et ou de leurs données et, plus généralement,    -
     * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.          -
     *                                                                             -
     * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez      -
     * pris connaissance de la licence CeCILL-C, et que vous en avez accepté les   -
     * termes.                                                                     -
     *                                                                             -
     *----------------------------------------------------------------------------*)
    unit SLT.Controls.VCL.GridsEx;
     
    interface
     
    uses System.SysUtils, System.Types,
      Vcl.Grids, Vcl.Graphics, Vcl.Themes;
     
    type
      /// <summary>Erreur liée à l'assistant TStringGridSLTAssistant de la classe TStringGrid</summary>
      EStringGridSLTAssistantError = class(Exception);
     
      /// <summary>Assistance de la classe TStringGrid</summary>
      /// <remarks>Le TStringGridSLTAssistant n'est pas un class helper car lors de sa création la version utilisée était C++Builder XE2,
      /// Traduction C++Builder vers Delphi en concervant ce concept des Assistances de classes (Class Helper) du Delphi.NET</remarks>
      TStringGridSLTAssistant = class(TObject)
      private
        // Membres privés
        FStringGrid: TStringGrid;
      public
        // Constructeurs
        constructor Create(AStringGrid: TStringGrid); overload;
        constructor Create(Sender: TObject); overload;
     
        /// <summary>DrawCellOfEmptyGrid Fournit une assistance au TStringGrid.OnDrawCell pour fournir le rendu d'une cellule vide</summary>
        /// <param name="ACol">indique l'indice de colonne de la cellule à redessiner</param>
        /// <param name="ARow">indique l'indice de ligne de la cellule à redessiner</param>
        /// <param name="ABackgroundColor">Couleur de fond, clNone pour utiliser la couleur du thème</param>
        /// <param name="AState">Le paramètre State indique si la cellule détient la focalisation, si elle est sélectionnée ou si la cellule est une cellule fixe (qui ne défile pas).</param>
        procedure DrawCellEmpty(ACol, ARow: Integer; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
     
        /// <summary>DrawCellTitleCenter Fournit une assistance au TStringGrid.OnDrawCell pour fournir le rendu d'une cellule de titre avec son texte centré horizontalement et verticalement</summary>
        /// <param name="ACol">indique l'indice de colonne de la cellule à redessiner</param>
        /// <param name="ARow">indique l'indice de ligne de la cellule à redessiner</param>
        /// <param name="AText">indique le texte de cellule à redessiner</param>
        /// <param name="ABackgroundColor">Couleur de fond, clNone pour utiliser la couleur du thème</param>
        /// <param name="AState">Le paramètre State indique si la cellule détient la focalisation, si elle est sélectionnée ou si la cellule est une cellule fixe (qui ne défile pas).</param>
        procedure DrawCellTitleCenter(ACol, ARow: Integer; const AText: string; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
     
        /// <summary>DrawCheckBox fournit un assistant d'implémentation d'un TStringGrid.OnDrawColumnCell qui dessine une case à cocher centrée dans la zone définie par ARect</summary>
        /// <param name="ACol">indique l'indice de colonne de la cellule à redessiner</param>
        /// <param name="ARow">indique l'indice de ligne de la cellule à redessiner</param>
        /// <param name="AChecked">Etat de la case à cocher</param>
        /// <param name="AEnabled">la case à cocher est-elle active</param>
        /// <param name="ABackgroundColor">Couleur de fond, clNone pour utiliser la couleur du thème</param>
        /// <param name="AState">Etat de la cellule d'une grille qui est en cours de restitution</param>
        procedure DrawCellCheckBox(ACol, ARow: Integer; AChecked: Boolean; AEnabled: Boolean = True; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
     
        // Méthodes d'ergonomie
        procedure ZoomColumnWidth(AZoomPourcent: Integer);
     
        // Propriétés
        property StringGrid: TStringGrid read FStringGrid;
      end;
     
    implementation
     
    uses Winapi.Windows,
      SLT.Controls.VCL.GraphicsEx;
     
    const
      ERR_UNASSISTED_CLASS = 'La Classe d''Assistance %s ne prend pas en charge la classe %s mais la classe %s';
     
    { TStringGridSLTAssistant }
     
    //------------------------------------------------------------------------------
    constructor TStringGridSLTAssistant.Create(AStringGrid: TStringGrid);
    begin
      inherited Create();
     
      FStringGrid := AStringGrid;
    end;
     
    //------------------------------------------------------------------------------
    constructor TStringGridSLTAssistant.Create(Sender: TObject);
    begin
      inherited Create();
     
      if Assigned(Sender) then
      begin
        if Sender is TStringGrid then
          FStringGrid := TStringGrid(Sender)
        else
          raise EStringGridSLTAssistantError.CreateFmt(ERR_UNASSISTED_CLASS, [ClassName(), Sender.ClassName(), TStringGrid.ClassName()]);
      end
      else
        raise EStringGridSLTAssistantError.CreateFmt(ERR_UNASSISTED_CLASS, [ClassName(), '[nil]', TStringGrid.ClassName()])
    end;
     
    //------------------------------------------------------------------------------
    procedure TStringGridSLTAssistant.DrawCellCheckBox(ACol, ARow: Integer; AChecked: Boolean; AEnabled: Boolean = True; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
    const
      CSelected: array[TGridDrawingStyle] of TThemedGrid = (
        tgClassicCellSelected, tgCellSelected, tgGradientCellSelected);
    var
      uState: UINT;
      tbState: TThemedButton;
      Details: TThemedElementDetails;
      StyleColor: TColor;
      Buffer: Vcl.Graphics.TBitmap;
      vRect, BufferRect: TRect;
    begin
      if StyleServices.Enabled then
      begin
        vRect := StringGrid.CellRect(ACol, ARow); // Rect Plus fiable que celui fourni par OnDrawCell !
     
        // J'utilise un Buffer temporaire pour dessiner le ThemedButton CheckBox dans le TStringGrid
        Buffer := Vcl.Graphics.TBitmap.Create();
        try
          BufferRect := Rect(0, 0, vRect.Width, vRect.Height);
          Buffer.SetSize(BufferRect.Width, BufferRect.Height);
     
          if ABackgroundColor = clNone then
          begin
            // On dessine un fond normal assurant normalement un bon constrate avec la CheckBox
            Details := StyleServices.GetElementDetails(tgCellNormal);
            StyleServices.GetElementColor(Details, ecFillColor, StyleColor);
          end
          else
            StyleColor := ABackgroundColor;
     
          // Même si la cellule est sélectionné, on laisse un fond neutre pour mieux voir la CheckBox
          // Force un fond opaque pour cacher le texte !
          Buffer.Canvas.Brush.Color := StyleColor;
          Buffer.Canvas.Brush.Style := bsSolid;
          Buffer.Canvas.FillRect(BufferRect);
     
          tbState := tbCheckBoxUncheckedNormal;
          if AChecked then
            tbState := tbCheckBoxCheckedNormal;
     
          if not AEnabled then
          begin
            if AChecked then
              tbState := tbCheckBoxCheckedDisabled
            else
              tbState := tbCheckBoxUncheckedDisabled;
          end;
     
          Details := StyleServices.GetElementDetails(tbState);
          StyleServices.DrawElement(Buffer.Canvas.Handle, Details, BufferRect, BufferRect);
          // Dessin final
          StringGrid.Canvas.Draw(vRect.Left, vRect.Top, Buffer);
        finally
          Buffer.Free();
        end;
      end
      else
      begin
        uState := DFCS_BUTTONCHECK;
        if AChecked then
          uState := uState or DFCS_CHECKED;
     
        if not AEnabled then
          uState := uState or DFCS_INACTIVE;
     
        StringGrid.Canvas.FillRect(vRect);
        DrawFrameControl(StringGrid.Canvas.Handle, vRect, DFC_BUTTON, uState);
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TStringGridSLTAssistant.DrawCellEmpty(ACol, ARow: Integer; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
    var
      Details: TThemedElementDetails;
      DetailColor: TColor;
      vRect: TRect;
    begin
      with StringGrid.Canvas do
      begin
        if StyleServices.Enabled then
        begin
          Details := StyleServices.GetElementDetails(tgCellNormal);
          StyleServices.GetElementColor(Details, ecFillColor, DetailColor);
          if ABackgroundColor = clNone then
          begin
            Pen.Color := DetailColor;
            Brush.Color := DetailColor;
          end
          else
          begin
            Pen.Color := ABackgroundColor;
            Brush.Color := ABackgroundColor;      
          end;
        end
        else
        begin
          Pen.Color := StringGrid.Color;
          Brush.Color := StringGrid.Color;
        end;  
     
        vRect := StringGrid.CellRect(ACol, ARow); // Rect Plus fiable que celui fourni par OnDrawCell !
        Rectangle(vRect);
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TStringGridSLTAssistant.DrawCellTitleCenter(ACol, ARow: Integer; const AText: string; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
    var
      vText: string;
      vRect: TRect;
    begin
      if ABackgroundColor <> clNone then
      begin
        StringGrid.Canvas.Pen.Color := ABackgroundColor;
        StringGrid.Canvas.Brush.Color := ABackgroundColor;
        DrawCellEmpty(ACol, ARow, ABackgroundColor, AState);
     
        StringGrid.Canvas.Font.Color := TCanvasSLTAssistant.GetConstratedColor(ABackgroundColor); // Couleur de luminosité inverse : Contraste garanti pour les couleurs claires ou foncées
      end;
     
      // TextRect encapsule DraxTextEx et est aussi pénible avec ses paramètres in-out !
      vText := AText;
      vRect := StringGrid.CellRect(ACol, ARow);
      vRect.Left := vRect.Left + 3; // Pour ne pas superposer le texte et le rectangle de sélection
      // DT_CENTER Centers text horizontally in the rectangle.
      // DT_VCENTER Centers text vertically. This value is used only with the DT_SINGLELINE value.
      StringGrid.Canvas.TextRect(vRect, vText, [tfCenter, tfSingleLine, tfVerticalCenter]);
    end;
     
    //------------------------------------------------------------------------------
    procedure TStringGridSLTAssistant.ZoomColumnWidth(AZoomPourcent: Integer);
    var
      I: Integer;
    begin
      for I := 0 to StringGrid.ColCount - 1 do
        StringGrid.ColWidths[I] := Round(StringGrid.ColWidths[I] * AZoomPourcent / 100);
    end;
     
    end.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
      TStringGridSliteHelper = class helper for TStringGrid
      public
        procedure DrawCellEmpty(ACol, ARow: Integer; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
        procedure DrawCellTitleCenter(ACol, ARow: Integer; const AText: string; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
        procedure DrawCellCheckBox(ACol, ARow: Integer; AChecked: Boolean; AEnabled: Boolean = True; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
        procedure ZoomColumnWidth(AZoomPourcent: Integer);
      end;

    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
    { TStringGridSliteHelper }
     
    //------------------------------------------------------------------------------
    procedure TStringGridSliteHelper.DrawCellCheckBox(ACol, ARow: Integer; AChecked: Boolean; AEnabled: Boolean = True; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
    begin
      with TStringGridSLTAssistant.Create(Self) do
      try
        DrawCellCheckBox(ACol, ARow, AChecked, AEnabled, ABackgroundColor, AState);
      finally
        Free();
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TStringGridSliteHelper.DrawCellEmpty(ACol, ARow: Integer; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
    begin
      with TStringGridSLTAssistant.Create(Self) do
      try
        DrawCellEmpty(ACol, ARow, ABackgroundColor, AState);
      finally
        Free();
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TStringGridSliteHelper.DrawCellTitleCenter(ACol, ARow: Integer; const AText: string; ABackgroundColor: TColor = clNone; AState: TGridDrawState = []);
    begin
      with TStringGridSLTAssistant.Create(Self) do
      try
        DrawCellTitleCenter(ACol, ARow, AText, ABackgroundColor, AState);
      finally
        Free();
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TStringGridSliteHelper.ZoomColumnWidth(AZoomPourcent: Integer);
    begin
      with TStringGridSLTAssistant.Create(Self) do
      try
        ZoomColumnWidth(AZoomPourcent);
      finally
        Free();
      end;
    end;
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  17. #17
    Membre régulier

    Homme Profil pro
    Direction financière et formateur en gestion et contrôle de gestion
    Inscrit en
    Mai 2003
    Messages
    184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Direction financière et formateur en gestion et contrôle de gestion
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2003
    Messages : 184
    Points : 119
    Points
    119
    Par défaut
    Je suis sous Delphi 10.2 et j'utilise la VCL.
    Bon j'ai refait avec un carré de 24x24 et ... c'est bon du coup!
    Le soucis venait donc du carré qui était en 16x16 et auquel je n'ai pas prêté attention!

    Mais la discussion était super intéressante ...
    Merci à tous
    Delphi 10.4.1 - Lazarus
    Utilisation de la VCL

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

Discussions similaires

  1. Réponses: 7
    Dernier message: 21/04/2013, 03h57
  2. Coordonnées écran en pixels d'une cellule
    Par issoram dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 29/06/2012, 14h31
  3. Ecrire au bord d'une cellule
    Par Invité dans le forum Balisage (X)HTML et validation W3C
    Réponses: 6
    Dernier message: 18/06/2012, 19h44
  4. Focused une cellule d'un stringGrid
    Par BuzzLeclaire dans le forum Composants VCL
    Réponses: 1
    Dernier message: 30/08/2008, 18h45
  5. les bords d'une cellule
    Par nawax88 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 23/04/2007, 17h31

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