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 :

Probleme pour recuperer des Metafichiers stockes dans un TList


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 Probleme pour recuperer des Metafichiers stockes dans un TList
    Bonjours
    J'ai besoin de stocker des Metafichiers dans un Tlist, ca je pense avoir reussi, mon probleme c'est que je n'arrive pas a recuperer les Metafichiers stockes, 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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include "Printers.hpp"
    #include "Graphics.hpp"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
     TMetafile *pMetafile;
     TList *LPages = new TList();
     for(int I = 0; I <=2; I++)
     {
     pMetafile = new TMetafile;
     pMetafile->Width = Printer()->PageWidth;
     pMetafile->Height = Printer()->PageHeight;
     LPages->Add(pMetafile);
     delete pMetafile;
     }
     }
    //---------------------------------------------------------------------------

  2. #2
    Membre expérimenté
    Avatar de sat83
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2004
    Messages
    1 040
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2004
    Messages : 1 040
    Points : 1 307
    Points
    1 307
    Par défaut
    Il ne faut pas détruire (delete pMetafile) les TMetaFile dans la boucle, sinon ton TList ne contiendra que des pointeurs incorrect (des poiteurs sur des objets qui ne sont plus alloués). Il faut détruire tous les éléments crées au moment de détruite la TList LPages.

    Ensuite, pour récupérer un élément de ton TList il faut faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    TMetafile *mf = (TMetafile *)LPages->Items[i] ;

    A noter : l'utilisation de TObjectList à la place de TList permet de s'affranchir de la libération finale des éléments contenu dans la liste (TObjectList gère lui même ses pointeurs)

  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 sat83
    J'ai reussi a imprimer un Metafile contenu dans un TList, 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
     
     
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include "Printers.hpp"
    //#include "Graphics.hpp"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    TRect R;
    String S;
    int P;
    P = GetDeviceCaps(Printer()->Handle, LOGPIXELSX);
     TMetafile *pMetafile;
     TList *LPages = new TList();
     for(int I = 0; I <=2; I++)
     {
     pMetafile = new TMetafile;
     pMetafile->Width = Printer()->PageWidth;
     pMetafile->Height = Printer()->PageHeight;
     LPages->Add(pMetafile);
     }
     TMetafile *lpMetafile = (TMetafile *)LPages->Items[0];
     TMetafileCanvas *pCanvas = new TMetafileCanvas(lpMetafile, 0);
     // ecriture d'une premiere chaine
     pCanvas->Font->PixelsPerInch = P;
     pCanvas->Font->Size = 18;
     pCanvas->TextOut(10, 10, "Vous etes sur la page 1");
     R.Top = (Printer()->PageHeight) / 2;
     R.left = 0;
     R.Right = Printer()->PageWidth;
     R.Bottom = (Printer()->PageHeight);
     // ecriture d'une seconde chaine
     pCanvas->Font->Size = 22;
     S = "Ce texte est centre sur la page";
     DrawText(pCanvas->Handle, S.c_str(),S.Length(), &R, DT_CENTER && DT_VCENTER && DT_SINGLELINE);
    delete pCanvas;
     
     Printer()->BeginDoc();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[0]);
     Printer()->EndDoc();
     }
    //---------------------------------------------------------------------------
    Il me reste a tester pour imprimer les autre Metafiles

  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 partiellement resolu mes problemes il me reste deux corrections a apporter
    - je n'arrive pas a centrer le texte verticalement avec cette fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    DrawText(pCanvas->Handle, S.c_str(),S.Length(), &R, DT_CENTER || DT_VCENTER || DT_SINGLELINE);
    pour y arriver j'ai du tricher comme ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    R = Rect(0, (Printer()->PageHeight) / 2, Printer()->PageWidth, Printer()->PageHeight);
    Je n'ai pas trouve comment utiliser le canvas de chaque Metafichier stocke dans le TList
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[0], 0);
    TMetafileCanvas *pCanvas1 = new TMetafileCanvas((TMetafile *)LPages->Items[1], 0);
    TMetafileCanvas *pCanvas2 = new TMetafileCanvas((TMetafile *)LPages->Items[2], 0);
    Voici le code complet
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include "Printers.hpp"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    TRect R;
    String S;
    int P;
    P = GetDeviceCaps(Printer()->Handle, LOGPIXELSX);
     TMetafile *pMetafile;
     TMetafileCanvas *pCanvas;
     TList *LPages = new TList();
     for(int I = 0; I <=2; I++)
     {
     pMetafile = new TMetafile;
     pMetafile->Width = Printer()->PageWidth;
     pMetafile->Height = Printer()->PageHeight;
     LPages->Add(pMetafile);
     }
    // if(pMetafile){ delete pMetafile; pMetafile=0; }
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[0], 0);
     // ecriture d'une premiere chaine
     pCanvas->Font->PixelsPerInch = P;
     pCanvas->Font->Size = 18;
     pCanvas->TextOut(10, 10, "Vous etes sur la page 1");
     // ecriture d'une seconde chaine
     pCanvas->Font->Size = 22;
     S = "Ce texte est centre sur la page";
     R = Rect(0, (Printer()->PageHeight) / 2, Printer()->PageWidth, Printer()->PageHeight);
     DrawText(pCanvas->Handle, S.c_str(),S.Length(), &R, DT_CENTER || DT_VCENTER || DT_SINGLELINE);
    delete pCanvas;
     TMetafileCanvas *pCanvas1 = new TMetafileCanvas((TMetafile *)LPages->Items[1], 0);
     InflateRect(&R, -300, -300);
     pCanvas1->Ellipse(R);
     pCanvas1->Pen->Color = clBlue;
     InflateRect(&R, -200, -200);
     pCanvas1->Ellipse(R);
     pCanvas1->Pen->Color = clGreen;
     InflateRect(&R, -200, -200);
     pCanvas1->Ellipse(R);
     pCanvas1->Pen->Color = clRed;
     InflateRect(&R, -200, -200);
     pCanvas1->Ellipse(R);
     InflateRect(&R, -200, -200);
     pCanvas1->Rectangle(R);
     delete pCanvas1;
     HDC I;
     I = GetDC(0); // Obtention d'un handle de device context sur l'écran
     TMetafileCanvas *pCanvas2 = new TMetafileCanvas((TMetafile *)LPages->Items[2], 0);
     StretchBlt(pCanvas2->Handle, 0, 0,
                 Printer()->PageWidth, Printer()->PageHeight,
                 I, 0, 0,
                 Screen->Width, Screen->Height,
                 SRCCOPY);
    ReleaseDC(Handle, I); // ne pas oublier de relâcher le HDC
    PaintBox1->Tag = 0;
     delete pCanvas2;
     Printer()->BeginDoc();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[0]);
     Printer()->NewPage();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[1]);
    // Printer()->NewPage();
    // Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[2]);
     Printer()->EndDoc();
     }
    //---------------------------------------------------------------------------

  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
    Le probleme du Canvas est resolu, il me reste celui du centrage vertical du texte
    Voici le code corrige
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include "Printers.hpp"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    TRect R;
    String S;
    int P;
    P = GetDeviceCaps(Printer()->Handle, LOGPIXELSX);
     TMetafile *pMetafile;
     TMetafileCanvas *pCanvas;
     TList *LPages = new TList();
     for(int I = 0; I <=2; I++)
     {
     pMetafile = new TMetafile;
     pMetafile->Width = Printer()->PageWidth;
     pMetafile->Height = Printer()->PageHeight;
     LPages->Add(pMetafile);
     }
    // if(pMetafile){ delete pMetafile; pMetafile=0; }
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[0], 0);
     // ecriture d'une premiere chaine
     pCanvas->Font->PixelsPerInch = P;
     pCanvas->Font->Size = 18;
     pCanvas->TextOut(10, 10, "Vous etes sur la page 1");
     // ecriture d'une seconde chaine
     pCanvas->Font->Size = 22;
     S = "Ce texte est centre sur la page";
     R = Rect(0, (Printer()->PageHeight) / 2, Printer()->PageWidth, Printer()->PageHeight);
     DrawText(pCanvas->Handle, S.c_str(),S.Length(), &R, DT_CENTER || DT_VCENTER || DT_SINGLELINE);
     delete pCanvas;
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[1], 0);
     InflateRect(&R, -300, -300);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clBlue;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clGreen;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clRed;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     InflateRect(&R, -200, -200);
     pCanvas->Rectangle(R);
     delete pCanvas;
     HDC I;
     I = GetDC(0); // Obtention d'un handle de device context sur l'écran
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[2], 0);
     StretchBlt(pCanvas->Handle, 0, 0,
                 Printer()->PageWidth, Printer()->PageHeight,
                 I, 0, 0,
                 Screen->Width, Screen->Height,
                 SRCCOPY);
     ReleaseDC(Handle, I); // ne pas oublier de relâcher le HDC
     PaintBox1->Tag = 0;
     delete pCanvas;
     Printer()->BeginDoc();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[0]);
     Printer()->NewPage();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[1]);
     Printer()->NewPage();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[2]);
     Printer()->EndDoc();
     }
    //---------------------------------------------------------------------------

  6. #6
    Membre averti

    Profil pro
    Inscrit en
    Juin 2005
    Messages
    351
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juin 2005
    Messages : 351
    Points : 446
    Points
    446
    Par défaut
    Il faut d'abord calculer la place qu'occupe le texte et centrer celui-ci dans le rectangle:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    void DrawText(TCanvas* canvas,
                                 const char* text,
                                 EFlowHorizontalAlign hAlign,
                                 EFlowVerticalAlign vAlign,
                                  TRect& R) {
    const Word Aligns[]={DT_LEFT, DT_CENTER, DT_RIGHT};
    TRect tempR=R;
    Word  flags = DT_EXPANDTABS | DT_WORDBREAK | Aligns[hAlign];
    DrawText(canvas->Handle,text,-1,&tempR,flags | DT_CALCRECT);
    tempR=AdjustTextRect(tempR,R,hAlign,vAlign);
    DrawText(canvas->Handle,text,-1,&tempR,flags);
    }

    avec

    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
     
    //! Horizontal alignement of the content
    typedef enum {
                   fohLeft=0,         //!< Align text on the left
                   fohCenter=1,       //!< Center text
                   fohRight=2        //!< Align text on the right
                 } EHorizontalAlign;
     
    //! Vertical alignement of the content
    typedef enum {
                   fovTop=0,          //!< Align text on the top
                   fovMiddle=1,       //!< Center text vertically
                   fovBottom=2       //!< Align text on the bottom
                 } EVerticalAlign;
     
    //! Adjuste position and the size of the rectangle to align text on "bounds" rect
    /*! \param R      is the size of the text to display
        \param bounds is the limit of the rectangle to align the text
        \param hPos   is the horizontal alignement of the R box in the bounds
        \param vPos   is the verticl alignement of the R box in the bounds
        \return R if the adjusted position of the rectangle where to draw the text
                     to align it as required relatively to the bounds rectangle
     */
    TRect CFlowPainter::AdjustTextRect(TRect R,
                                       const TRect& bounds,
                                       EHorizontalAlign hPos,
                                       EVerticalAlign vPos) const {
     
      int dX=bounds.right  - bounds.left + R.left - R.right;
      int dY=bounds.bottom - bounds.top  + R.top  - R.bottom;
     
      if (dX<0) R.right +=dX;
      if (dY<0) R.bottom+=dY;
      if ((dX<0)||(hPos==fohLeft)) dX=0;
      if ((dY<0)||(vPos==fovTop))  dY=0;
      if (hPos==fohCenter) dX=dX/2;
      if (vPos==fovMiddle) dY=dY/2;
      dX+=bounds.left-R.left;
      dY+=bounds.top -R.top;
      R.left+=dX;   R.right +=dX;
      R.top +=dY;   R.bottom+=dY;
     
      return R;
    }

  7. #7
    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 Patrick Seuret
    J'ai trouve mon erreur, il n'y a rien a calculer, voici les lignes corrigees, j'avais double les " | "
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     R = Rect(0, 0, Printer()->PageWidth, Printer()->PageHeight);
     DrawText(pCanvas->Handle, S.c_str(),S.Length(), &R, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    voici le code complet
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include "Printers.hpp"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    TRect R;
    String S;
    int P;
    P = GetDeviceCaps(Printer()->Handle, LOGPIXELSX);
     TMetafile *pMetafile;
     TMetafileCanvas *pCanvas;
     TList *LPages = new TList();
     for(int I = 0; I <=2; I++)
     {
     pMetafile = new TMetafile;
     pMetafile->Width = Printer()->PageWidth;
     pMetafile->Height = Printer()->PageHeight;
     LPages->Add(pMetafile);
     }
    // if(pMetafile){ delete pMetafile; pMetafile=0; }
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[0], 0);
     // ecriture d'une premiere chaine
     pCanvas->Font->PixelsPerInch = P;
     pCanvas->Font->Size = 18;
     pCanvas->TextOut(10, 10, "Vous etes sur la page 1");
     // ecriture d'une seconde chaine
     pCanvas->Font->Size = 22;
     S = "Ce texte est centre sur la page";
     R = Rect(0, 0, Printer()->PageWidth, Printer()->PageHeight);
     DrawText(pCanvas->Handle, S.c_str(),S.Length(), &R, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
     delete pCanvas;
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[1], 0);
     InflateRect(&R, -300, -300);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clBlue;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clGreen;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clRed;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     InflateRect(&R, -200, -200);
     pCanvas->Rectangle(R);
     delete pCanvas;
     HDC I;
     I = GetDC(0); // Obtention d'un handle de device context sur l'écran
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[2], 0);
     StretchBlt(pCanvas->Handle, 0, 0,
                 Printer()->PageWidth, Printer()->PageHeight,
                 I, 0, 0,
                 Screen->Width, Screen->Height,
                 SRCCOPY);
     ReleaseDC(Handle, I); // ne pas oublier de relâcher le HDC
     PaintBox1->Tag = 0;
     delete pCanvas;
     Printer()->BeginDoc();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[0]);
     Printer()->NewPage();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[1]);
     Printer()->NewPage();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[2]);
     Printer()->EndDoc();
     }
    //---------------------------------------------------------------------------

  8. #8
    Membre averti

    Profil pro
    Inscrit en
    Juin 2005
    Messages
    351
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juin 2005
    Messages : 351
    Points : 446
    Points
    446
    Par défaut
    Salut,

    C'est juste, en fait je me souviens que j'avais dû faire le calcul pour les alignements en bas et que j'avais trouvé ce code en Delphi qui gérait tous les alignements. Merci pour l'info du DT_VCENTER

  9. #9
    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
    Le programme complet avec impression et previsualisation
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include "Printers.hpp"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
     TMetafile *pMetafile;
     TMetafileCanvas *pCanvas;
     TList *LPages = new TList();
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
    TRect R;
    String S;
    int P;
    P = GetDeviceCaps(Printer()->Handle, LOGPIXELSX);
     for(int I = 0; I <=2; I++)
     {
     pMetafile = new TMetafile;
     pMetafile->Width = Printer()->PageWidth;
     pMetafile->Height = Printer()->PageHeight;
     LPages->Add(pMetafile);
     }
    // if(pMetafile){ delete pMetafile; pMetafile=0; }
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[0], 0);
     // ecriture d'une premiere chaine
     pCanvas->Font->PixelsPerInch = P;
     pCanvas->Font->Size = 18;
     pCanvas->TextOut(10, 10, "Vous etes sur la page 1");
     // ecriture d'une seconde chaine
     pCanvas->Font->Size = 22;
     S = "Ce texte est centre sur la page";
     R = Rect(0, 0, Printer()->PageWidth, Printer()->PageHeight);
     DrawText(pCanvas->Handle, S.c_str(),S.Length(), &R, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
     delete pCanvas;
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[1], 0);
     InflateRect(&R, -300, -300);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clBlue;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clGreen;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     pCanvas->Pen->Color = clRed;
     InflateRect(&R, -200, -200);
     pCanvas->Ellipse(R);
     InflateRect(&R, -200, -200);
     pCanvas->Rectangle(R);
     delete pCanvas;
     HDC I;
     I = GetDC(0); // Obtention d'un handle de device context sur l'écran
     pCanvas = new TMetafileCanvas((TMetafile *)LPages->Items[2], 0);
     StretchBlt(pCanvas->Handle, 0, 0,
                 Printer()->PageWidth, Printer()->PageHeight,
                 I, 0, 0,
                 Screen->Width, Screen->Height,
                 SRCCOPY);
     ReleaseDC(Handle, I); // ne pas oublier de relâcher le HDC
     Prv->Tag = 0;
     delete pCanvas;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
     // imprimer
     Printer()->BeginDoc();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[0]);
     Prv->Tag = 0;
     Prv->Refresh();
     Sleep(1000);
     Printer()->NewPage();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[1]);
     Prv->Tag = 1;
     Prv->Refresh();
     Sleep(1000);
     Printer()->NewPage();
     Printer()->Canvas->Draw(0, 0, (TMetafile *)LPages->Items[2]);
     Prv->Tag = 2;
     Prv->Refresh();
     Sleep(1000);
     Printer()->EndDoc();
     }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::PrvPaint(TObject *Sender)
     {
     if(LPages->Count > 0)
     {
     Prv->Canvas->Brush->Color = clWhite;
     pMetafile = (TMetafile *)LPages->Items[Prv->Tag];
     Prv->Canvas->FillRect(Rect(0,0, pMetafile->Width, pMetafile->Height)); //Pour "peindre" le fond en blanc.
     Prv->Canvas->StretchDraw(Rect(0, 0, Prv->Width, Prv->Height), pMetafile);
     }
     }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button2Click(TObject *Sender)
     {
     // avant
     if(Prv->Tag > 0)
     {
     Prv->Tag = Prv->Tag - 1;
     Prv->Refresh();
     }
     else
     {
     Prv->Refresh();
     ShowMessage(" Vos êtes sur la première page visible ");
     }
     }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
     // apres
     if(Prv->Tag < LPages->Count - 1)
     {
     Prv->Tag = Prv->Tag + 1;
     Prv->Refresh();
     }
     else
     {
     Prv->Refresh();
     ShowMessage(" Vos êtes sur la dernière page visible ");
     }
     }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormDestroy(TObject *Sender)
     {
     for(int i = 0; i <= LPages->Count; i++)
     {
     pMetafile = (TMetafile *)LPages->Items[i];
     delete pMetafile;
     LPages->Delete(i);
     }
     LPages->Free();
     }
    //---------------------------------------------------------------------------
    je ne suis pas sur de liberer correctement la memoire avec ce code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    void __fastcall TForm1::FormDestroy(TObject *Sender)
     {
     for(int i = 0; i <= LPages->Count; i++)
     {
     pMetafile = (TMetafile *)LPages->Items[i];
     delete pMetafile;
     LPages->Delete(i);
     }
     LPages->Free();
     }
    je ne sais pas verifier les fuites memoire

  10. #10
    Membre régulier Avatar de jehrikhan
    Inscrit en
    Mars 2008
    Messages
    90
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 90
    Points : 89
    Points
    89
    Par défaut
    Tu as essayé en passant par un logiciel type memProof?
    Ca te trouvera pas forcément toutes les fuites s'il y en a mais il en troue déjà pas mal

  11. #11
    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 !

    TMetaFile sait libérer toute la mémoire qu'il a alloué.
    Voici deux exemples pour purger et libérer une liste :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    while(List->Count != 0)
        {
        delete (TMetafile*)List->First();
        List->Delete(0);
        }
    delete List;
    On peut décliner ce premier exemple avec List->Last mais dans ce cas on supprime le dernier élement de la liste (List->Count-1).

    Ou bien encore :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    for(int j = 0; j < List->Count; j++)
        {
        delete (TMetaFile*)List->Items[j];
        }
    List->Clear();
    delete List;
    A plus !

  12. #12
    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 jehrikhan, je me demande si BCB ne possederait pas un utilitaire type memProof ?

    Merci henderson, si j'ai bien compris mon code liberait les Metafichiers, les Items du Tlist, mais pas le Tlist

  13. #13
    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 !

    Je me base sur ce qui est dit dans l'aide :

    N'appelez pas la méthode Free d'un objet. Utilisez à la place le mot-clé delete qui appelle Free pour détruire un objet.
    A plus !

Discussions similaires

  1. Réponses: 2
    Dernier message: 18/06/2009, 15h26
  2. Réponses: 3
    Dernier message: 14/11/2006, 10h45
  3. Problème awk pour récuperer des champs
    Par ab_2006 dans le forum Shell et commandes GNU
    Réponses: 15
    Dernier message: 21/09/2006, 09h36
  4. Réponses: 1
    Dernier message: 22/05/2006, 14h44
  5. Probleme pour afficher des printf dans le main
    Par Battosaiii dans le forum C
    Réponses: 4
    Dernier message: 13/03/2006, 10h58

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