You are on page 1of 10

GMPRINTSUITE REPORT & PRINT PREVIEW

COMPONENTS

TUTORIAL 3

PRINTING TSTRINGGRIDS WITH THE TGMGRIDPRINT


COMPONENT

graham@murtsoft.com
Printing TStringGrids with the TGmGridPrint component Tutorial 3

Contents

Introduction ............................................................................................................................................................ 3

Previewing a TStringGrid with 1 Line of Code...................................................................................................... 3

TGmGridPrint’s GridToPage Method Explained................................................................................................... 4

Scaling the Width of the Drawn Grid ..................................................................................................................... 6

Default Cell Alignments & the OnGetCellAlignment Event.................................................................................. 8

TGmGridPrint’s DefaultCellAlignments .......................................................................................................... 8

Using the OnGetCellAlignment Event ............................................................................................................. 9

Changing the Colour of cell Fonts & Backgrounds.............................................................................................. 10

www.murtsoft.com Page 2 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

Introduction

TGmGridPrint is a component, which links to a TGmPreview component to facilitate the previewing and
printing of TStringGrids.

This tutorial will focus on the different functionality that the TGmGridPrint component allows, from simple
TStringGrid printing to custom drawn grid cells.

Previewing a TStringGrid with 1 Line of Code

STEP 1

Start a new application and add a


TGmPreview, TGmGridPrint, a
TStringGrid and a TButton to the
Form.

Set the TGmGridPrint’s Preview


property to the TGmPreview.

Also set the TStringGrid’s Editing


option to True so that you can enter
values in the cells at run-time.

STEP 2

In the “Draw Grid” button’s OnClick event, place the following code –

procedure TForm1.btnDrawGridClick(Sender: TObject);


begin
GmGridPrint1.GridToPage(30, 30, 0, GmMillimeters, StringGrid1);
end;

It would also be a good idea to set the TGmPreview’s Zoom property to 80 (or more) so that you can read any
text that may be in the grid cells.

www.murtsoft.com Page 3 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

STEP 3

Run the program and type some text in


the TStringGrid’s cells.

When you want to preview the grid, click


the DrawGrid button and you will see the
results in the TGmPreview.

Here you can see the text in the cells


which have been drawn when the
TGmPreview’s Zoom property is set to
100 (%)

TGmGridPrint’s GridToPage Method Explained

Below is an explanation of the parameters which are taken by the TGmGridPrint’s GridToPage method which
we have used above to output the grid to the page.

GmGridPrint1.GridToPage(30, 30, 0, GmMillimeters, StringGrid1);

The first 2 parameters are the x and y values of the top-left corner of the grid on the page. These values are
given in the measurement specified by the 4th parameter (GmMillimeters) - so using these values we have
requested that the top-left corner of the grid should be at an (x,y) of 30mm x 30mm.

www.murtsoft.com Page 4 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

The 3rd parameter is the desired width of the grid, again this value is specified by the value of the 4th parameter
(GmMillimeters). Setting this parameter to ‘0’ tells the component to draw the grid at 100% (screen size).
If the screen width of the grid is 60mm and this parameter is set to 100mm, the component will scale the grid
columns proportionally so that the preview/printed width is 100mm.
Also, if the ‘ScaleText’ parameter is set to true (default) then the text in the cells is also resized. This is so that
no cell text is cut-off if the columns are reduced in width.

The last parameter, ‘StringGrid1’, specifies the TStringGrid you wish to output to the page.

www.murtsoft.com Page 5 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

Scaling the Width of the Drawn Grid

Sometimes, there may be the case when the grid, which needs to be drawn, is wider than the page… In this
situation, you will need to alter the Width parameter of the GridToPage method so that the columns are
proportionally reduced in size.

STEP 1

Modify the application so that the


StringGrid now has 12 columns.
(i.e. set it’s ColCount property to
12)

Now run the program and click on


the “Draw Grid” button.

You will see that the grid which is


drawn is too wide to fit on the
page.

STEP 2

Now modify the GridToPage call so that the width parameter now reads 150 (m) as shown below…

GmGridPrint1.GridToPage(30, 30, 150, GmMillimeters, StringGrid1);

www.murtsoft.com Page 6 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

STEP 3

Now re-run the program and click on


the “Draw Grid” button.

The grid’s columns have now been re-


sized to fit on the page.

As the start X position is 30mm, and


the page width is 210mm (A4), setting
the width of the grid to 150mm means
that there will be a 30mm margin to
the left and right of the grid.
(30mm + 150mm + 30mm = 210mm)

www.murtsoft.com Page 7 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

Default Cell Alignments & the OnGetCellAlignment Event

It’s not always the case that you want to draw the text in the cells aligned to the left, especially when dealing
with currency and/or decimal values.

There are two ways of changing the text alignment when using the TGmGridPrint component, you can set the
default Horizontal and Vertical cell text alignment using the object inspector so that all cells use the default
values…
Or you can use the OnGetCellAlignment event to alter the alignment on a cell-by-cell basis.

TGmGridPrint’s DefaultCellAlignments

Select the TGmGridPrint component and open the Object Inspector. You will see two properties called –

• DefaultCellAlignment - Horizontal cell text alignment


• DefaultCellVertAlignment - Vertical cell text alignment

Set the DefaultCellAlignment to


taRightJustify, set the
DefaultCellVertAlignment to
gmMiddle and set the TGmPreview’s
Zoom to 100 (%)

Now run the program, type some text


in the TStringGrid cells and click on
the Draw Grid button.

You will see (as in the image opposite)


that all text is now aligned to the right
of the cells.

In the example above, you can see that I have made one of the Grid rows higher than the others so that the
effects of the vertical alignment are apparent.

www.murtsoft.com Page 8 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

Using the OnGetCellAlignment Event

More often than not, you will find that you only want 1 or 2 columns right aligned, and the rest left aligned.
To achieve this we need to use the OnGetCellAlignment event.

Reset the TGmGridPrint default alignment values to taLeftJustify and gmTop.

Now, double click on the TGmGridPrint’s OnGetCellAlignment event and enter the following code…

procedure TForm1.GmGridPrint1GetCellAlignment(Sender: TObject; AGrid: TDrawGrid;


Col, Row: Integer; var Alignment: TAlignment; var VertAlignment:
TGmVertAlignment);
begin
if Col = 2 then Alignment := taRightJustify;
end;

Now run the program, enter some


values in the TStringGrid’s cells and
click on the ‘Draw Grid’ button.

You will see that all cells are left


aligned (default) except for those
which are in column 2 where they
are right aligned because of our
event code.

Right-aligned column

www.murtsoft.com Page 9 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3

Changing the Colour of cell Fonts & Backgrounds

The background colour and font colour of a cell can be easily changed when the grid is drawn to the page by
placing the relevant code in the OnDrawCell event of the TGmGridPrint component.

The way in which this is achieved is by modifying the font and background in the OnDrawCell event, then
calling the DefaultDrawCell method to draw the cell with the new colour properties.

If the DefaultDrawCell is not called, you will need to draw any text/images to the cell manually, much like the
OnDrawItem of many VCL controls.

Double click on the TGmGridPrint’s OnDrawCell event in the Object Inspector.


In this event, enter the following code…

procedure TForm1.GmGridPrint1DrawCell(Sender: TObject; AGrid: TDrawGrid;


Col, Row: Integer; ARect: TGmValueRect; ACanvas: TGmCanvas);
begin
if Col = 2 then ACanvas.Font.Color := clRed;
if (Col = 3) and (Row > 0) then ACanvas.Brush.Color := clYellow;

// call the default drawing method...


GmGridPrint1.DefaultDrawCell(AGrid, ARect, Col, Row);
end;

Run the program and enter similar


values as before in the cells… then
click on the ‘Draw Grid’ button.

You will see that any text column 2


is now drawn in red and all cells in
column 3 have a yellow background.

This can be useful for example when


attention needs to be drawn to
negative currency values… You can
either write the value in red or give it
a red background.

www.murtsoft.com Page 10 of 10

You might also like