Easily implement report processing with Visual C ++

xiaoxiao2021-03-17  224

Easily implement report handlers with Visual C : Wang Jun

Foreword Database is widely used in the enterprise software system, while the report's display and printing becomes the necessary functions of such software. Unfortunately, VC does not integrates the report acceptance tool, but its powerful features plus the functional report processing tools on the market make this work easily. This article describes the Crystal Report (Crystal Report) using Seagate in the VC environment. To implement the display and printing of the report, and provide sample code. I. Preparation (1) Installing the Crystal Report Software Crystal Report 4.6 is a free report tool that can be found on CDs such as VB5 or Windows NT 4.0 BackOffice. Crystal Report4.6 contains report design tools and report print controls. The issuance of the program only needs to install the print control related section. (2) Design Reports We generate a report called Sample.rpt, which can be referred to the help documentation comes with the Crystal Report, which is not described in detail. 2. Creating a VC Project Add Report Control In the VC integration environment, you will build an MFC single document project in AppWizard in the VC integration environment, and the rest of the options are default. Select Project-> Add to Project-> Components and Controls ... Pop-up components, control selection dialog box, enter Registered ActiveX Controls, select Crystal Report Control 4.6, click the Insert button, confirm, and enter the class configuration dialog box, press By default. Turns off the control selection dialog Completes the addition of the control. Third, the display and printing of the report is under the dialog box's static creation process, and the dynamic creation process of the control is demonstrated in the main view. 3.1 Print the report in the dialog box to open the ID of the IDD_ABOUTBOX in the resource editor, in the Controls toolbar, we can find the Crystal Report Control 4.6 control button just added to the project to insert it into the dialog. At the location. Right-click on the controls in the dialog, select Properties, and you can set up a number of properties for the control. We entered the report file name Sample.rptfilename in the Control Options page ReportFileName, set the appropriate location of the control in the Print Window option page, and press and hold the Ctrl key in the dialog template, double-click the left mouse button, The Add Member Variable dialog pops up, we name the member variable m_crystalReport, open the classwizard, add the process function for the WM_INITDIALOG message for the CaboutDLG dialog: BOOL OnItDialog (), and do the following processing in the function

Bool caboutdlg :: OnNitDialog () {cdialog :: OnIndIalog (); / This row sets the parent window of the control, you can also remove the row to see the running effect / m_crystalreport.SetWindowParentHandle ((Long) (THIS "(THIS system_hwnd); / Print report to the window / m_crystalreport.printreport (); return true;}

At this point, the first part of the program is written, compiled, open the About dialog to see the effect! Oh! Not bad! If you don't show a report, there is a possible reason: (1) The control is not placed in a suitable location or size is wrong. (2) The report file itself exists such as a data source to be unavailable. The toolbar in the control window provides scaling, printing and other functions, you can also try the effect of printing on the printer. 3.2 Displaying the report in the program main window Open ClassWizard to increase the handler for id_file_open and id_file_print, the code is as follows void cmyreportview :: onfileopen () {char filter [] = "crystal report files (*. Rpt) | *. Rpt | All Files (*. *) | *. * || "; CRECT RECT; CFiledialog OpenDLG (True, 0, 0, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, (LPCTSTSTSTSTER) FILTER, NULL); if (OpenDLG.Domodal ()! = IDOK) // / Display file dialog return; cstring m_fname = OPENDLG.GETPATHNAME (); /// gets the file name if (m_crystalreport) m_crystalreport.destroyWindow (); getClientRect (RECT); /// creation control /// if (! M_crystalReport. create (AfxRegisterWndClass (0, AfxGetApp () -> LoadStandardCursor (IDC_ARROW)), WS_CHILD | _ WS_VISIBLE, rect, this, IDC_CRYSTALREPORT1)) {AfxMessageBox ( "controls creation failed!"); return;} m_CrystalReport.SetWindowParentHandle ((long) (this-> m_hwnd)); /// Set the parent window m_crystalreport.SetWindowBorderstyle (0); /// set to no border m_crystalreport.SetWindowleft (0); /// Left space M_CrystalReport.SetWindowTop (0); /// headspace m_CrystalReport.SetWindowControls (FALSE); /// not show the toolbar m_CrystalReport.SetReportFileName (m_fName); /// set the report file m_CrystalReport.SetWindowWidth (rect.Width ()); /// set the window width m_CrystalRepo Rt.SetWindowHeight (Rect.Height ()); /// Settings Window Height M_CryStalReport.setFormulas (0, "Company = /" VC Knowledge Base / "); // Set the value of the COMPANY variable in the report to VC Knowledge m_CrystalReport.SetDestination (0); /// set the output destination is the screen m_CrystalReport.PrintReport (); /// Show report} void CMyReportView :: OnFilePrint () {if (m_CrystalReport && m_CrystalReport.GetReportFileName () = ""! ) {m_crystalreport.setDestination (1); /// Setting the output object is the printer m_crystalreport.printreport (); /// print}}

转载请注明原文地址:https://www.9cbs.com/read-129697.html

New Post(0)