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

📄 vcg12.htm

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

**   FUNCTION: Calls ReportEase PLUS's report generator to create

**             a new report or edit an existing report.

**

******************************************************************************/

// LOCAL PROTOTYPES:

void InitDataField(void);

int FAR PASCAL _export UserFieldSelection(HWND hWnd,

    struct  StrField huge * field, int SortFieldNo);

int FAR PASCAL _export VerifyField(struct StrField huge * field,

    int SortFieldNo);

// END LOCALS

// Externs

extern  HWND    hDlgPrint;

extern  BOOL    bUserAbort;

extern  int  nCurrentSelection;

// Extern  REPORT   Report;

extern  WORD    wReportType;

// End externs

// Defines

#define MAX_FILES    1  /* One file, the database...    */

#define MAX_FIELDS  65  /* Possible fields              */

int     nReportNumberFields = {0};

// End defines

#pragma pack(1)

struct StrDataField

{

        char    name[35];       // Field name

        int     width;          // Field width

        int     type;           // Field type

        int     DecPlaces;      // Decimal places

        int     FieldId;

} DataField[MAX_FILES][MAX_FIELDS];

#pragma pack()

#pragma pack(1)

StrForm FormParam;

#pragma pack()

void    ReportSystem(HWND       hWndParent)

{

//      First, initialize the DataField structure.

//      Initialize the report's columns

        InitDataField();

//      Got report filename. Now set up for the form generator.

        FormParam.x = CW_USEDEFAULT;

        FormParam.y = CW_USEDEFAULT;

        FormParam.width = CW_USEDEFAULT;

        FormParam.height = CW_USEDEFAULT;

        FormParam.UserSelection = UserFieldSelection;

        FormParam.VerifyField = VerifyField;

        strcpy(FormParam.file, ReportStuff.szFileName);

        strcpy(FormParam.DataSetName, "HUH");

        FormParam.ShowMenu = TRUE;

        FormParam.ShowHorBar = TRUE;

        FormParam.ShowVerBar = TRUE;

        FormParam.hInst = hInst;

        FormParam.hPrevInst = NULL;

        FormParam.hParentWnd = hWndParent;

        FormParam.hFrWnd = 0;

        FormParam.style=WS_OVERLAPPEDWINDOW;  // Editor window style

        FormParam.FontTypeFace[0] = '\0';

        FormParam.EndForm = NULL;

        FormParam.open = FALSE;

        FormParam.modified = FALSE;

//      It's set up. Now call the form generator:

        form(&FormParam);

//      Set up stuff to return, like the report's filename:

        strcpy(ReportStuff.szFileName, FormParam.file);

        return;

}</FONT></PRE>

<P>Figure 12.8 shows the ReportEase report generator being called using the code shown next.

<BR>

<P><B><A HREF="12vcg08.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg08.gif">Figure 12.8. ReportEase's report generator in action.</A></B>

<BR>

<P>To incorporate ReportEase into your application, you must do the following. First, you must include an #include directive for the ReportEase include file, REP.H. This file contains the definitions that are needed to create the structures used by ReportEase. Next you must create the data structures that are used to hold information about the fields that will be available to the user when the report is being designed. The StrDataField structure is used for this purpose. It's configured as shown in this code fragment:

<BR>

<PRE>

<FONT COLOR="#000080">#define MAX_FILES    1   /* One file, the database... */

#define MAX_FIELDS  65   /* 65 possible fields */

struct StrDataField

{

    char       name[35];     // Field name

    int        width;        // Field width

    int        type;         // Field type

    int        DecPlaces;    // Decimal places

    int        FieldId;      // Identifier for each field

} DataField[MAX_FILES][MAX_FIELDS];</FONT></PRE>

<P>The DataField structure must contain certain information, but you can also include any additional information that might help your application process columns in the report. In my program, a field called FieldId has been added to hold an identifier for each field (otherwise, you would have to do string compares on the name field) to let a field be easily identified when the report is being processed.

<BR>

<P>The fields generally are self-explanatory, with the exception of the type field, which will contain values defined in REP.H. The valid values for the type field appear in Table 12.2.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 12.2. Values for the type field.</B></FONT></CENTER>

<BR>



<CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Value</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Description</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

TYPE_TEXT

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A text field. Text operations are allowed on this field.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

TYPE_NUM

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A fixed-point numeric field. Numeric operations are allowed on this field. The number of digits after the decimal point should be stored in the DecPlaces field. The user can alter the number of decimal places when the report form is generated.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

TYPE_DBL

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A double-precision floating-point numeric field. Numeric operations are allowed on this field. The number of digits after the decimal point should be stored in the DecPlaces field. The user can alter the number of decimal places when the report form is generated.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

TYPE_DATE

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A date field. Dates are stored in a long integer (32-bit) field in the format of either YYMMDD or YYYYMMDD.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

TYPE_LOGICAL

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A numeric field. Logical data is stored as either a long integer (32-bit) 1 or 0.</FONT>

</TABLE></CENTER><P>Next you must write a function to let the user select fields to place in the report. Here is the function prototype for this function:

<BR>

<PRE>

<FONT COLOR="#000080">int far PASCAL UserFieldSelection(HWND hWndParent,

               struct StrField,

    far * field, int SortFieldNumber);</FONT></PRE>

<P>This function can display a simple dialog box that contains a list box and an OK button. Figure 12.9 shows a typical field selection dialog box.

<BR>

<P><B><A HREF="12vcg09.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/12vcg09.gif">Figure 12.9. A field selection dialog box.</A></B>

<BR>

<P>Listing 12.16 shows the code necessary to manage the field selection dialog box shown in Figure 12.9. Much of this code was actually generated using ClassWizard.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 12.16. REPORTCO.CPP: Field selection dialog box code.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">// reportco.cpp : implementation file

//

#include &quot;stdafx.h&quot;

#include &quot;starae.h&quot;

#include &quot;reportco.h&quot;

#ifdef _DEBUG

#undef THIS_FILE

static char BASED_CODE THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CReportColumns dialog

CReportColumns::CReportColumns(CWnd* pParent /*=NULL*/)

    : CDialog(CReportColumns::IDD, pParent)

{

    //{{AFX_DATA_INIT(CReportColumns)

    m_ColumnName = -1;

    //}}AFX_DATA_INIT

}

void CReportColumns::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CReportColumns)

    DDX_Control(pDX, IDC_REPORT_COLUMN, m_ColumnControl);

    DDX_CBIndex(pDX, IDC_REPORT_COLUMN, m_ColumnName);

    //}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CReportColumns, CDialog)

    //{{AFX_MSG_MAP(CReportColumns)

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CReportColumns message handlers

// These defines are *also* in RepSys.CPP

//

#define MAX_FILES    1    /* One file, the database...    */

#define MAX_FIELDS  65    /* 65 possible fields           */

extern    int    nReportNumberFields;

// End defines

#pragma pack(1)

extern struct StrDataField

{

    char    name[35];       // Field name

    int        width;       // Field width

    int        type;        // Field type

    int        DecPlaces;   // Decimal places

    int        FieldId;     // Unique identifier for field

} DataField[MAX_FILES][MAX_FIELDS];

#pragma pack()

BOOL CReportColumns::OnInitDialog()

{

    CDialog::OnInitDialog();

    // TODO: Add extra initialization here

int        i = 0;

    while (DataField[0][i].name[0] &amp;&amp; i &lt;=  nReportNumberFields)

    {

        m_ColumnControl.AddString(DataField[0][i].name);

        ++i;

    }

    m_ColumnControl.SetCurSel(0);

    return TRUE;  // Return TRUE  unless you set the focus to a control

}</FONT></PRE>

<P>The code to create the dialog box is shown next. This code works with a minimum amount of programming because the m_ColumnName is bound to the dialog box combo box control and is automatically updated each time the user makes a selection from the combo box. If m_ColumnName is less than zero, the user didn't have a column selected.

<BR>

<P>Here's a typical UserFieldSelection() function:

<BR>

<PRE>

<FONT COLOR="#000080">int FAR PASCAL _export UserFieldSelection(

HWND hWnd,

struct    StrField huge * field,

int        SortFieldNo)

{

// Display dialog box of field names and allow user to select one

// Return (TRUE) if selection was successful or !TRUE if unsuccessful

// Allow the user to select from a dialog box...

int        CurFile = 0;

int        CurField = -1;

int        nReturn = IDCANCEL;

    // CWnd used to tell dialog to return to report generator, not star

    CWnd cwForm;

    cwForm.Attach(FormParam.hFrWnd);

    CReportColumns reportcolumn(&amp;cwForm);

    reportcolumn.m_ColumnName = 0;

    if (reportcolumn.DoModal() == IDOK &amp;&amp;

        reportcolumn.m_ColumnName &gt;= 0)

    {

        CurField = reportcolumn.m_ColumnName;

        nReturn = IDOK;

    }

    cwForm.Detach();  // So we don't blow up in destructor

    if (nReturn != IDCANCEL)

    {//    User selected a column to use.

     //    Now save it...

        lstrcpy(field-&gt;name, DataField[

⌨️ 快捷键说明

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