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

C++Builder Discussion :

Comment mettre une image en fond d'un TStringGrid


Sujet :

C++Builder

  1. #1
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Points : 3 766
    Points
    3 766
    Par défaut Comment mettre une image en fond d'un TStringGrid
    Bonsoir
    Voici comment mettre une image en fond d'un TStringGrid
    le code
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
          int ARow, TRect &Rect, TGridDrawState State)
    {
    int i, j, X, Y;
    TRect R;
    if(State.Contains(gdFixed))
    {
    // Les cellules fixes sont toujours dessinées en gris
    StringGrid1->Canvas->Brush->Color = clBtnFace;
    StringGrid1->Canvas->Brush->Style = bsSolid;
    StringGrid1->Canvas->FillRect(Rect);
    }
    else if(State.Contains(gdSelected))
    {
    // Les cellules sélectionnées sont en bleue
    StringGrid1->Canvas->Brush->Color = clNavy;
    StringGrid1->Canvas->Brush->Style = bsSolid;
    StringGrid1->Canvas->FillRect(Rect);
    }
    else
    {
    // Recherche de la zone image à copier pour tenir compte des décalages
    // de la grille en fonction des barres de défilement.
    X = 0;
    for(i = StringGrid1->FixedCols + 1; i <= ACol; i++) (X++, StringGrid1->ColWidths [i]);
    {
    Y = 0;
    for(i = StringGrid1->FixedRows + 1; i <= ARow; i++) (Y++, StringGrid1->RowHeights[i]);
    {
    R.Left = X;
    R.Right = X + Rect.Right - Rect.Left;
    R.Top = Y;
    R.Bottom = Y + Rect.Bottom - Rect.Top;
    // Dessin d'une partie de l'image
    Image1->Visible = false;
    Image1->Picture->Bitmap->LoadFromFile("C:\\Documents and Settings\\blondelle\\Mes documents\\Mes images\\Massiv10\\Massiv10\\Bitmaps\\Arrow\\arcarrow1.bmp");
    // "C:\\Documents and Settings\\blondelle\\Mes documents\\Mes images\\Massiv10\\Massiv10\\Bitmaps\\Arrow\\arcarrow1.bmp"
    StringGrid1->Canvas->CopyRect(Rect, Image1->Picture->Bitmap->Canvas, R);
    StringGrid1->Canvas->Brush->Style = bsClear;
    }
    }
    }
    // Sélection de la couleur de texte
    if(State.Contains(gdSelected))
    {
    SetTextColor(StringGrid1->Canvas->Handle, clWhite);
    }
    else
    {
    SetTextColor(StringGrid1->Canvas->Handle, clBlack);
    }
    // Dessin du texte en utilisant la fonction API
    DrawText(StringGrid1->Canvas->Handle, (StringGrid1->Cells[ACol][ARow]).c_str(), -1, &Rect, DT_NOPREFIX );
    }
    //---------------------------------------------------------------------------
    le lien Delphi

  2. #2
    Membre chevronné

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    1 390
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 1 390
    Points : 1 777
    Points
    1 777
    Par défaut
    Salut !

    Il y a quelque chose de très sympa à faire, toujours en partant d'une classe dérivée.

    On sait que l'on peut surcharger sa méthode Paint qu'il hérite de TCustomControl et on peut lui définir une DrawCell (la méthode qui normalement gère OnDrawCell).

    Je suis parti d'une image 640 * 480, la trifide .. pour ne pas changer !

    Chez moi, l'effet est garanti, mais à chacun de faire en sorte que ça "jette" encore plus :

    Donc il s'agit toujours de ma jStringGrid et je lui déclare ces deux méthodes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    //Pas public !!! protected ou private !!!
    void __fastcall DrawCell(int ACol, int ARow, const TRect &ARect,
                                  TGridDrawState AState);
    void __fastcall Paint();
    Dont voici le code :

    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
     
    void __fastcall jStringGrid::DrawCell(int ACol, int ARow, const TRect &ARect,
                                                    TGridDrawState AState)
    {
    //Juste un exemple, on donne quand même à l'utilisateur de quoi gérer lui-même !
    if(OnDrawCell == NULL)
        {
        if(AState.Contains(gdFixed))
            {
            Canvas->Brush->Style = bsSolid;
            Canvas->Brush->Color = clSilver;
            Canvas->RoundRect(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom,8,8);
            }
        else
            {
            Canvas->Brush->Style = bsClear;
            Canvas->Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
            }
        }
    else
        {
        //L'image en arrière plan est de facto dessinée par Paint 
        OnDrawCell(this, ACol, ARow, ARect, AState);
        }
    }
     
     
    void __fastcall jStringGrid::Paint()
    {
    TGridDrawState GrState;
    Canvas->Draw(0,0,Form1->Image1->Picture->Bitmap); // << ma trifide... invisible
    Canvas->Pen->Style = psSolid;
    Canvas->Pen->Color = clBlack;
    Canvas->Pen->Mode = pmNot;
    int xs;
    int ys;
    // C'est un essai donc pour les boucles ... ... ... je teste sur 5 * 5
    for(int y = 0; y < RowCount; y++)
        {
        for(int x = 0; x < ColCount; x++)
            {
            //juste pour tester donc à améliorer 
            if((y == 0) || (x == 0))GrState = GrState << gdFixed;
            else GrState = GrState >> gdFixed;
            xs = x * DefaultColWidth; //  << à améliorer
            ys = y * DefaultRowHeight; // << idem
            DrawCell(x,y, Rect(xs,
                               ys,
                               xs + DefaultColWidth,
                               ys + DefaultRowHeight),
                               GrState);
            }
        }
    }
    Soit on en fait un composant soit on la garde comme une classe volante !
    Développé avec BCB3 Pro donc ... à tester chez vous !

    A plus !

  3. #3
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Points : 3 766
    Points
    3 766
    Par défaut
    Merci pour ta reponse henderson
    Je vais essayer de tester tes classes ce weekend, ca me permettra de voir si j'ai bien tout compris

  4. #4
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Points : 3 766
    Points
    3 766
    Par défaut
    J'ai teste le code propose, il fonctionne partiellement, j'ai donc du louper quelque chose, je n'ai pas su mettre la classe en protected ou private, BCB ne reconnais pas mes deux fonctions, et le dessin se place en haut a gauche du StringGrid et FixedRows et FixedColumns prennent une couleur noire
    voici le code
    le .h
    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
     
    //---------------------------------------------------------------------------
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <ExtCtrls.hpp>
    #include <Grids.hpp>
    #include <Graphics.hpp>
    //---------------------------------------------------------------------------
    class jStringGrid : public TStringGrid
    {
    public :
    //Pas public !!! protected ou private !!!
    void __fastcall DrawCell(int ACol, int ARow, const TRect &ARect,
                                  TGridDrawState AState);
    void __fastcall Paint();
    };
    class TForm1 : public TForm
    {
    __published: // IDE-managed Components
            TImage *Image1;
            TStringGrid *StringGrid1;
            TButton *Button1;
            TButton *Button2;
            void __fastcall Button1Click(TObject *Sender);
            void __fastcall Button2Click(TObject *Sender);
    private: // User declarations
    public:  // User declarations
            __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    le .cpp
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall jStringGrid::DrawCell(int ACol, int ARow, const TRect &ARect,
                                                    TGridDrawState AState)
    {
    //Juste un exemple, on donne quand même à l'utilisateur de quoi gérer lui-même !
    if(jStringGrid::OnDrawCell == NULL)
        {
        if(AState.Contains(gdFixed))
            {
            jStringGrid::Canvas->Brush->Style = bsSolid;
            jStringGrid::Canvas->Brush->Color = clSilver;
            jStringGrid::Canvas->RoundRect(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom,8,8);
            }
        else
            {
            jStringGrid::Canvas->Brush->Style = bsClear;
            jStringGrid::Canvas->Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
            }
        }
    else
        {
        //L'image en arrière plan est de facto dessinée par Paint
        jStringGrid::OnDrawCell(this, ACol, ARow, ARect, AState);
        }
    }
    //--------------------------------------------------------------------------- 
    void __fastcall jStringGrid::Paint()
    {
    TGridDrawState GrState;
    jStringGrid::Canvas->Draw(0,0,Form1->Image1->Picture->Bitmap); // << ma trifide... invisible
    jStringGrid::Canvas->Pen->Style = psSolid;
    jStringGrid::Canvas->Pen->Color = clBlack;
    jStringGrid::Canvas->Pen->Mode = pmNot;
    int xs;
    int ys;
    // C'est un essai donc pour les boucles ... ... ... je teste sur 5 * 5
    for(int y = 0; y < RowCount; y++)
        {
        for(int x = 0; x < ColCount; x++)
            {
            //juste pour tester donc à améliorer 
            if((y == 0) || (x == 0))GrState = GrState << gdFixed;
            else GrState = GrState >> gdFixed;
            xs = x * DefaultColWidth; //  << à améliorer
            ys = y * DefaultRowHeight; // << idem
            jStringGrid::DrawCell(x,y, Rect(xs,
                               ys,
                               xs + DefaultColWidth,
                               ys + DefaultRowHeight),
                               GrState);
            }
        }
    }
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    ( (jStringGrid*) StringGrid1 )->jStringGrid::Paint();
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    ( (jStringGrid*) StringGrid1 )->jStringGrid::DrawCell(2, 2, ( (jStringGrid*) StringGrid1 )->CellRect(2,2), TGridDrawState());
    }
    //---------------------------------------------------------------------------

  5. #5
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Points : 3 766
    Points
    3 766
    Par défaut
    Je suis parvenu a tapisser partiellement le fond d'un StringGrid et a avoir un affichage correct du texte que ce soit sur l'image ou la grille, l'idee viend d'un poste sur le Forum le code fonctionnait partiellement
    le code
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
          int ARow, TRect &Rect, TGridDrawState State)
    {
    if (!State.Contains(gdFocused))
    {
    StringGrid1->Canvas->Pen->Color = clWhite;
    StringGrid1->Canvas->FillRect(Rect);
    }
    TCanvas *C = StringGrid1->Canvas;
    AnsiString N = StringGrid1->Cells[ACol][ARow];
    Image1->Visible = false;
    C->CopyRect(Rect, Image1->Picture->Bitmap->Canvas, Rect);
    TColor c = clBlack;
    switch(ACol)
        {
        case 1 : c = clBlue; break;
        case 2 : c = clRed; break;
        case 3 : c = clYellow; break;
        case 5 : c = clWhite; break;
        }
    C->Font->Color = c;
    C->Brush->Style = bsClear;
    DrawText(C->Handle, (StringGrid1->Cells[ACol][ARow]).c_str(), -1, &Rect, DT_CENTER | DT_NOPREFIX | DT_VCENTER | DT_SINGLELINE);
    }
    //---------------------------------------------------------------------------

  6. #6
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Points : 3 766
    Points
    3 766
    Par défaut
    Une autre facon d'inserer une image differente par cellule tire de l'aide en ligne, les images sont dans un TListImage
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
          int ARow, TRect &Rect, TGridDrawState State)
    {
      if (State.Contains(gdFocused))   // (gdFocused))
      {
      StringGrid1->Canvas->Brush->Color = clDkGray;
      StringGrid1->Canvas->FillRect(Rect);
      ImageList1->Draw(StringGrid1->Canvas,Rect.Left,Rect.Top,2);
      }
      long index = ACol * StringGrid1->ColCount + ARow;
      ImageList1->Draw(StringGrid1->Canvas,Rect.Left,Rect.Top,index);
      if (State.Contains(gdFocused))
      {
        StringGrid1->Canvas->DrawFocusRect(Rect);
      }
      index = ARow * StringGrid1->ColCount + ACol;
      ImageList1->Draw(StringGrid1->Canvas,Rect.Left,Rect.Top,index);
      if (State.Contains(gdFixed))   // (gdFocused))
      {
        StringGrid1->Canvas->DrawFocusRect(Rect);
      }
    }
    //---------------------------------------------------------------------------

Discussions similaires

  1. comment mettre une image de fond sur un bouton ?
    Par neufrdb dans le forum AWT/Swing
    Réponses: 1
    Dernier message: 17/04/2011, 13h21
  2. mettre une image en fond d'un TStringGrid
    Par farid0031 dans le forum C++Builder
    Réponses: 2
    Dernier message: 10/06/2009, 22h55
  3. Comment mettre une image en fond de JFrame
    Par marc26 dans le forum Débuter
    Réponses: 3
    Dernier message: 19/01/2004, 17h57

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