Solved

Sample of printing draw/string Grid Contents

Posted on 1998-06-19
1
1,140 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

17 Experts available now in Live!

Get 1:1 Help Now