Deep QuickReport (3)
Author: Dongwei Chun
(This article has been published in the "9CBS Development Master" in the first, second and third phases, and the requirements of the majority of netizens, published in the 9CBS Author's document, which is slightly modified, but only here, not After 9CBS or author I agree with any individual and website must not be reproduced, you can take it, otherwise any of the consequences involved in copyright.
Part III: Other issues in the report
Through the first two parts, I think you have a certain master of Quickrep. In this part, we will make a brief introduction to some other questions in the report design, maybe these are not common, but the same content is to learn QuickRep is still very beneficial.
This part of the content is explained in the form of routines. In order to maintain the integrity of the article, the number of the example is connected to the part.
Example 5, the connection and preservation of reports
Through the above example, you may do a few reports, sometimes you will be able to connect some of the reports, form a comprehensive report, and operate as a whole. It is not difficult to achieve this in BCB, we should use the TQRCompositEreport component at this time. It provides an onaddreprots event that will trigger this event when creating a report, so we can use the Add method in this event to add a report that needs to be connected to the component in this event. Here is an example block, which is added to the two reports to the comprehensive report, the code is as follows:
Void __fastcall tform1 :: qrcompositereportaddreports (TOBJECT * SENDER)
{
((TqrcompositerEport *) Sender) -> report-> add (form2-> quickrep1); // Add the first report
((TqrcompositerEport *) sender) -> Reports-> add (form3-> quickrep1); // Add a second report
}
We must save it, save the file format: text format file (TXT), component tqrtextFilter; Hypertext Format file (HTML / HTM), component TQRHTMLFilder; comma separator file (CSV), component TQRCSVFILTER; And report documents. Save the first three format files need to call the EXPORTTOFILTER method, and directly save the report components, just use Save. In this example we put a TQRTextFilter, TQRHTMLFilder, TQRCSVFilter, three components in the TQRTextFilter, TQRHTMLFilder, TQRCSVFilter page in the QuickReport component page. The complete code is as follows:
Void __fastcall tform1 :: savereportClick (Tobject * Sender)
{
Ansistring fileext;
/ / Open the save file dialog to get the file name
IF (SaveDialog1-> Execute ())
{
// Get a file suffix
FileExt = ANSIUPPERCASE (ExtractFileExt (Savedialog1-> FileName);
// Output HTML hypertext file
IF ((FILEEXT == ".html") || (FileExt == ".htm"))))
QuickRep1-> EXPORTTTOFILTER (New TqrhtmldocumentFilter); // Output TXT text file
Else IF (fileext == ".txt")
QuickRep1-> ExportTOFILTER (New TqrasciiexportFilter (Savedialog1-> FileName);
// Output CSV file
Else IF (fileext == ".csv")
QuickRep1-> EXPORTTTOFILTER (New TQRCommaseParatedFilter (Savedialog1-> FileName);
/ / Output report file
Else
{
QuickRep1-> qrprinter-> save (Savedialog1-> filename);
}
}
}
/ / -------------------------------------------------------------------------------------------- -------------------
Example 6. Custom Report Preview Window
QuickReport's report preview features can not achieve satisfactory results, so we have necessary to customize the preview window for the quick report to achieve the perfect design requirements.
1) Set a preview window
New project, add a Toolbar control on the Form1 form and add the following buttons to: "Turn on Report", "Print", "Print Settings", "Previous", "Next", "Zoom "," Narrow "and" off ". Add a Statusbar on the Form1 form, double-click the component, insert three items in the editor, display the page information in the third item. Add a TQRPREVIEW control on the Form1 form, aligned with the AlClient, the FORM1 form appearance is shown in Figure 1:
Buy a form again, set its name to FORM2, add a TQUickRep control on the form, set its name to QuickRep1, and the Units attribute in its printerSetting is set to mm (in millimeter as a unit), and then establishes a report.
2) Programming implementation
(1) Select QuickRep1 on Form2, select OnPreview in its event, enter the following code:
#include
#pragma HDRSTOP
#include "unit2.h"
#include "unit1.h" // Call the contents of Form1
/ / -------------------------------------------------------------------------------------------- -------------------
#pragma package (smart_init)
#pragma resource "* .dfm"
TFORM2 * FORM2;
/ / -------------------------------------------------------------------------------------------- -------------------
__fastcall tform2 :: tform2 (tcomponent * Owner)
: TFORM (OWNER)
{
}
/ / -------------------------------------------------------------------------------------------- -------------------
Void __fastcall tform2 :: quickreppreview (TOBJECT * Sender)
{
FORM1-> qrpreview1-> qrprinter = form2-> quickrep1-> qrprinter; // The most critical step}
/ / -------------------------------------------------------------------------------------------- -------------------
(2) Add the following code for the onclick event for each function button in Form1:
#include
#pragma HDRSTOP
#include "unit1.h"
#include "unit2.h" // Call the contents of form2
/ / -------------------------------------------------------------------------------------------- -------------------
#pragma package (smart_init)
#pragma resource "* .dfm"
TFORM1 * FORM1;
/ / -------------------------------------------------------------------------------------------- -------------------
__fastcall tform1 :: tform1 (tComponent * Owner)
: TFORM (OWNER)
{
}
/ / -------------------------------------------------------------------------------------------- -------------------
// Turn on the report
Void __fastcall tform1 :: qrloadclick (Tobject * Sender)
{
FORM2-> QuickRep1-> prepare (); // does not join the sentence, the next three pages will not appear correctly
Statusbar1-> panel-> items [2] -> text = "第 " " > Pagenumber) " page " " total " INTOSTR (Form2-> QuickRep1-> Pagenumber) page "
FORM2-> QuickRep1-> preview (); // Display page information
}
/ / -------------------------------------------------------------------------------------------- -------------------
// Print the report
Void __fastcall tform1 :: printclilck (TOBJECT * SENDER)
{
FORM2-> QuickRep1-> Print ();
}
/ / -------------------------------------------------------------------------------------------- -------------------
//Printing settings
Void __fastcall tform1 :: printsetupclick (Tobject * Sender)
{
FORM2-> QuickRep1-> printersetup ();
FORM2-> QuickRep1-> print (); // Don't add this sentence can only be set, can not print, in my computer test is like this
}
/ / -------------------------------------------------------------------------------------------- -------------------
// Previous page
Void __fastcall tform1 :: PageupClick (TOBJECT * SENDER)
{
IF (qrpreview1-> pagenumber> 1)
Qrpreview1-> Pagenumber--;
Statusbar1-> panel-> items [2] -> text = "第 " " > Pagenumber) " page " " total " INTOSTR (Form2-> QuickRep1-> Pagenumber) page "}
/ / -------------------------------------------------------------------------------------------- -------------------
// Next page
Void __fastcall tform1 :: pagedownloadnclick (TOBJECT * SENDER)
{
IF (qrpreview1-> pagenumber
Qrpreview1-> Pagenumber ;
Statusbar1-> panel-> items [2] -> text = "第 " " > Pagenumber) " page " " total " INTOSTR (Form2-> QuickRep1-> Pagenumber) page "
}
/ / -------------------------------------------------------------------------------------------- -------------------
/ / Report amplification
Void __fastcall tform1 :: zoominclick (TOBJECT * SENDER)
{
IF (qrpreview1-> zoom <200)
Qrpreview1-> zoom = 5;
}
/ / -------------------------------------------------------------------------------------------- -------------------
/ / Report reduction
Void __fastcall tform1 :: zoomoutclick (TOBJECT * Sender)
{
IF (qrpreview1-> zoom> 5)
Qrpreview1-> zoom- = 5;
}
/ / -------------------------------------------------------------------------------------------- -------------------
// Program close
Void __fastcall tform1 :: closeclick (Tobject * Sender)
{
CLOSE ();
}
/ / -------------------------------------------------------------------------------------------- -------
It's just a simple design, you can do it more powerful, more beautiful appearance.
Dynamic setting and discussion of QR components
A large number of VCL components are provided in the BCB, sometimes it is necessary to dynamically create components in the program, and the VCL is written with Object Pascal, so the VCL class object can only be created in the heap. If you create a TQRLABLE object, we can create: tqrlable * myqrlable = new tqrlable (from1); it is written as described below:
Class name * Object name = new class name (...); Note: () can be the parent class name, NULL or THIS of the project you have created. But it is best to be the parent class name of the object. Example: Dynamically generate TQRLABLE Components Let us first on the form (Form1), put a tquickrep and put a Band on it. In the Form1 Form, click the following code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {tqrlable * myqrlable = new tqrlable (from1); myqrlable -> parent = Band1; // Most key One, otherwise you will not see anything, but compile is the correct myqrlable -> top = 10; myqrlable -> left = 38; myqrlable -> height = 25; myqrlable -> width = 100; myqrlable -> caption = "I'm myqrlable!";} Through this example we should clearly see several important steps to dynamically create a QR component: 1) To a space (memory); // tqrlable * myqrlable = new tqrlable (from1); 2 ) Specify its parent components, saying that it is to specify a container for this object we want to create; // myqrlable -> Parent = BAND1; 3) Specify which position to appear in the parent component; // myqrlable -> TOP = 10; myqrlable -> left = 38; myqrlable -> height = 25; myqrlable -> width = 100; 4) Other important properties. // myqrlable -> caption = "I'm myqrlable!"; Note that the above steps cannot be arranged any, otherwise your program will joke. Add the corresponding header file when dynamically generating the original components of the non-Baolian VCL. We must join: #include "qrctrls.hpp" // If there is a problem, you have to join: #include "quickrpt.hpp" additional BCB conflicts to the system, hardware Your dynamic creation program may not be a little wrong, but it is compiled; sometimes it passed, the second time the same procedure can't pass, there is such a prompt, the simplest method is to cancel the system, Try again, most of them can solve it. Since such a dynamic generating component will have a lot of unable to meet, have we still have a better way to achieve a similar function? Yes, the answer is certain.
We can place all components in tquickRep, all segment components, the visual components are placed in position, just some specific attributes we don't give it, and through the program, you can imitate dynamic creation Component's method to dynamically generate reports. It is simple to say that it is ready to put the components in advance. When you use it, you don't have to give a critical property, that is, use the properties to malfaxially, let it do not work.
Example: Dynamic Settings QrDbText Properties Void__fastcall tform1 :: Button1Click (Tobject * Sender)
{
QrDBText1-> Dataset = Table1;
QrDbText1-> datafield = "area"; // After the double quotation number, everything OKJ
Quickrep1-> preview ();
Table1-> close ();
}
/ / -------------------------------------------------------------------------------------------- ---------------------------
or
Void __fastcall tform1 :: button1click (Tobject * Sender)
{
QrDBText1-> Dataset = Table1;
Qrdbtext1-> datafield = Table1-> Fields-> Fields [3] -> FieldName;
Quickrep1-> preview ();
}
/ / ---------------------------------------------
Note: The following programs are wrong
Void __fastcall tform1 :: button1click (Tobject * Sender)
{
QrDBText1-> Dataset = Table1;
QrDbText1-> Datafield = Table1-> FieldValues ["Area"]; // Note that this will not get what you want, because he is not named Datafield
Quickrep1-> preview ();
}
Single a QuickReport dynamically generated report, I think it is not three words, I don't have to pass the text, I have said that some common problems in the dynamic generated report have been said, and the top of the solution is also mentioned. Also given several examples, through these description, if you have energy, you will definitely make a dynamic generation reporting system that does not have problems.
The above programs are compiled under the XP system, BCB6, and EPSON C43UX.
Conclusion: Write here I believe that most readers should have a more comprehensive and in-depth understanding of QuickReport. But after I read this article carefully, I found that there were still many things that I didn't write it, so I realized that I would like to write QuickReport all in an article, I really have some difficulties, this is good, after a point Write, let's don't leave? J
In fact, it is simple, QuickReport is not difficult, but sometimes we look at her, but did not expect that QuickReport is then going on, she appeared on the BCB, now she can say that she is completely complete. Get into the VCL component. So, if you want to know the QUICKREPORT, the key is depends on our degree of mastery of VCL.
Acknowledgments: First, I would like to thank Han Lei, who was gave me about this article. I felt that some friends needed such an article and thanked him for valuable comments from this article. Of course, there are 9CBS Xiong Jianguo, Ouyang, especially Ouyang's brother, and he has made meticulous modifications to the article. The final form of this article is inseparable from his hard work and inin. Finally, I would like to thank my girlfriend Wang Yan, she helped me complete the full text of the text.