Professional Documents
Culture Documents
COMPONENTS
TUTORIAL 3
graham@murtsoft.com
Printing TStringGrids with the TGmGridPrint component Tutorial 3
Contents
Introduction ............................................................................................................................................................ 3
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.
STEP 1
STEP 2
In the “Draw Grid” button’s OnClick event, place the following code –
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
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.
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
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
STEP 2
Now modify the GridToPage call so that the width parameter now reads 150 (m) as shown below…
www.murtsoft.com Page 6 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3
STEP 3
www.murtsoft.com Page 7 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3
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 –
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
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.
Now, double click on the TGmGridPrint’s OnGetCellAlignment event and enter the following code…
Right-aligned column
www.murtsoft.com Page 9 of 10
Printing TStringGrids with the TGmGridPrint component Tutorial 3
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.
www.murtsoft.com Page 10 of 10