home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / Print.exe / mainform.cpp next >
C/C++ Source or Header  |  1998-02-09  |  5KB  |  140 lines

  1. //---------------------------------------------------------------------------
  2. // Borland C++Builder
  3. // Copyright (c) 1987, 1998 Borland International Inc.  All Rights Reserved.
  4. //---------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8. #include <printers.hpp>
  9.  
  10. #include "mainform.h"
  11. //---------------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. TForm1 *Form1;
  14. //---------------------------------------------------------------------------
  15. __fastcall TForm1::TForm1(TComponent* Owner)
  16.         : TForm(Owner)
  17. {
  18.  
  19. }
  20. //---------------------------------------------------------------------------
  21. void __fastcall TForm1::PrintBtnClick(TObject *Sender)
  22. {
  23.  
  24.      int trips;         // number of times to print from first to last
  25.      int dups;          // number of copies of each page to print each time
  26.      int firstpg;
  27.      int lastpg;
  28.      bool needsnewpg=false;
  29.  
  30.  
  31.      // make sure the current page is saved before printing
  32.      pages[TabSet1->TabIndex]->Text = Memo1->Text;
  33.  
  34.      if (!PrintDialog1->Execute())
  35.           return; // user did not press OK
  36.  
  37.      // if we're not collating, we only go from the first page to last page once
  38.      // but each time through we print duplicates of each page.
  39.      if (!PrintDialog1->Collate)
  40.      {
  41.         trips = 1;
  42.         dups = PrintDialog1->Copies;
  43.      }
  44.      // if we're collating, we go from the first page to the last page
  45.      // multiple times but only print one duplicate each time through.
  46.      else
  47.      {
  48.         trips = PrintDialog1->Copies;
  49.         dups = 1;
  50.      }
  51.  
  52.      // user has selected to print all pages...
  53.      if (PrintDialog1->PrintRange == prAllPages)
  54.      {
  55.         firstpg = 1;
  56.         lastpg = 5;
  57.      }
  58.      // ...or a range of pages
  59.      else
  60.      {
  61.          firstpg = PrintDialog1->FromPage;
  62.          lastpg = PrintDialog1->ToPage;
  63.      }
  64.  
  65.      // Initialize print job
  66.      Printer()->BeginDoc();
  67.  
  68.      // Title to appear in the print manager and network print banners
  69.      Printer()->Title = "Borland C++Builder Printer Example";
  70.  
  71.      for (int x=0; x<trips; x++)                    // for each pass
  72.           for (int y=firstpg-1; y<lastpg; y++)      // for each page
  73.                for (int z=0; z<dups; z++)           // for each dup of the page
  74.                {
  75.                     if (needsnewpg)     // page 1 does not need a page break
  76.                        Printer()->NewPage();
  77.                     PrintPage(y);      // print the page
  78.                     needsnewpg = true; // subsequent pages need page breaks
  79.                }
  80.  
  81.      Printer()->EndDoc();
  82. }
  83. //---------------------------------------------------------------------
  84. void __fastcall TForm1::FormShow(TObject *Sender)
  85. {
  86.      // initialize variables
  87.      currentpage=0;
  88.      for (int i=0; i<5; i++)
  89.      {
  90.          pages[i] = new TStringList;
  91.          pages[i]->Text = "Page " + IntToStr(i+1);
  92.      }
  93.  
  94.      // the first page is the initial page
  95.      Memo1->Text = pages[0]->Text;
  96. }
  97. //---------------------------------------------------------------------
  98. void __fastcall TForm1::TabSet1Click(TObject *Sender)
  99. {
  100.      // When a tab is selected, put the text for that page into the memo control
  101.      Memo1->Text = pages[TabSet1->TabIndex]->Text;
  102. }
  103. //---------------------------------------------------------------------
  104. void __fastcall TForm1::PrintPage(int page)
  105. {
  106.      int pageline=0; // keeps track of the line number as we print for correct
  107.                      // positioning of text on the page
  108.  
  109.      // print a banner
  110.      Printer()->Canvas->TextOut(20,
  111.           (10+Printer()->Canvas->TextHeight("Hi there!"))*pageline,
  112.           "Borland C++Builder Printer Example -- Page " + IntToStr(page+1));
  113.      pageline+=2;
  114.  
  115.      for (int line=0; line < pages[page]->Count; line++)
  116.      {
  117.           Printer()->Canvas->TextOut(20,
  118.                (10+Printer()->Canvas->TextHeight("Hi there!"))*pageline,
  119.                pages[page]->Strings[line].c_str());
  120.           pageline++;
  121.      }
  122. }
  123. //---------------------------------------------------------------------
  124. void __fastcall TForm1::TabSet1Change(TObject *Sender, int NewTab,
  125.       bool &AllowChange)
  126. {
  127.       // before we leave this tab, save the text for this page
  128.       pages[TabSet1->TabIndex]->Text = Memo1->Text;
  129. }
  130. //---------------------------------------------------------------------
  131. __fastcall TForm1::~TForm1(void)
  132. {
  133.      // release dynamic data
  134.      for (int i=0; i<5; i++)
  135.      {
  136.          delete pages[i];
  137.      }
  138. }
  139. //---------------------------------------------------------------------
  140.