c++快速开发例子--打印

这段代码展示了如何使用Borland C++ Builder进行打印操作,包括设置打印次数、副本数量、页码范围,并在打印过程中处理页面布局和标题。程序首先保存当前页面,然后根据用户的打印设置进行多次打印,每次打印可能包含多个副本。对于每个页码,程序会打印页面内容并在必要时插入新的页面。

 

#include <vcl.h>
#pragma hdrstop
#include <printers.hpp>

#include "mainform.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
        : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TFormMain::PrintBtnClick(TObject *Sender)
{

     int trips;         // number of times to print from first to last
     int dups;          // number of copies of each page to print each time
     int firstpg;
     int lastpg;
     bool needsnewpg=false;


     // make sure the current page is saved before printing
     pages[TabSet1->TabIndex]->Text = Memo1->Text;

     if (!PrintDialog1->Execute())
          return; // user did not press OK

     // if we're not collating, we only go from the first page to last page once
     // but each time through we print duplicates of each page.
     if (!PrintDialog1->Collate)
     {
        trips = 1;
        dups = PrintDialog1->Copies;
     }
     // if we're collating, we go from the first page to the last page
     // multiple times but only print one duplicate each time through.
     else
     {
        trips = PrintDialog1->Copies;
        dups = 1;
     }

     // user has selected to print all pages...
     if (PrintDialog1->PrintRange == prAllPages)
     {
        firstpg = 1;
        lastpg = 5;
     }
     // ...or a range of pages
     else
     {
         firstpg = PrintDialog1->FromPage;
         lastpg = PrintDialog1->ToPage;
     }

     // Initialize print job
     Printer()->BeginDoc();

     // Title to appear in the print manager and network print banners
     Printer()->Title = "Borland C++Builder Printer Example";

     for (int x=0; x<trips; x++)                    // for each pass
          for (int y=firstpg-1; y<lastpg; y++)      // for each page
               for (int z=0; z<dups; z++)           // for each dup of the page
               {
                    if (needsnewpg)     // page 1 does not need a page break
                       Printer()->NewPage();
                    PrintPage(y);      // print the page
                    needsnewpg = true; // subsequent pages need page breaks
               }

     Printer()->EndDoc();
}
//---------------------------------------------------------------------
void __fastcall TFormMain::FormShow(TObject *Sender)
{
     // initialize variables
     currentpage=0;
     for (int i=0; i<5; i++)
     {
         pages[i] = new TStringList;
         pages[i]->Text = "Page " + IntToStr(i+1);
     }

     // the first page is the initial page
     Memo1->Text = pages[0]->Text;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::TabSet1Click(TObject *Sender)
{
     // When a tab is selected, put the text for that page into the memo control
     Memo1->Text = pages[TabSet1->TabIndex]->Text;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::PrintPage(int page)
{
     int pageline=0; // keeps track of the line number as we print for correct
                     // positioning of text on the page

     // print a banner
     Printer()->Canvas->TextOut(20,
          (10+Printer()->Canvas->TextHeight("Hi there!"))*pageline,
          "Borland C++Builder Printer Example -- Page " + IntToStr(page+1));
     pageline+=2;

     for (int line=0; line < pages[page]->Count; line++)
     {
          Printer()->Canvas->TextOut(20,
               (10+Printer()->Canvas->TextHeight("Hi there!"))*pageline,
               pages[page]->Strings[line].c_str());
          pageline++;
     }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::TabSet1Change(TObject *Sender, int NewTab,
      bool &AllowChange)
{
      // before we leave this tab, save the text for this page
      pages[TabSet1->TabIndex]->Text = Memo1->Text;
}
//---------------------------------------------------------------------
__fastcall TFormMain::~TFormMain(void)
{
     // release dynamic data
     for (int i=0; i<5; i++)
     {
         delete pages[i];
     }
}
//-------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moneytree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值