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();
}
//--------------------------------------------------------------------------- |