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 :

Conserver couleur cellule dans grille


Sujet :

Langage Delphi

  1. #1
    Membre habitué
    Homme Profil pro
    Dev informatique retraité
    Inscrit en
    Août 2005
    Messages
    221
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev informatique retraité
    Secteur : Santé

    Informations forums :
    Inscription : Août 2005
    Messages : 221
    Points : 171
    Points
    171
    Par défaut Conserver couleur cellule dans grille
    Je veux colorier les cellules que j'ai sélectionné dans un StringGrid.
    Pour cela à partir de la procedure StringGridDrawCell je fais ce qui suit

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     If gdSelected In State Then
          Begin
            { Les cellules sélectionnées sont en Jaune }
            StringGrid.canvas.Brush.Color:=clYellow;
            StringGrid.canvas.Rectangl(Rect.Left,Rect.Top,Rect.Right,Rect.bottom);
          End;
    Cela marche quand je clique mais la cellule sélectionnée ne conserve pas la couleur (jaune) dans mon exemple

  2. #2
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    Salut,

    Une piste.

    1) Tu créer un tableau représentant l'ensemble de ton grid avec tout à False
    2) Dans l'évènement onclick de ton Grid tu alimente un tableau avec comme indice la colonne et la ligne et tu assigne True
    3) dans l'evenement Draw tu parcours le tableau si true tu met jaune sinon tu fais rien.

    A toi de jouer.

    PS : j'espère que ton Grid n'as pas 100 colonnes et 2000 lignes !!

  3. #3
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

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

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut
    Salut,

    Voici en gros ce que peut donner la solution proposé par BuzzLeclaire

    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
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids, Contnrs;
     
    type
      TForm1 = class(TForm)
        StringGrid: TStringGrid;
        AddCell: TButton;
        procedure StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure AddCellClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Déclarations privées }
        // Tableau de coordonnées (X/Y) des cellules à mettre en jaune
        FArrayYellowCell : array of TPoint;
      public
        { Déclarations publiques }
        // Méthodes d'ajout de cellule
        procedure addNewCell(X,Y : integer);
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.addNewCell(X, Y: integer);
    begin
      //Ajout d'une nouvelle cellule
      SetLength(FArrayYellowCell,Length(FArrayYellowCell)+1);
      FArrayYellowCell[High(FArrayYellowCell)] := Point(X,Y);
    end;
     
    procedure TForm1.AddCellClick(Sender: TObject);
    begin
      addNewCell(1,1);
      addNewCell(2,1);
      addNewCell(3,1);
      addNewCell(2,2);
      addNewCell(2,4);
      // On force le rafraichissement des cellules visibles
      StringGrid.Invalidate;
    end;
     
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      SetLength(FArrayYellowCell,0);
    end;
     
    procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      isYellow : boolean;
      i : integer;
    begin
      isYellow := false;
      for i := 0 to High(FArrayYellowCell) do
      begin
        if ((FArrayYellowCell[i].X = ACol) and (FArrayYellowCell[i].Y = ARow))  then
        begin
          isYellow := true;
          break;
        end;
      end;
     
      if isYellow then
      begin
        { Les cellules sélectionnées sont en Jaune }
        with StringGrid.canvas do
        begin
          Brush.Color:=clYellow;
          Rectangle(Rect.Left,Rect.Top,Rect.Right,Rect.bottom);
        end;
      end;
    end;
     
    end.
    Ne pas oublier de mettre dans les uses l'unité Contnrs.

    Bon, c'est surement pas optimal, mais bon, c'est une des solutions possible

    A+

  4. #4
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    Re,

    Voilà exactement ce que j'aurais fais :

    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
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids;
     
    type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure FormCreate(Sender: TObject);
        procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
        AColorier : Array of Array of Boolean; // Tableau dynamique
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm1.FormCreate(Sender: TObject);
    Var
      x,y : Integer;
    begin
    // On cincidère que le stringgrid est prédéterminé sur le form
    // Donc on connais les valeurs max col et row
      SetLength(AColorier,StringGrid1.ColCount,StringGrid1.RowCount);
     
    // On initialise le tableau
      for x := 0 to StringGrid1.ColCount do
        for y := 0 to StringGrid1.RowCount do
          AColorier[x,y] := false;
    end;
     
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if AColorier[ACol,ARow] then // si c'est cliqué alors on mets jaune
      begin
        { Les cellules sélectionnées sont en Jaune }
        with StringGrid1.canvas do
        begin
          Brush.Color:=clYellow;
          Rectangle(Rect.Left,Rect.Top,Rect.Right,Rect.bottom);
        end;
      end;
     
    end;
     
    procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      GridCoord: TGridCoord; // recevra les coordonnées des cellules
    begin
      GridCoord    := TStringGrid(Sender).MouseCoord(X,Y); // on récupère le numéro de colonne et ligne ;)
      if (GridCoord.X > 0) And (GridCoord.Y > 0) then
      Begin
    // Cela fonctionnera de la sorte
    // Je clique une fois dans la cellule c'est jaune True
    // Je clique une deuxième fois c'est normal False
       AColorier[GridCoord.X,GridCoord.Y] := Not AColorier[GridCoord.X,GridCoord.Y]; // Mettre AColorier[GridCoord.X,GridCoord.Y] := True; Si tu souhaite conservé meme apres un second clique la couleur
        TStringGrid(Sender).Invalidate;
      End;
    end;
    On pourrais ensuite imaginer de remettre à false tous le tableau sur clique boutton.



    Bye

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

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 695
    Points : 13 131
    Points
    13 131
    Par défaut
    Une autre variante en gérant directement l'Object de chaque cellule comme un booléen:

    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
    procedure TForm6.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    begin
      //Inversion
      with TStringGrid(Sender) do
        Objects[aCol, aRow] := TObject(not boolean(Objects[aCol, aRow]));
    end;
     
    procedure TForm6.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    begin
      with TStringGrid(Sender), Canvas do
        if boolean(Objects[aCol, aRow]) then
        begin
          Brush.Color := clYellow;
          FillRect(Rect);
        end;
    end;

  6. #6
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

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

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Points : 4 935
    Points
    4 935
    Par défaut
    Très intéressant ta solution Andnotor!
    C'est simple et clair.

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

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 695
    Points : 13 131
    Points
    13 131
    Par défaut

  8. #8
    Membre éprouvé Avatar de BuzzLeclaire
    Homme Profil pro
    Dev/For/Vte/Ass
    Inscrit en
    Août 2008
    Messages
    1 606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dev/For/Vte/Ass

    Informations forums :
    Inscription : Août 2008
    Messages : 1 606
    Points : 1 113
    Points
    1 113
    Par défaut
    C'est carrément plus net, AndNotOr

  9. #9
    Membre habitué
    Homme Profil pro
    Dev informatique retraité
    Inscrit en
    Août 2005
    Messages
    221
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev informatique retraité
    Secteur : Santé

    Informations forums :
    Inscription : Août 2005
    Messages : 221
    Points : 171
    Points
    171
    Par défaut
    Merci de votre aide précieuse.
    La solution de Andnotor marche Bien.

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

Discussions similaires

  1. couleur de police d'une cellule dans une collection pour combobox
    Par fabrice44 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 16/05/2008, 15h08
  2. Changer la couleur d'une cellule dans une table
    Par gbrion dans le forum WinDev
    Réponses: 4
    Dernier message: 26/04/2007, 21h55
  3. problème de couleur de cellule dans une JTable
    Par rprom1 dans le forum Composants
    Réponses: 3
    Dernier message: 04/04/2007, 00h01
  4. Problème : couleur de cellule dans un datagrid
    Par vilcoy dans le forum VB.NET
    Réponses: 9
    Dernier message: 15/03/2007, 17h27
  5. Fuison de cellule dans une grille
    Par AlexB59 dans le forum Composants VCL
    Réponses: 1
    Dernier message: 21/11/2005, 16h25

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