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

📄 vcg12.htm

📁 Visual C++与数据库的连接经典实例
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>

<TITLE>vcg12.htm </TITLE>

<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm">

<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/htindex.htm">

<LINK REL="Next" HREF="vcgp4.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp4.htm">

<LINK REL="Previous" HREF="vcg11.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg11.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">

<A NAME="I0"></A><P ALIGN=CENTER>

<A HREF="vcg11.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg11.htm" TARGET="_self"><IMG SRC="blanprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blanprev.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm" TARGET="_self"><IMG SRC="blantoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blantoc.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="TOC"></A>

<A HREF="vcgp4.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp4.htm" TARGET="_self"><IMG SRC="blannext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blannext.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A>


<HR ALIGN=CENTER>

<P>

<UL>

<UL>

<UL>

<LI>

<A HREF="#E68E67" >Using Crystal Reports in New Visual C++ Applications</A>

<LI>

<A HREF="#E68E68" >Adding Crystal Reports to Existing Visual C++ Applications</A>

<UL>

<LI>

<A HREF="#E69E189" >Adding Crystal Reports to the Menu Bar</A>

<LI>

<A HREF="#E69E190" >Adding Crystal Reports Classes to Your Application</A>

<LI>

<A HREF="#E69E191" >Adding Handlers for Crystal Reports Menu Items</A>

<UL>

<LI>

<A HREF="#E70E61" >Open Report...</A>

<LI>

<A HREF="#E70E62" >Close Report</A>

<LI>

<A HREF="#E70E63" >Close All</A>

<LI>

<A HREF="#E70E64" >Print Preview</A>

<LI>

<A HREF="#E70E65" >Printer...</A>

<LI>

<A HREF="#E70E66" >Export...</A>

<LI>

<A HREF="#E70E67" >Mail...</A></UL>

<LI>

<A HREF="#E69E192" >Generating Reports for Crystal Reports</A></UL>

<LI>

<A HREF="#E68E69" >Using Crystal Reports Pro</A>

<LI>

<A HREF="#E68E70" >Using ReportEase with Visual C++ Applications</A>

<UL>

<LI>

<A HREF="#E69E193" >Creating Reports with ReportEase</A>

<LI>

<A HREF="#E69E194" >Printing Reports with ReportEase</A></UL>

<LI>

<A HREF="#E68E71" >Using ReportSmith with Visual C++ Applications</A>

<LI>

<A HREF="#E68E72" >Summary</A></UL></UL></UL>

<HR ALIGN=CENTER>

<A NAME="E66E31"></A>

<H1 ALIGN=CENTER>

<CENTER>

<FONT SIZE=6 COLOR="#FF0000"><B>- 12 -</B>

<BR><B>Printing Reports with Report Generators</B></FONT></CENTER></H1>

<BR>

<P>Previous versions of Visual C++ were oriented toward performing actions, not printing reports. C and C++ have never been noted for built-in printing prowess, so report generation has typically fallen on the programmer's shoulders. Prior to version 4, Visual C++ didn't have any report generator facilities or classes. Visual C++ 4 includes Crystal Reports, which programmers can redistribute with their applications. Also, several companies have produced products that let C/C++ programmers add report generation facilities to their Windows programs. This chapter highlights Crystal Reports and two add-on products, ReportEase and ReportSmith (a Borland product).

<BR>

<P>The first part of this chapter looks at what it takes to use the Crystal Reports package. Crystal Reports is the product of Seagate's Crystal division. Seagate acquired Crystal Services, Inc., in May of 1994. Crystal Reports packages are now included with Visual C++ 4 and a number of versions of Visual Basic. At the time this book was written, the most current version of Crystal Reports was 4.5. Visual Basic users are still using version 3, so we're ahead of the Visual Basic users for once!

<BR>

<P>Crystal Reports is an ODBC-based report generator (like ReportSmith) that doesn't directly interact with your application's data. The application must save the data in an ODBC-accessible database. Crystal Reports then generates the report from the database's contents and the report template.

<BR>

<P>In this chapter you will generate a project that uses the Crystal Reports AppWizard add-on that lets you quickly create Visual C++ applications with complete report-writing functionality. The Crystal AppWizard add-on looks very similar to the standard Visual C++ 4 AppWizard, but it includes additional options to include Crystal Reports functionality.

<BR>

<P>The final part of this chapter looks at ReportEase and ReportSmith. There are basic differences between these two products. ReportSmith is an ODBC application (like Crystal Reports) that can be called by a Visual C++ program (using DDE techniques) or a stand-alone application, and ReportEase is a non-ODBC application that must be incorporated into your application. ReportEase doesn't have a stand-alone report generator, so you must start your application in order to use ReportEase to print reports.

<BR>

<BR>

<A NAME="E68E67"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Using Crystal Reports in New Visual C++ Applications</B></FONT></CENTER></H3>

<BR>

<P>The folks at Crystal have made it easy to create a new project with Crystal Reports support built in. They've taken advantage of Visual C++ 4's ability to create custom AppWizard projects. This allows the creation of a project with Crystal Reports support built in with little or no effort on the programmer's part!

<BR>

<P>To create a new project with Crystal Reports support, start AppWizard by selecting File | New. In the New dialog box, select Project Workspace and click OK. In the New Project Workspace dialog box, select Crystal Reports AppWizard, as shown in Figure 12.1. Usually this selection will be at or near the bottom of the list, following Custom AppWizard.

<BR>

<P><B><A HREF="12vcg01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg01.gif">Figure 12.1. The Crystal Reports selection of the New Project Workspace dialog box.</A></B>

<BR>

<P>After you've selected Crystal Reports AppWizard and filled in the project's name and directory, click the Create button. The Crystal Reports AppWizard will be started, and you will see the first five Wizard dialogs that AppWizard displays for all MFC-type applications. The sixth Wizard dialog, shown in Figure 12.2, lets you configure the source of the reports that your application will allow the user to print. The two choices are to have a single, predefined report for the application or to allow the user to select reports using a dialog box.

<BR>

<P><B><A HREF="12vcg02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg02.gif">Figure 12.2. The sixth Crystal Reports AppWizard Wizard dialog: report source.</A></B>

<BR>

<P>For most applications, you'll want to let the user select from a number of predefined reports. Some simpler applications might have only one report defined. You can predefine the report to give the user easier report printing capabilities.

<BR>

<P>As soon as AppWizard has completed its design of the Crystal Reports application, you see the New Project Information dialog box, shown in Figure 12.3. This dialog lets you see what files, classes, and features are selected for your new project.

<BR>

<P><B><A HREF="12vcg03.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg03.gif">Figure 12.3. Crystal Reports' New Project Information dialog box.</A></B>

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>It's beyond the scope of this book to delve into the new custom AppWizard features that are part of Visual C++ 4. However, the Crystal Reports AppWizard is an excellent example of what can be done with the custom AppWizard features.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>Click the OK button to have AppWizard create the project's files and then open the project in Visual C++ as the current project. You can choose to either initially build the project (a good idea) or immediately start customizing the project.

<BR>

<P>Let's take a look at what's really offered in the Crystal Reports application. Notice that the original File | Print selections are still included. Also notice that the Crystal Reports AppWizard has added a new menu called Report. Under the Report menu are the options of Open Report, Close Report, Close All, Print Preview, Printer, Export, and Mail.

<BR>

<P>As you can see in Figure 12.4, ClassWizard already has handlers for each of these menu selections.

<BR>

<P><B><A HREF="12vcg04.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg04.gif">Figure 12.4. Visual C++'s ClassWizard showing handlers for the Crystal Reports menu items.</A></B>

<BR>

<P>Each of these handlers is placed in the application's CWinApp-based class. For example, in our program, the handlers are in the CCrystalReportsDemoApp class. Handlers are already written for each menu selection. (You don't have to do any programming to complete the implementation of the Crystal Reports portion of your application.)

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>Although you don't have to do major programming for most implementations of Crystal Reports in a Visual C++ MFC application, you do have to design all the reports that your application requires.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>The Crystal Reports report generator program can't be redistributed. You must either design the reports for your application or arrange for your application's users to obtain licensed copies of the Crystal Reports report generator.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>Each report must be created by you, the programmer. Generally, end users of your product won't have regular access to the Crystal Reports report generator, shown in Figure 12.5, but it might be possible to have end users assist in the development of the application's reports. Because report specification files aren't part of your application, you can at any point upgrade or modify the reports and redistribute them to your users. Nothing in an application is dependent on a specific report unless you design the application to open a fixed report by name.

<BR>

<P><B><A HREF="12vcg05.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg05.gif">Figure 12.5. The Crystal Reports Report Generator program.</A></B>

<BR>

<P>After designing a report (or several reports), you would usually include these reports in the same directory as the main application's executable file(s). Some applications have a separate folder for report specification files, especially when the application has a large number of reports. One way to avoid the problem of end users not having access to the report generator program is to give every conceivable report a separate folder (or a hierarchy of folders). This can greatly assist the users in managing their reports.

<BR>

<P>The next part of this chapter looks at what is necessary to add Crystal Reports to an existing 32-bit MFC Windows application.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>Crystal Reports is a 32-bit application. It can be used only with Visual C++ 4, although you might be able to make it work with Visual C++ 2.x by manually writing the interface calling code. You can't use Crystal Reports with any of the 16-bit versions of Visual C++.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E68E68"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Adding Crystal Reports to Existing Visual C++ Applications</B></FONT></CENTER></H3>

<BR>

<P>Sometimes an exciting new feature or option comes along after you've started developing your application. Crystal Reports and Visual C++ 4 are an example of this. Converting an existing Visual C++ 2.x application to Visual C++ 4 is a simple task. You just open the project and resave it as a Visual C++ 4 project. Adding Crystal Reports to an existing application is a bit more complex. You must manually add several new items to your application:

<BR>

<UL>

<LI>A menu structure, either as a top-level menu item (this is how the Crystal Reports AppWizard does it) or perhaps as a nested menu option under the application's File | Print option. It's possible to call all the Crystal Reports functions from a dialog box, but I don't recommend it.

<BR>

<BR>

<LI>The Crystal Reports classes file, PEPLUS.CPP, and the Crystal Reports header file, PEPLUS.H.

<BR>

<BR>

<LI>Handlers for each applicable menu item. The following sections look at each function added to the application and the handler that Crystal Reports AppWizard would have created.

<BR>

<BR>

</UL>

<P>You also must generate any reports that the application will require.

<BR>

<BR>

<A NAME="E69E189"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Adding Crystal Reports to the Menu Bar</B></FONT></CENTER></H4>

<BR>

<P>To add Crystal Reports to the application's menu bar, you must add a number of menu selections. The Crystal Reports AppWizard adds the following menu selections (in order) to the application's menu bar. Crystal Reports AppWizard adds a new top-level menu called Report, but there's no reason why you can't have this arranged as a submenu.

<BR>

<UL>

<LI>Open Report: This selection is present only if you design your application to allow the user to open (or use) multiple reports that won't be predefined in the application. If you don't use an Open Report menu option, you must have a menu option to allow the opening of a specific report. The default implementation of this menu option will open the report and display it in a print preview window.

<BR>

<BR>

<LI>Close Report: This menu selection allows the user to close a specific open report definition file.

<BR>

<BR>

<LI>Close All: This menu selection allows the user to close all currently open report definition files.

<BR>

<BR>

<LI>Print Preview: This menu selection opens an on-screen print preview that is very similar to Visual C++ MFC applications' print preview.

<BR>

<BR>

<LI>Printer: This menu selection allows an open report to be configured and printed.

<BR>

<BR>

<LI>Export: This menu selection lets the user export the report in a number of different formats, including Excel, DIFF, RTF, Word DOC, Lotus 1-2-3, and as a text file.

<BR>

<BR>

<LI>Mail: This menu selection lets the user send the report as a mail (MAPI) message. The Mail menu selection supports all the formats supported by the Export option.

<BR>

<BR>

</UL>

<P>After you've added menu selections to the menu and assigned them identifiers (you don't have to use the identifiers that I've listed, but it will make things easier if you do), you can add the handlers for each of these menu selections using ClassWizard.

<BR>

<P>Figure 12.6 shows the Crystal Reports menu structure added as a submenu under the application's File | Print menu option.

<BR>

<P><B><A HREF="12vcg06.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg06.gif">Figure 12.6. Crystal Reports added as a submenu under File | Print.</A></B>

<BR>

<BR>

<A NAME="E69E190"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Adding Crystal Reports Classes to Your Application</B></FONT></CENTER></H4>

<BR>

<P>To add the classes for Crystal Reports to your application's project, first copy PEPLUS.CPP and PEPLUS.H from the Crystal Reports main directory (C:\CRW if you installed Crystal Reports in the default installation directory). The PEPLUS files contain several classes that your Crystal Reports menu handles will use.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>You need to include the Crystal Reports .LIB file, CRPE32M.LIB, in your application's project. With Visual C++ 4, including PEPLUS.H and PEPLUS.CPP in your project is sufficient to force Visual C++ to include the correct library file. Crystal Reports includes a #pragma comment(lib, &quot;crpe32m.lib&quot;) statement in CRPE.H, which is included into your project by PEPLUS.CPP.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>To insert these new files into your project, select Insert | Files Into Project, and then select the file PEPLUS.CPP. (You don't need to include PEPLUS.H explicitly. It will be picked up when dependencies are scanned.) After you've added PEPLUS.CPP, your application's ClassView tab will look something like the one shown in Figure 12.7.

<BR>

<P><B><A HREF="12vcg07.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg07.gif">Figure 12.7. Visual C++'s ClassView tab after PEPLUS.CPP has been added to the project.</A></B>

<BR>

<P>Figure 12.7 shows that PEPLUS.CPP adds a large number of classes. Actually, these aren't classes; they're structures. However, the distinction between a class and a structure (a class has a constructor and a destructor, and a structure doesn't) isn't significant for the purposes of this chapter.

<BR>

<BR>

<A NAME="E69E191"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Adding Handlers for Crystal Reports Menu Items</B></FONT></CENTER></H4>

<BR>

<P>Earlier you added seven new menu items to an application. These menu items now need to have both a command handler and a update handler. The command handler is used to actually perform the work of the menu item, while the update handler performs the process of enabling and disabling the menu selections as appropriate. You will examine each of the menu items and learn about the command handler and the update handler. The default update handler generally has all the menu selections except Open Report disabled unless a report is open.

<BR>

<P>All the handlers are usually placed in the CWinApp-derived class. You must also add a number of supporting variables to this class, as shown in Listing 12.1. In addition, you must add #include PEPLUS.H to the CWinApp-derived class's include file list.

<BR>

⌨️ 快捷键说明

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