Tutorial1 - Delphi
Tutorial1 - Delphi
Tutorial1 - Delphi
COMPONENTS
TUTORIAL 1
graham@murtsoft.com
GmPrintSuite Tutorial Tutorial 1
Contents
Contents.................................................................................................................................................................. 2
What is GmPrintSuite............................................................................................................................................. 3
Tutorial Summary................................................................................................................................................. 17
www.murtsoft.com Page 2 of 17
GmPrintSuite Tutorial Tutorial 1
What is GmPrintSuite
GmPrintSuite is a collection of components, which provide the ability to generate & print reports with full
print preview capabilities.
TGmPreview is the main component in the GmPrintSuite set and provides the developer with a simple
preview & print interface with a rich set of features.
To demonstrate how easy TGmPreview is to use, the first example takes you, step-by-step, through writing &
printing the words “Hello World” to the preview page.
After the tutorial, each line of the code will be explained to give you a clearer understanding of what is
happening.
www.murtsoft.com Page 3 of 17
GmPrintSuite Tutorial Tutorial 1
STEP 1
STEP 2
STEP 3
www.murtsoft.com Page 4 of 17
GmPrintSuite Tutorial Tutorial 1
STEP 4
Below is an explaination of the code entered into the 2 button OnClick events.
GmPreview1.BeginUpdate;
This line sets TGmPreview into an Updating state and is required to prevent any unneccessary processing
while you are generating your page(s).
GmPreview1.Canvas.Font.Size := 50;
The TGmPreview has a canvas property like a Tform and it is through this that all drawing takes place.
This line sets the font size for all proceeding text operations to 50pt.
www.murtsoft.com Page 5 of 17
GmPrintSuite Tutorial Tutorial 1
GmPreview1.EndUpdate;
This last line brings the TGmPreview out of update mode and updates the on-screen display of the page.
GmPreview1.Print;
This is a simple call which will send all pages in the TGmPreview to the printer.
www.murtsoft.com Page 6 of 17
GmPrintSuite Tutorial Tutorial 1
The majority of reports that you will want to create will not fit on one page, fortunately TGmPreview has a
simple method adding extra pages.
Below is a demo which builds on the “Helo World” example above with the added functionallity of adding extra
pages.
This example also demonstrates how to navigate between pages in the report.
STEP 1
• A “Clear” button
• A “New Page” button
• A “Previous Page” button
• A “Next Page” Button
STEP 2
Modify the code in the “Generate” button’s OnClick event as shown below –
www.murtsoft.com Page 7 of 17
GmPrintSuite Tutorial Tutorial 1
STEP 3
Now in the OnClick events for the 4 new buttons add the following code…
STEP 4
www.murtsoft.com Page 8 of 17
GmPrintSuite Tutorial Tutorial 1
Below is an explaination extra code added to the original “Hello World” example.
This line adds an extra piece of text to each page which displays the page number.
GmPreview1.NewPage;
// now call the code for the "Generate" button...
btnGenerate.Click;
Here, a new page is added to the TGmPreview, then the code in the “Generate” button is called to perform
the drawing of the text to the new page.
GmPreview1.PrevPage;
&
GmPreview1.NextPage
These methods jump to, either the previous page or the next page of the report.
GmPreview1.Clear;
This clears all pages which exist so that only 1 blank page is left.
www.murtsoft.com Page 9 of 17
GmPrintSuite Tutorial Tutorial 1
In the real world, it is unlikely that you will have a large caption on the preview page telling you what the
current page number is, therefore, a good idea is to link the preview to a TLabel component which will tell us
the current page and also the total pages.
STEP 1
STEP 3
STEP 4
www.murtsoft.com Page 10 of 17
GmPrintSuite Tutorial Tutorial 1
If you are familiar to the TCanvas object, (for example – TForm.Canvas), then this next section should be easy
to understand as it uses the same principles.
The canvas is the name given to the surface on which we draw text and shapes.
Shapes and Text are drawn to the Canvas using the values of the canvas’ Font, Brush & Pen which represent
the standard TFont, TBrush, and TPen Delphi objects.
Modify the code from the previous example so that it includes the extra lines below –
Change the font size, styles & colors and observe the effects in the preview.
The Pen property will be introduced a bit later as it deals with shapes rather than text.
www.murtsoft.com Page 11 of 17
GmPrintSuite Tutorial Tutorial 1
One of the key features which makes the TGmPreview component so flexible is the wide range of
measurements with which it can operate.
So far in each of the above examples we have only used Millimeters although the following measurements
are also supported –
The way in which these measurements are used is identical to the “GmMillimeters” parameter we have used
in the above examples.
For example, the “Hello World” caption is currently drawn at an (x,y)of (30mm x 30mm). If we wanted to
draw this text at an (x,y) of (1 inch x 2 inch) we would modify the code so that it looked like this –
GmPreview1.Canvas.TextOut(1, 2, 'Hello World', GmInches);
Or possibly we wanted to use centimeters instead and draw the text at (10 x 15) centimers –
GmPreview1.Canvas.TextOut(10, 15, 'Hello World', GmCentimeters);
It really is as simple as that. Wherever you can use one of the measurements, you can use them all and this
applies not only for text but for all drawing methods including drawing Graphics, Shapes & Lines.
www.murtsoft.com Page 12 of 17
GmPrintSuite Tutorial Tutorial 1
TGmPreview has the ability to draw a header and/or footer on each generated page.
Headers and Footers can each contain 3 pieces of text, a left aligned, a center aligned and a right-aligned
caption. Each of these captions can be written using a different font size/style/colour etc…
Finally, the “Visible” property determines whether the Header/Footer is shown at all.
The TGmPreview component allows the use of certain text tags called “Tokens” within headers and footers
(and in page text for that matter) which are replaced with their represented values at the time of generating
the report.
www.murtsoft.com Page 13 of 17
GmPrintSuite Tutorial Tutorial 1
Using these tokens, it makes it simple to write, for example, “Page # of #” in the page footer (where the #’s
would be replaced with their numerical values)
Below is an example of setting up a page header and footer using the tokens explained in the previous section.
STEP 1
Here I have set the PaperSize to A5 and set the Zoom to 50% so that you will be able to see the page captions
more easily.
STEP 2
www.murtsoft.com Page 14 of 17
GmPrintSuite Tutorial Tutorial 1
You will notice that as you enter these captions, the TGmPreview component updates itself by adding the
captions to the page.
If you increase the Zoom property you will be able to read the text more clearly which should look like the
below image…
STEP 3
Left Caption
Set the left footer caption to
Scroll the preview down to the bottom of the page and you should see that the footer has also updated itself
with the new captions.
Also notice that the tokens have been replaced with their represented values.
www.murtsoft.com Page 15 of 17
GmPrintSuite Tutorial Tutorial 1
STEP 4
Try changing the font’s of the header and footer captions in the object inspector and see the changes updated
in the TGmPreview component with the new fonts.
STEP 5
As in previous examples, add three buttons – “New Page”, “Previous Page” and “Next Page”
The code for each of these buttons is shown again below…
STEP 6
Now run the program and click on the “New Page” button several times. As you navigate the pages using the
Previous and Next Page buttons you will notice that the header captions and the left-footer caption remain the
same but the right-footer caption changes from page to page as shown in the below image.
Header Captions and Left-Footer Caption This footer caption is updated from page to
remain the same on all pages. page.
www.murtsoft.com Page 16 of 17
GmPrintSuite Tutorial Tutorial 1
Tutorial Summary
After reading this tutorial and working through the exercises, you should now be aware of the following
TGmPreview functionality…
• Simple text drawing
• Adding pages
• Navigating through the pages using NextPage & PrevPage methods
(you could also include the methods TGmPreview.FirstPage & TGmPreview.LastPage)
• Changing the Canvas Font, Brush & Pen properties.
• Drawing Text using different units of measurement.
• The structure of Headers and Footers
• Using Tokens within Header/Footer captions.
Self Study
Look in the help file under the TGmPreview.Canvas methods. You will see that there are several other text
functions such as “TextOutCenter” and “TextOutRight”. These are extremely similar in structure to the
“TextOut” function we have already used so write a small demo to help familiarise yourselves with these
methods.
There are also a lot of other simple Canvas methods such as Rectangle and Ellipse. Look at their structure and
write a small demo using them. Experiment with changing the Canvas Pen & Brush properties before drawing
the shapes and observe the results in the preview.
www.murtsoft.com Page 17 of 17