Solved

Sample of printing draw/string Grid Contents

Posted on 1998-06-19
1
1,145 Views
Last Modified: 2008-02-01
I'm looking for a good sample of how to print the contents of a string or draw grid.
0
Comment
Question by:blitz051697
1 Comment
 
LVL 3

Accepted Solution

by:
vladika earned 200 total points
ID: 1354394
The main idea: use QuickReport and OnNeedData event

Here is example

// ----------------
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TTest = class(TForm)
    StringGrid: TStringGrid;
    TestFillBtn: TButton;
    TestPrintBtn: TButton;
    procedure TestFillBtnClick(Sender: TObject);
    procedure TestPrintBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Test: TTest;

implementation

uses PrnFrm;

{$R *.DFM}

procedure TTest.TestFillBtnClick(Sender: TObject);
var I, J: Integer;
begin
  for I := 0 to StringGrid.RowCount-1 do
    for J := 0 to StringGrid.ColCount-1 do
      StringGrid.Cells[J,I] := Format('Test %d %d', [I, J]); // fill StringGrid with test value
end;

procedure TTest.TestPrintBtnClick(Sender: TObject);
begin
  PrintForm.StringGrid := StringGrid;
  PrintForm.QuickRep.Preview;  // preview report
end;

end.
// ------- end of Main Unit -----

object Test: TTest
  Left = 206
  Top = 139
  Width = 367
  Height = 330
  Caption = 'Test'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid: TStringGrid
    Left = 8
    Top = 8
    Width = 225
    Height = 281
    ColCount = 3
    FixedCols = 0
    RowCount = 100
    FixedRows = 0
    TabOrder = 0
  end
  object TestFillBtn: TButton
    Left = 256
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Test Fill'
    TabOrder = 1
    OnClick = TestFillBtnClick
  end
  object TestPrintBtn: TButton
    Left = 256
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Test Print'
    TabOrder = 2
    OnClick = TestPrintBtnClick
  end
end

// ---- end of Test Form (Main program form)

unit PrnFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  quickrpt, Qrctrls, ExtCtrls, Grids;

type
  TPrintForm = class(TForm)
    QuickRep: TQuickRep;
    QRBand1: TQRBand;
    QRLabel1: TQRLabel;
    QRLabel2: TQRLabel;
    QRLabel3: TQRLabel;
    procedure QuickRepNeedData(Sender: TObject; var MoreData: Boolean);
    procedure QuickRepBeforePrint(Sender: TQuickRep;
      var PrintReport: Boolean);
  private
    { Private declarations }
    FStringGrid: TStringGrid;
    FCurrentRow: Integer;
  public
    { Public declarations }
    property StringGrid: TStringGrid read FStringGrid write FStringGrid;
  end;

var
  PrintForm: TPrintForm;

implementation

{$R *.DFM}

procedure TPrintForm.QuickRepNeedData(Sender: TObject; var MoreData: Boolean);
begin
  MoreData := FCurrentRow < FStringGrid.RowCount;
  if not MoreData then Exit;  // no more data

  QRLabel1.Caption := FStringGrid.Cells[0, FCurrentRow];  // set report data from StringGrid
  QRLabel2.Caption := FStringGrid.Cells[1, FCurrentRow];
  QRLabel3.Caption := FStringGrid.Cells[2, FCurrentRow];
  Inc(FCurrentRow);
end;

procedure TPrintForm.QuickRepBeforePrint(Sender: TQuickRep;
  var PrintReport: Boolean);
begin
  FCurrentRow := 0;  // init CurrentRow
end;

end.
// ------ end of PrnFrm Unit

object PrintForm: TPrintForm
  Left = 200
  Top = 108
  Width = 544
  Height = 375
  Caption = 'PrintForm'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  Scaled = False
  PixelsPerInch = 96
  TextHeight = 13
  object QuickRep: TQuickRep
    Left = 0
    Top = 0
    Width = 794
    Height = 1123
    Frame.Color = clBlack
    Frame.DrawTop = False
    Frame.DrawBottom = False
    Frame.DrawLeft = False
    Frame.DrawRight = False
    BeforePrint = QuickRepBeforePrint
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Arial'
    Font.Style = []
    OnNeedData = QuickRepNeedData
    Options = [FirstPageHeader, LastPageFooter]
    Page.Columns = 1
    Page.Orientation = poPortrait
    Page.PaperSize = A4
    Page.Values = (
      100
      2970
      100
      2100
      100
      100
      0)
    PrintIfEmpty = False
    PrinterSettings.Copies = 1
    PrinterSettings.Duplex = False
    PrinterSettings.FirstPage = 0
    PrinterSettings.LastPage = 0
    PrinterSettings.OutputBin = First
    SnapToGrid = True
    Units = MM
    Zoom = 100
    object QRBand1: TQRBand
      Left = 38
      Top = 38
      Width = 718
      Height = 35
      Frame.Color = clBlack
      Frame.DrawTop = False
      Frame.DrawBottom = False
      Frame.DrawLeft = False
      Frame.DrawRight = False
      AlignToBottom = False
      Color = clWhite
      ForceNewColumn = False
      ForceNewPage = False
      Size.Values = (
        92.6041666666667
        1899.70833333333)
      BandType = rbDetail
      object QRLabel1: TQRLabel
        Left = 24
        Top = 0
        Width = 58
        Height = 17
        Frame.Color = clBlack
        Frame.DrawTop = False
        Frame.DrawBottom = False
        Frame.DrawLeft = False
        Frame.DrawRight = False
        Size.Values = (
          44.9791666666667
          63.5
          0
          153.458333333333)
        Alignment = taLeftJustify
        AlignToBand = False
        AutoSize = True
        AutoStretch = False
        Caption = 'QRLabel1'
        Color = clWhite
        Transparent = False
        WordWrap = True
        FontSize = 10
      end
      object QRLabel2: TQRLabel
        Left = 248
        Top = 0
        Width = 58
        Height = 17
        Frame.Color = clBlack
        Frame.DrawTop = False
        Frame.DrawBottom = False
        Frame.DrawLeft = False
        Frame.DrawRight = False
        Size.Values = (
          44.9791666666667
          656.166666666667
          0
          153.458333333333)
        Alignment = taLeftJustify
        AlignToBand = False
        AutoSize = True
        AutoStretch = False
        Caption = 'QRLabel2'
        Color = clWhite
        Transparent = False
        WordWrap = True
        FontSize = 10
      end
      object QRLabel3: TQRLabel
        Left = 472
        Top = 0
        Width = 58
        Height = 17
        Frame.Color = clBlack
        Frame.DrawTop = False
        Frame.DrawBottom = False
        Frame.DrawLeft = False
        Frame.DrawRight = False
        Size.Values = (
          44.9791666666667
          1248.83333333333
          0
          153.458333333333)
        Alignment = taLeftJustify
        AlignToBand = False
        AutoSize = True
        AutoStretch = False
        Caption = 'QRLabel3'
        Color = clWhite
        Transparent = False
        WordWrap = True
        FontSize = 10
      end
    end
  end
end

// end of Print Form

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now