⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch17.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
	<META NAME="Author" Content="Steph Mineart">
	<TITLE>Ch 17 -- Printing: QuickReport and Related 
Technologies</TITLE>
</HEAD>

<BODY BACKGROUND="bg1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/bg1.gif" BGCOLOR="#FFFFFF">

<P ALIGN="CENTER"><IMG SRC="sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/sams.gif" WIDTH="75" HEIGHT="24" ALIGN="BOTTOM"
BORDER="0"><BR>
<BR>
<A HREF="index-3.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/index.htm"><IMG SRC="toc.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/toc.gif" 
WIDTH="40" HEIGHT="40" ALIGN="BOTTOM"
ALT="TOC" BORDER="0" NAME="toc4"></A><A HREF="ch16.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch16.htm"><IMG SRC="back-1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/back.gif"
WIDTH="40" HEIGHT="40" ALIGN="BOTTOM" ALT="BACK" BORDER="0" NAME="toc1"></A><A HREF="ch18.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch18.htm"><IMG
SRC="forward.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/forward.gif" 
WIDTH="40" HEIGHT="40" ALIGN="BOTTOM" ALT="FORWARD" BORDER="0"
NAME="toc2"></A></P>
<H2 ALIGN="CENTER"><FONT COLOR="#000077">Charlie Calvert's C++ Builder Unleashed</FONT></H2>
<P>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">- 17 
-</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Printing: QuickReport
and Related Technologies</FONT></H2>
<P>
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">Overview</FONT></H3>
<P>In this chapter you will get a look 
at seven ways to print data:

<UL>
	<LI>Printing data from databases via QuickReport
	<P>
	<LI>Printing QuickReport data in multiple columns and rows
	<P>
	<LI>Printing QuickReport data by group
	<P>
	<LI>Printing QuickReport data in one-to-many 
relationships
	<P>
	<LI>Using the <TT>TPrinter</TT> object to print a graphic image of a form exactly
	as the form appears to the user at runtime
	<P>
	<LI>Printing via the GDI, so that you can duplicate, fonts, pictures, and shapes
	drawn through the 
GDI
	<P>
	<LI>Using ReportSmith
</UL>

<P>I am going to explain QuickReport first, since it is by far the most important
technique. However, you should be sure to glance down at the other techniques, because
they are valuable and can be absorbed 
fairly quickly.</P>
<P>Printing has traditionally been a complicated subject in Windows. In DOS, it was
easier, but still very time-consuming. In BCB, all forms of printing are easy to
master, and QuickReport is a subject most programmers can learn in 
a few hours and
master in a few days. After you understand the basics of how to use QuickReport,
you should be able to prepare many reports in well under an hour. Even if you were
a perfectionist struggling to put together a complicated report, it is 
unlikely that
you would spend more than a few hours on a report.</P>
<P>If you are familiar with ReportSmith or Crystal Reports, you might be inclined
to use them first without examining QuickReport. I would, however, spend the time
it takes to get up 
to speed on QuickReport. You might be surprised to find how powerful
this tool can be. Certainly, QuickReport is almost preternaturally easy to use, especially
when considering how much quicker it makes a traditionally long, complex and frustrating

task.</P>
<P>Having said all this, I have to confess that printing is still a task I don't
enjoy much. I personally don't have much need for printed reports, because they take
up space, become outdated quickly, and are very difficult to track. As a 
result,
I prefer to keep information in electronic form. However, I recognize the importance
of printing, and will deal with it in this chapter in considerable depth.</P>
<P>One hint that can help you ease the burden of creating reports is to let SQL 
statements
carry the burden of the work. If you need to create a particular report, write a
SQL statement that will generate it as nearly as possible, and then let the statement
do double duty as a way of preparing an electronic report, and as a way 
of preparing
a paper-based report. This technique makes the task less onerous to me, and it is
usually the quickest way to complete the chore. In this chapter, I examine this technique
in my analysis of making a grouped report for the Music program.

<H3><A NAME="Heading4"></A><FONT COLOR="#000077">QuickReport Basics</FONT></H3>
<P>QuickReport was originally a third-party tool written in Object Pascal that the
VCL team decided to incorporate into the product. This unusual move came about as
a 
result of the high quality of this product, along with its relatively close conformance
to the VCL programming paradigm. You will, however, notice a few rough edges because
some of the conventions in this product do not exactly match the conventions 
used
elsewhere in the VCL.

<DL>
	<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The rough edges between the VCL
	and QuickReport will probably be smoothed over in later releases. As a result, you
	should be 
prepared for the possibility that your code in this one area might break
	in future releases of BCB. However, QuickReport is easy to use, and the code associated
	with it is by definition isolated from the rest of the code in your application.
	As a 
result, you should be able to adapt to the change fairly easily. <BR>
	<BR>
	Because QuickReport is made by a third party, you should look on the Web to see if
	you can find updates to this product. For instance, Delphi 3 will ship with a version
	of 
QuickReport that is much more sophisticated than the version included with BCB
	1.0. 
<HR>


</BLOCKQUOTE>

<P>The basic idea behind QuickReport is to provide a set of components that are as
closely parellel as possible to the tools you use when 
creating a standard BCB form.
For instance, Table 17.1 gives a list of standard visual controls and visual database
controls with their QuickReport equivalents. <BR>
<BR>
<B>Table 17.1. Standard VCL controls on the left and QuickReport equivalents on 
the
right.</B><FONT SIZE="2"> </FONT>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="109" ALIGN="LEFT"><B>VCL controls</B></TD>
		<TD WIDTH="157" ALIGN="LEFT"><B>QuickReport controls</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD 
WIDTH="109" ALIGN="LEFT"><TT>TLabel</TT></TD>
		<TD WIDTH="157" ALIGN="LEFT"><TT>TQRLabel</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="109" ALIGN="LEFT"><TT>TDBEdit</TT></TD>
		<TD WIDTH="157" ALIGN="LEFT"><TT>TQRDBText</TT></TD>
	
</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="109" ALIGN="LEFT"><TT>TDBLabel</TT></TD>
		<TD WIDTH="157" ALIGN="LEFT"><TT>TQRDBText</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="109" ALIGN="LEFT"><TT>TDBMemo</TT></TD>
		<TD 
WIDTH="157" ALIGN="LEFT"><TT>TQRMemo</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD WIDTH="109" ALIGN="LEFT"><TT>TShape</TT></TD>
		<TD WIDTH="157" ALIGN="LEFT"><TT>TQRShape</TT></TD>
	</TR>
</TABLE>
<BR>
<BR>
As you can see, there is no 
difference between a QuickReport label and a QuickReport
edit control. Needless to say, this makes sense when you consider the fact that you
can't edit a field in a report!</P>
<P>Each of the data-aware controls has a <TT>DataSource</TT> and 
<TT>DataField</TT>
property. The <TT>TQRLabel</TT> and <TT>TQRShape</TT> controls do not have a these
properties, because they are not data-aware.</P>
<P>Each report that you generate appears on its own form. The act of creating the
form is a simple 
matter of laying out <TT>TQRMemo</TT> and <TT>TQRDBText</TT> controls
on a series of bands called <TT>TQRBand</TT> controls.</P>
<P>One visual QuickReport component that does not fit into any predefined category
is called <TT>TQRSysData</TT>. This 
component can display the current project title,
page number, date, time, or record count. The particular function adopted by the
component is controlled by its <TT>Data</TT> property. I will explain how to use
the component in more depth later in the 
chapter.</P>
<P>Each time you create a new QuickReport form, you should also drop down a <TT>TQuickReport</TT>
component and at least one <TT>TQRBand</TT> component. (Don't confuse the <TT>TQuickReport</TT>
component with the product itself, which is 
called QuickReport.) The <TT>TQuickReport</TT>
component is a &quot;magic&quot; component that converts a regular form into a QuickReport
form. All you need to do is drop the component on the form and set its <TT>DataSource</TT>
property to a 
<TT>TDataSource</TT>, just as you would when using a <TT>TDBGrid</TT>.
The <TT>TQRBand</TT> component is the surface on which you should place the QuickReport
visual controls.

<DL>
	<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT 
COLOR="#000077"><B>NOTE:</B></FONT><B> </B>In most reports you want to print
	multiple rows, as you would when showing data in a grid. To do this, set the <TT>BandType</TT>
	property of a <TT>TQRBand</TT> report to <TT>rbDetail</TT>. If you want to 
have multiple
	columns in your report, set the <TT>Columns</TT> property of <TT>TQuickReport</TT>
	to the number of columns you want to use. I will explain all this in more depth later
	in this chapter. 
<HR>


</BLOCKQUOTE>

<P>After you finish 
designing a form, you can run the program and call the <TT>Preview</TT>
method of <TT>TQuickReport</TT> to view the output. The preview form has a print
button on it so you can print the report. If you want, you can skip the preview screen
and 
directly call the <TT>Print</TT> method of <TT>TQuickReport</TT>.</P>
<P>The great thing about QuickReport is that it lets you use existing datasets, including
calculated fields and lookup fields, that are already part of your project. Rather
than 
concerning yourself with creating custom queries or with setting up sort orders,
you can just plug your QuickReport components directly into your existing datasets.
If you sort the data in some particular way on a form, that is the same sort order
you 
see when you pop up a QuickReport based on the tables used by that form.</P>
<H3><FONT COLOR="#000077">Using TQRBand: Creating Rows and Columns in QuickReport</FONT></H3>
<P>Most people can figure out how to use QuickReport without help from a book 
like
this. However, there are a few things that people sometimes can't figure out on their
own:

<UL>
	<LI>How to print rows of data
	<P>
	<LI>How to print columns of data
	<P>
	<LI>How to group data
	<P>
	<LI>How to create a one-to-many report
</UL>


<P>All four of these subjects will be covered in this chapter. I've already described
how to perform the first two chores earlier, but I will repeat the information here,
just so these key bits of information will be as easy as possible to find.</P>

<P>To create multiple rows of data, set the <TT>BandType</TT> property of a <TT>TQRBand</TT>
report to <TT>rbDetail</TT>. If you want to have multiple columns in your report,
set the <TT>Columns</TT> property of <TT>TQuickReport</TT> to the number of 
columns
you want to use. That's all you have to do.</P>
<P>If you dig into the QuickReport controls a bit, you will find the <TT>TQRBand</TT>
component has a <TT>BandType</TT> property that can be set to one of the values in
Table 17.2.<BR>
<BR>

<B>Table 17.2. BandType property values and purposes. </B>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Value</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Purpose</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		
<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbTitle</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A title printed once at the start of the report.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbPageHeader</TT></TD>
		<TD 
ALIGN="LEFT" VALIGN="TOP">Appears at the top of each page.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbDetail</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Use this type if you want to repeat many rows of data, or for 
use with one-to-many
			reports.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbSubDetail</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">The detail band in a one-to-many report. This connects the <TT>DetailGroup</TT> band
			
to the <TT>DetailLink</TT> component using the <TT>QRDetailLink.DetailBand</TT> property.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbPageFooter</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Creates a footer at the 
bottom of the page.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbSummary</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Creates a summary at the end of the report.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD 
ALIGN="LEFT" VALIGN="TOP"><TT>rbGroupHeader</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Creates group headers for use with <TT>QRGroup</TT> and <TT>QRDetailLink</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" 
VALIGN="TOP">components.</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbGroupFooter</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Creates footers for use with <TT>QRGroup</TT> and 
<TT>QRDetailLink</TT> components.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbColumnHeader</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Labels each column in a column-based report.</TD>
	</TR>
	<TR ALIGN="LEFT" 
rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>rbOverlay</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Floats on top of all other text and graphics printed on the page.</TD>
	</TR>
</TABLE>
<BR>
<BR>
The best way to learn about these properties is 
through experience. In particular,
you will find that the <TT>BandType</TT> property should be set to reflect the types
of components being used in a particular context. For instance, the <TT>TQRSysData</TT>
component, which is often used to display 
the date, time, or page count will generally
go on <TT>TQRBand</TT> components set to <TT>rbPageHeader</TT> or <TT>rbPageFooter</TT>.
This is only logical, because page count, date, and time information usually appears
on the header or footer of a 
report.</P>
<P>The easiest way to learn how to use <TT>TQRBand</TT>s and their related components
is through experience. The next several sections of this chapter give working examples
of using these components.</P>
<H3><A NAME="Working with the 
Sample Programs"></A><FONT COLOR="#000077">Working
with the Sample Programs</FONT></H3>
<P>In the next few pages I describe the reports from all three major database programs
found in this book:

<UL>
	<LI>The reports for the flat file Address2 
program illustrate the basic facts about
	using QuickReport, including grouping.
	<P>
	<LI>The reports for the relational kdAdds and Music program show how to create master
	detail reports, and how to write custom queries that make it easy to create 
reports.
</UL>

<P>Many people could probably learn how to use QuickReport by running these programs
and popping up the various preview pages. After they are familiar with the different
styles I use in my preview pages, they can just go back to design 
mode and study
my QuickReport forms to see how I achieved a particular effect. In other words, I
expect many people will learn by example.</P>
<P>The text that follows is therefore a bit sketchier than what you find elsewhere
in the book. The issue 
here is simply that this is primarily a mechanical task that
takes only a minimal amount of understanding. As a result, I will try to point you
in the right direction wherever possible, and then step out of the way so you can
experiment on your own 
computer.</P>
<H4><A NAME="The Address2 Program"></A><FONT COLOR="#000077">The Address2 Program</FONT></H4>
<P>I add five reports to the Address2 program from Chapter 13, &quot;Flat-File, Real-World
Databases.&quot; The first prints only addresses, 
the second prints only phone numbers,
the third prints all the data in the report, and the fourth prints reports by grouping
them on the <TT>Category</TT> field. Later in the chapter, I will also include a
section that describes how to print a report 
from inside of ReportSmith.</P>
<H4><A NAME="Address2: Printing Address"></A><FONT COLOR="#000077">Address2: Printing
Address</FONT></H4>
<P>This report has the task of printing out addresses. I do not include phone numbers,
just addresses--as if I 
were making labels. A copy of the finished report as it appears
in preview mode is shown in Figure 17.1. Figure 17.2 shows the same report in design
mode.</P>
<P>To create the report, start by laying down a <TT>TQuickReport</TT> component.
Connect it 
to the <TT>AddressSource</TT> on the program's data module. To do this,
you will need to include <TT>DMod1</TT> in your project by pulling down the File
menu and selecting Include Unit Header.<BR>
<BR>
<A NAME="FIGURE"></A><A 
HREF="17ebu01.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/17/17ebu01.jpg">FIGURE 17.1.</A><FONT COLOR="#000077">
</FONT><I>The Address Report from the Address2 program.<BR>
<BR>
<A NAME="Heading8"></A></I><A HREF="17ebu02.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/17/17ebu02.jpg">FIGURE 17.2.</A><FONT COLOR="#000077">
</FONT><I>The Address 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -