Memotext formatiert auf einem Drucker ausgeben

# include "printers.h"

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TPrinter Prntr = Printer();
    TRect r = Rect(200,200,Prntr->PageWidth – 200,Prntr->PageHeight – 200);
    Prntr->BeginDoc();
    Prntr->Canvas->TextRect(r, 200, 200, Memo1->Lines->Text);
    Prntr->EndDoc();
}

Screenshot einer Form auf dem Drucker ausgeben

  • Im Beispiel wird das aktuelle Formular zusammen mit einer Überschrift auf dem Drucker um 90° gedreht ausgegeben (Querformat)
void __fastcall From1::PrintDiagram()
{
    // Printdialog initialisieren
    TPrintDialog* pPrintDialog = new TPrintDialog(this);
    pPrintDialog->FromPage = 1; // Parameter setzen (hier soll nur eine Seite gedruckt werden)
    pPrintDialog->MinPage = 1;
    pPrintDialog->ToPage = 1;
    pPrintDialog->MaxPage = 1;

    // Druckdialog öffnen
    if (pPrintDialog->Execute())
    {
        // Druckerobjekt erzeugen
        TPrinter* pPrinter = Printer();

        // Seitengröße Rect(RandL,RandO,RandR,RandU);
        int RandL=350;
        int RandO=350;
        int RandR = pPrinter->PageWidth-200;
        int RandU = pPrinter->PageHeight-200;

        // für die Zeitangabe auf der Seite
        TDateTime dtNow = Now();

        // Bestimmte Anzahl von Kopien drucken
        int iCopies;
        if (pPrintDialog->Copies == 0) pPrintDialog->Copies = 1;
        iCopies = pPrintDialog->Copies;

        while (iCopies)
        {
            iCopies--;

            pPrinter->BeginDoc();

            // Bild für die Kopfzeile erzeugen
            Graphics::TBitmap* bmpHeadLine = new Graphics::TBitmap();
            bmpHeadLine->Height = 20;
            bmpHeadLine->Width = 1024;

            // Kopfzeilentext zusammenbauen
            AnsiString strHeadline = "Ausdruck vom " + FormatDateTime("dd'.'mm'.'yyyy hh':'nn':'ss", dtNow);

            // Schriftart setzen und Schreiben
            bmpHeadLine->Canvas->Font->Assign(Font);
            bmpHeadLine->Canvas->Font->Style = TFontStyles() << fsBold;
            bmpHeadLine->Canvas->Font->Size = 10;
            bmpHeadLine->Canvas->TextOut(0, 0, strHeadline); // Überschrift Schreiben

            // Text drehen um 90° (für Querformat)
            Graphics::TBitmap* bmpHeadLineRotate = new Graphics::TBitmap();
            bmpHeadLineRotate->Height= bmpHeadLine->Width;
            bmpHeadLineRotate->Width= bmpHeadLine->Height;

            for (int x=0; x<bmpHeadLine->Width; x++)
            {
                int iHw = bmpHeadLine->Width-1-x;

                for (int y=0; y<bmpHeadLine->Height; y++) bmpHeadLineRotate->Canvas->Pixels[y][iHw]= bmpHeadLine->Canvas->Pixels[x][y];
            }

            // Text stretchen
            int iRx = (RandR-RandL) * 0.03; // legt fest, wie groß die Schrift auf dem Blatt gestretcht wird
            TRect rectHeadlineInsert = Rect(RandL,RandO,RandL+iRx,RandU);
            pPrinter->Canvas->StretchDraw(rectHeadlineInsert, bmpHeadLineRotate);

            // Bild
            // ScreenShot des akt. Canvas machen
            Graphics::TBitmap* bmpPanel = new Graphics::TBitmap();
            bmpPanel->Height = Height;
            bmpPanel->Width = Width;
            TRect rectPanel = Bounds(0, 0, Width, Height);
            bmpPanel->Canvas->CopyRect(rectPanel, this->Canvas, rectPanel);

            //-- Bild um 90° drehen (für Querformat)
            Graphics::TBitmap* bmpPanelRotate = new Graphics::TBitmap();
            bmpPanelRotate->Height = bmpPanel->Width;
            bmpPanelRotate->Width = bmpPanel->Height;

            for (int x=0; x<Width; x++)
            {
                int iHw=Width-1-x;
                for (int y=0; y<Height; y++) bmpPanelRotate->Canvas->Pixels[y][iHw]= bmpPanel->Canvas->Pixels[x][y];
            }

            // Bild wieder stretchen
            TRect rectPanelInsert = Rect(RandL+iRx, RandO, RandR, RandU);
            pPrinter->Canvas->StretchDraw(rectPanelInsert, bmpPanelRotate);

            // Bild Drucken
            pPrinter->EndDoc();

            // aufräumen
            if (bmpHeadLine) delete bmpHeadLine;
            if (bmpHeadLineRotate) delete bmpHeadLineRotate;
            if (bmpPanel) delete bmpPanel;
            if (bmpPanelRotate) delete bmpPanelRotate;
        }
    }

    if (pPrintDialog) delete pPrintDialog;
}