Solved

Sample of printing draw/string Grid Contents

Posted on 1998-06-19
1
1,172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

707 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