15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Пример печати из TStringGrid.

Компилятор: C++ Builder

Пример демонстрирует простую печать:

//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <Grids.hpp>
#include <Dialogs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TStringGrid *StringGrid1;
        TBitBtn *Populate;
        TBitBtn *SpitToForm2;
        TBitBtn *PrintMe;
        TPrintDialog *PrintDialog1;
        void __fastcall PopulateClick(TObject *Sender);
        void __fastcall SpitToForm2Click(TObject *Sender);
        void __fastcall PrintMeClick(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:	// IDE-managed Components
        TRichEdit *RichEdit1;
private:	// User declarations
public:		// User declarations
        __fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif

//Unit1.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#include "vclprinters.hpp"
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PopulateClick(TObject *Sender)
{
int rangeRow1 =  StringGrid1->RowCount;
int startRow1 =  StringGrid1->FixedRows;



for(; startRow1 <rangeRow1; startRow1++) { int rangeCol1="StringGrid1-">ColCount;
   int startCol1 =  StringGrid1->FixedCols;
   for(; startCol1 <rangeCol1; startCol1++) { String foo="rec" + IntToStr(startRow1 ) + "-" + IntToStr(startCol1); StringGrid1->Cells[startCol1][startRow1] = foo;
      }
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpitToForm2Click(TObject *Sender)
{

Form2->Show();
  int rangeRow2 =  StringGrid1->RowCount;
  int startRow2 =  StringGrid1->FixedRows;

  for(;startRow2 <= rangeRow2; startRow2++) { int rangeCol2="StringGrid1-">ColCount;
    int startCol2 =  StringGrid1->FixedCols;

    String bar = "";
    for(;startCol2 <= rangeCol2; startCol2++) { String poof ; poof +="StringGrid1-">Cells[startCol2][startRow2];
      for(int f = 0;f<(10 StringGrid1->Cells[startCol2][startRow2].Length());f++)
         poof =  poof + "  ";
      bar += poof;
      }
    Form2->RichEdit1->Lines->Add(bar);
    }
Form1->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PrintMeClick(TObject *Sender)
{

if(PrintDialog1->Execute())
  {
  TPrinter * thePrinter = Printer();
  thePrinter->BeginDoc();
  //ShowMessage(IntToStr(thePrinter->PageWidth));

  int i, x;
  String S("String Grid Printout");

  thePrinter->Canvas->Font->Name = "FixedSys";
  thePrinter->Canvas->Font->Size = 8;
  thePrinter->Canvas->Font->Pitch = TFontPitch() << fpFixed; thePrinter->Canvas->Font->PixelsPerInch = 400;
  x = thePrinter->Canvas->TextHeight(S);

   for(i = 0;  i <Form2->RichEdit1->Lines->Count; i++)
     {
     if ((i%(thePrinter->PageHeight/x))== 0)
       {
       if(i!=0)
         thePrinter->NewPage();
       }
     S = Form2->RichEdit1->Lines->Strings[i];
     thePrinter->Canvas->TextOut(1, (x* (i%(thePrinter->PageHeight/x)))+x, S);
     }
  thePrinter->EndDoc();
  }

}
//---------------------------------------------------------------------------