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

📄 vcg12.htm

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

<P>

<FONT COLOR="#000080"><B>Listing 12.1. Crystal Reports support variables.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">CRPEngine    m_printEng;

CRPEJob     *m_pPrintJob;

char         szFileTitle[256];</FONT></PRE>

<BR>

<A NAME="E70E61"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Open Report...</B></FONT></CENTER></H5>

<BR>

<P>There are two possible ways to implement the Open Report menu selection. The first and most flexible is to present an open files dialog box listing the different report files available to the user. Because Windows 95 allows long filenames, the reports can have meaningful filenames. A second implementation would be to present a (fixed) list of reports for the user to select from.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>TIP</B>

<BR>

<BR>In keeping with the philosophy of not doing more work than necessary, the easiest way to create the basic code for your Crystal Reports handlers is to first create a dummy Crystal Reports application using the Crystal Reports AppWizard (or the Crystal Reports sample application included on the CD that comes with this book) and then cut and paste from this dummy application into your main application. In fact, all the handlers shown next were actually created by the Crystal Reports AppWizard when it created a sample program.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The handler for Open Report is shown in Listing 12.2. This handler shows what Crystal Reports does when it opens a report file. This code first initializes the Crystal Reports report print engine (if it's not already initialized) and then displays the Open File common dialog box to allow the user to select a filename.

<BR>

<P>If the user enters a valid filename, the Crystal Reports report engine is run with the newly opened report by calling m_pPrintJob = m_printEng.OpenJob(), which opens the Crystal Reports report job; m_pPrintJob-&gt;OutputToWindow(), which displays the Print Preview window; and m_pPrintJob-&gt;Start(), which prints the report in the Print Preview window. As Listing 12.2 shows, each of these calls has error checking to ensure that the function is successful.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.2. The Open Report command handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">/////////////////////////////////////////////////////////////////////////////

// CCrystalReportsDemoApp commands

// Handler for Crystal Reports Open Report Command

void CCrystalReportsDemoApp::OnOpenReport()

{

    OPENFILENAME    ofn;

    char    szFile[256];

    UINT    i;

    CString csFilter, csDirName;

    char    chReplace, *szFilter, *szDirName;

    // char szFileTitle[] defined as data member

    // Open the Crystal Reports Print Engine if it hasn't been opened.

    if ( m_printEng.GetEngineStatus() != CRPEngine::engineOpen ) {

        // Verify the Print Engine is not missing.

        VERIFY( m_printEng.GetEngineStatus() != CRPEngine::engineMissing );

        // If Print Engine cannot be opened, display the error message

        // and the Print Engine error code.

        if ( !m_printEng.Open() ) {

            AfxMessageBox( m_printEng.GetErrorText() );

        }

    }

    // THE CODE BELOW IS ONLY A SAMPLE. YOU MAY MAKE ANY CHANGE OR WRITE

    // YOUR OWN CODE FOR YOUR SPECIFIC NEEDS.

    szFile[0] = '\0';

    // Load the CRW directory

    szDirName = csDirName.GetBuffer(256);

    if ( !csDirName.LoadString(IDS_CRWDIRECTORY) ) {

        TRACE0( &quot;Cannot load String Table&quot; );

    }

    // Load the File_Filter String

    szFilter = csFilter.GetBuffer(256);

    if ( !csFilter.LoadString(IDS_FILTERSTRING) ) {

        TRACE0(&quot;Can not load String Table&quot;);

    }

    chReplace = csFilter[csFilter.Find('\0') - 1]; // Retrieve wildcard

    for (i = 0; szFilter[i] != '\0'; i++) {

        if (szFilter[i] == chReplace)

            szFilter[i] = '\0';

    }

    memset(&amp;ofn, 0, sizeof(OPENFILENAME)); // Set all structure members to zero

    // Fill in struct OPENFILENAME

    ofn.lStructSize = sizeof(OPENFILENAME);

    ofn.hwndOwner = this -&gt; m_pMainWnd-&gt;m_hWnd;

    ofn.lpstrFilter = szFilter;

    ofn.nFilterIndex = 1;

    ofn.lpstrFile = szFile;

    ofn.nMaxFile = sizeof(szFile);

    ofn.lpstrFileTitle = szFileTitle;

    ofn.nMaxFileTitle = sizeof(szFileTitle);

    ofn.lpstrInitialDir = szDirName;

    ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |

        OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;

    // Open a Crystal Reports Print Job.

    if ( GetOpenFileName( &amp;ofn ) ) {

        CRPEJob *pOldJob = m_pPrintJob;

        if ( ( m_pPrintJob=m_printEng.OpenJob( szFile ) ) == NULL) {

            AfxMessageBox( m_printEng.GetErrorText() );

            m_pPrintJob = pOldJob;

            return;

        }

        // YOU MAY CHANGE THE SIZE AND STYLE OF WINDOW FOR PREVIEW REPORT HERE

        if ( !m_pPrintJob-&gt;OutputToWindow(    szFileTitle, 50, 50, 550, 500,

            CW_USEDEFAULT | WS_SYSMENU | WS_THICKFRAME |

            WS_MAXIMIZEBOX | WS_MINIMIZEBOX,

            0) ) {

            AfxMessageBox( m_pPrintJob-&gt;GetErrorText() );

            m_pPrintJob = pOldJob;

            return;

        }

        if ( !m_pPrintJob-&gt;Start() ) {

            AfxMessageBox( m_pPrintJob-&gt;GetErrorText() );

            m_pPrintJob -&gt; Close();

            m_pPrintJob = pOldJob;

        }

    }

}</FONT></PRE>

<P>It wouldn't be difficult to create multiple open reports, because the m_pPrintJob member variable returned by the call to OpenJob() could easily be stored in a list.

<BR>

<P>Since the Open Report menu option is always active, the update handler isn't used. You can code an update handler that does nothing or code no handler at all.

<BR>

<BR>

<A NAME="E70E62"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Close Report</B></FONT></CENTER></H5>

<BR>

<P>Closing a report is easier than opening one. It isn't necessary to ask the user which report to close; the current report is the one that gets closed. For example, the default Crystal Reports handler for Close Report, shown in Listing 12.3, simply closes the print window, closes the actual report, and sets the print job member variable m_pPrintJob to NULL (which the update handlers use to enable and disable the other menu selections).

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.3. The Close Report command handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">void CCrystalReportsDemoApp::OnCloseReport()

{

    // Close a Crystal Reports print job.

    m_pPrintJob -&gt; CloseWindow();

    m_pPrintJob -&gt; Close();

    m_pPrintJob = NULL;

}</FONT></PRE>

<P>The update handler disables the Close Report menu selection based on whether there is a currently open Crystal Reports report. The variable m_pPrintJob is non-NULL if an existing Crystal Reports report is open. Listing 12.4 shows the update handler for this menu item.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.4. The Close Report update handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">void CCrystalReportsDemoApp::OnUpdateCloseReport( CCmdUI* pCmdUI )

{

    pCmdUI -&gt; Enable( m_pPrintJob != NULL );

}</FONT></PRE>

<BR>

<A NAME="E70E63"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Close All</B></FONT></CENTER></H5>

<BR>

<P>Closing all reports is easier than closing a specific report. It isn't necessary to ask the user which report to close; all reports are closed automatically. For example, the default Crystal Reports handler for Close Report, shown in Listing 12.5, simply sets the print job member variable m_pPrintJob to NULL (which the update handlers use to enable and disable the other menu selections) and then calls the print engine's Close() member function.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.5. The Close All command handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">void CCrystalReportsDemoApp::OnCloseAllReport()

{

    // Close the Print Engine, thereby closing all print jobs.

    m_pPrintJob = NULL;

    m_printEng.Close();

}</FONT></PRE>

<P>The update handler disables the Close All menu selection based on the value returned by the GetNPrintJobs() function. Listing 12.6 shows the update handler for this menu item.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.6. The Close All update handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">void CCrystalReportsDemoApp::OnUpdateCloseAllReport( CCmdUI* pCmdUI )

{

    pCmdUI -&gt; Enable( m_printEng.GetNPrintJobs() );

}</FONT></PRE>

<BR>

<A NAME="E70E64"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Print Preview</B></FONT></CENTER></H5>

<BR>

<P>Crystal Reports initially calls Print Preview when a report is first opened. The user can close the Print Preview window (without closing the actual report) and then later reopen it by selecting the Crystal Reports Print Preview menu option.

<BR>

<P>As you can see from Listing 12.7, the Print Preview handler calls the OutputToWindow() function exactly as it is called in the Open Report handler. It then calls the Start() function to do the actual printing in the Print Preview window. There is less error handling in the Print Preview handler, because the Open Report handler will have closed the report if Print Preview failed at that point. If you remove the automatic Print Preview from the Open Report handler, you should make sure that your Print Report and Print Preview handlers are sufficiently rugged to handle any problems that might arise.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.7. The Print Preview command handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">void CCrystalReportsDemoApp::OnPreviewReport()

{

    if ( !m_pPrintJob-&gt;OutputToWindow(    szFileTitle, 50, 50, 550, 500,

         CW_USEDEFAULT | WS_SYSMENU | WS_THICKFRAME |

         WS_MAXIMIZEBOX | WS_MINIMIZEBOX, 0) ) {

        AfxMessageBox( m_pPrintJob-&gt;GetErrorText() );

        return;

    }

    if ( !m_pPrintJob-&gt;Start() )

    {

        AfxMessageBox( m_pPrintJob-&gt;GetErrorText() );

    }

}</FONT></PRE>

<P>The update handler disables the Print Preview menu selection based on whether a Crystal Reports report is currently open. The variable m_pPrintJob is non-NULL if an existing Crystal Reports report is open. Listing 12.8 shows the update handler for this menu item.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.8. The Print Preview update handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">void CCrystalReportsDemoApp::OnUpdatePreviewReport( CCmdUI* pCmdUI )

{

    pCmdUI -&gt; Enable(m_pPrintJob!= NULL );

}</FONT></PRE>

<BR>

<A NAME="E70E65"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Printer...</B></FONT></CENTER></H5>

<BR>

<P>Printing reports entails displaying the Windows print dialog box (which lets the user select a printer, select which pages to print, and so on) and then printing the report.

<BR>

<P>When the report is printed, the user-entered information (such as starting and ending pages) is passed to the Crystal Reports print engine. Then the Crystal Reports printout is performed by calling OutputToPrinter() and then the Start() function, as shown in Listing 12.9. Do you notice something here? The Start() function is called regardless of whether the report is going to the screen (Print Preview) or the printer.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.9. The Print command handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">void CCrystalReportsDemoApp::OnPrinterReport()

{

    CRPEPrintOptions printOpt;

    CPrintDialog printDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES

                                | PD_HIDEPRINTTOFILE | PD_NOSELECTION,

                          this-&gt;m_pMainWnd);

    printDlg.m_pd.nMaxPage = 0xFFFF;

    printDlg.m_pd.nMinPage = 1;

    printDlg.m_pd.nFromPage = 1;

    printDlg.m_pd.nToPage = 0xFFFF;

    if(printDlg.DoModal() == IDCANCEL)

        return;

    if( printDlg.PrintRange() ) {

        printOpt.m_startPageN = (unsigned short)printDlg.GetFromPage();

        printOpt.m_stopPageN = (unsigned short)printDlg.GetToPage();

    }

    printOpt.m_nReportCopies = (unsigned short)printDlg.GetCopies();

    m_pPrintJob -&gt; SetPrintOptions( &amp;printOpt );

    m_pPrintJob -&gt; OutputToPrinter( (short)printOpt.m_nReportCopies );

    if ( !m_pPrintJob-&gt;Start() )

    {

        AfxMessageBox( m_pPrintJob-&gt;GetErrorText() );

    }

}</FONT></PRE>

<P>The update handler disables the Print menu selection based on whether there is a currently open Crystal Reports report. The variable m_pPrintJob is non-NULL if an existing Crystal Reports report is open. Listing 12.10 shows the update handler for this menu item.

<BR>

⌨️ 快捷键说明

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