📄 statpage.cpp
字号:
// StatPage.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "StatPage.h"
#include "StatSheet.h"
#include "demo2005Dlg.h"
#include "demoCommon.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CStatPage1, CPropertyPage)
IMPLEMENT_DYNCREATE(CStatPage2, CPropertyPage)
IMPLEMENT_DYNCREATE(CStatPage3, CPropertyPage)
IMPLEMENT_DYNCREATE(CStatPage4, CPropertyPage)
/////////////////////////////////////////////////////////////////////////////
// CStatPage1 property page
CStatPage1::CStatPage1() : CPropertyPage(CStatPage1::IDD)
{
//{{AFX_DATA_INIT(CStatPage1)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CStatPage1::~CStatPage1()
{
}
void CStatPage1::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStatPage1)
DDX_Control(pDX, IDC_STAT_TYPE, m_cmbStatType);
DDX_Control(pDX, IDC_STAT_FIELD, m_cmbStatField);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStatPage1, CPropertyPage)
//{{AFX_MSG_MAP(CStatPage1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStatPage2 property page
CStatPage2::CStatPage2() : CPropertyPage(CStatPage2::IDD)
{
//{{AFX_DATA_INIT(CStatPage2)
m_val = _T("");
m_fName = _T("");
m_op = _T("");
//}}AFX_DATA_INIT
}
CStatPage2::~CStatPage2()
{
}
void CStatPage2::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStatPage2)
DDX_Control(pDX, IDC_STAT_FIELD_VALUE, m_edVal);
DDX_Control(pDX, IDC_STAT_OPERATER, m_cmbOp);
DDX_Control(pDX, IDC_STAT_FIELD_NAME, m_cmbFName);
DDX_Text(pDX, IDC_STAT_FIELD_VALUE, m_val);
DDX_CBString(pDX, IDC_STAT_FIELD_NAME, m_fName);
DDX_CBString(pDX, IDC_STAT_OPERATER, m_op);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStatPage2, CPropertyPage)
//{{AFX_MSG_MAP(CStatPage2)
ON_BN_CLICKED(IDC_AND_BUTTON, OnAndButton)
ON_BN_CLICKED(IDC_OR_BUTTON, OnOrButton)
ON_CBN_SELCHANGE(IDC_STAT_FIELD_NAME, OnSelchangeStatFieldName)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStatPage3 property page
CStatPage3::CStatPage3() : CPropertyPage(CStatPage3::IDD)
{
//{{AFX_DATA_INIT(CStatPage3)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CStatPage3::~CStatPage3()
{
}
void CStatPage3::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStatPage3)
DDX_Control(pDX, IDC_STAT_SENTENCE, m_StatSentence);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStatPage3, CPropertyPage)
//{{AFX_MSG_MAP(CStatPage3)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStatPage4 property page
CStatPage4::CStatPage4() : CPropertyPage(CStatPage4::IDD)
{
//{{AFX_DATA_INIT(CStatPage4)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CStatPage4::~CStatPage4()
{
}
void CStatPage4::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStatPage4)
DDX_Control(pDX, IDC_STAT_VALUE, m_edStatVal);
DDX_Control(pDX, IDC_STAT_CAPTION, m_staStatCaption);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStatPage4, CPropertyPage)
//{{AFX_MSG_MAP(CStatPage4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// 修改各页的按钮
BOOL CStatPage1::OnSetActive()
{
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
pParent->SetWizardButtons( PSWIZB_NEXT );
return CPropertyPage::OnSetActive();
}
BOOL CStatPage2::OnSetActive()
{
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
pParent->SetWizardButtons( PSWIZB_BACK | PSWIZB_NEXT );
return CPropertyPage::OnSetActive();
}
BOOL CStatPage3::OnSetActive()
{
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
pParent->SetWizardButtons( PSWIZB_BACK | PSWIZB_NEXT );
return CPropertyPage::OnSetActive();
}
BOOL CStatPage4::OnSetActive()
{
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
pParent->SetWizardButtons( PSWIZB_FINISH );
return CPropertyPage::OnSetActive();
}
/******************************
** 对"选择字段"对话框的操作 **
******************************/
BOOL CStatPage1::OnInitDialog()
{
CPropertyPage::OnInitDialog();
// show fields-name in m_cmbStatField
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
CDemo2005Dlg *pDemo = (CDemo2005Dlg*)pParent->GetParent();
for(int i=0; i<pDemo->fcnt; i++)
m_cmbStatField.AddString((LPCTSTR)pDemo->pFieldDsc[i].fName);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
LRESULT CStatPage1::OnWizardNext()
{
CString sztmp;
int n;
statType st;
UpdateData(TRUE);
// Get fields of view
n = m_cmbStatField.GetCurSel();
st = (statType)m_cmbStatType.GetCurSel();
if( n<0 )// no fields
{
AfxMessageBox("Should select fields for statistics !", MB_OK|MB_ICONERROR);
return -1;
}
if( st<0 )// no fields
{
AfxMessageBox("Should select operation type for statistics !", MB_OK|MB_ICONERROR);
return -1;
}
// Get Parents
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
CDemo2005Dlg *pDemo = (CDemo2005Dlg*)pParent->GetParent();
// check if the type fit with the field
if( pDemo->pFieldDsc[n].fType>_DOUBLE && st!=_COUNT )
{
switch(pDemo->pFieldDsc[n].fType)
{
case _DATE: sztmp = "日期型数据"; break;
case _TIME: sztmp = "时间型数据"; break;
case _STRING: sztmp = "字符串型数据"; break;
default:
AfxMessageBox("错误的数据类型!", MB_OK|MB_ICONERROR);
return -1;
}
if( st==_SUM )
sztmp += "不能求总和!";
else // if( st==_AVG )
sztmp += "不能求平均数!";
AfxMessageBox(sztmp, MB_OK|MB_ICONERROR);
return -1;
}
// save statistics information
pParent->fIndex = n;
pParent->sType = st;
return CPropertyPage::OnWizardNext();
}
/******************************
** 对"选择条件"对话框的操作 **
******************************/
BOOL CStatPage2::OnInitDialog()
{
CPropertyPage::OnInitDialog();
// show fields-name in m_cmbFName
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
CDemo2005Dlg *pDemo = (CDemo2005Dlg*)pParent->GetParent();
for(int i=0; i<pDemo->fcnt; i++)
m_cmbFName.AddString((LPCTSTR)pDemo->pFieldDsc[i].fName);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//AND button
void CStatPage2::OnAndButton()
{
// save a AND instruction
if( !bSaved && SaveCondition() )
bSaved = TRUE; // current condition saved
}
//OR button
void CStatPage2::OnOrButton()
{
// add last AND instruction first
if( !bSaved && SaveCondition() )
{
// save index of OR in queryInst
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
pParent->qcond.orIndex[++pParent->qcond.ornumth] = pParent->qcond.icnt; // or index
// current condition saved
bSaved = TRUE;
}
}
LRESULT CStatPage2::OnWizardNext()
{
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
// add last AND instruction first
if( !bSaved )
{
if( !SaveCondition() )
return -1;
bSaved = TRUE; // current condition saved
}
// save index of OR in queryInst
if( pParent->qcond.orIndex[pParent->qcond.ornumth] != pParent->qcond.icnt )
pParent->qcond.orIndex[++pParent->qcond.ornumth] = pParent->qcond.icnt;
return CPropertyPage::OnWizardNext();
}
void CStatPage2::OnSelchangeStatFieldName()
{
bSaved = FALSE;
}
// save a where-condition
BOOL CStatPage2::SaveCondition()
{
// check
if( !CheckCondition() )
return FALSE;
int nSel = m_cmbFName.GetCurSel();
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
CDemo2005Dlg *pDemo = (CDemo2005Dlg*)pParent->GetParent();
//make sure the value you input is correct at lest in format
if( !CheckValue( m_val, pDemo->pFieldDsc[nSel].fType,
pDemo->pFieldDsc[nSel].fLen) )
return FALSE;
// save instruction
int icnt = pParent->qcond.icnt;
strcpy( pParent->qcond.queryInst[icnt].fName, (LPCTSTR)m_fName );
strcpy( pParent->qcond.queryInst[icnt].op, (LPCTSTR)m_op );
strcpy( pParent->qcond.queryInst[icnt].val, (LPCTSTR)m_val );
pParent->qcond.queryInst[icnt].fType = pDemo->pFieldDsc[nSel].fType;
pParent->qcond.queryInst[icnt].fLen = pDemo->pFieldDsc[nSel].fLen;
pParent->qcond.queryInst[icnt].offset = pDemo->pFieldDsc[nSel].offset;
// increment icnt;
pParent->qcond.icnt++;
// Reset Edits
ResetEdits();
return TRUE;
}
// check condition-input
BOOL CStatPage2::CheckCondition()
{
UpdateData(TRUE);
// if( m_fieldCombo.GetCurSel()==CB_ERR || m_opCombo.GetCurSel()==CB_ERR
if( m_fName.IsEmpty() || m_op.IsEmpty() || m_val.IsEmpty() )
{
AfxMessageBox("条件输入不完全",MB_OK|MB_ICONERROR);
return FALSE;
}
return TRUE;
}
// Reset Edits
void CStatPage2::ResetEdits()
{
m_cmbFName.SetCurSel(-1);
m_cmbFName.SetWindowText("");
m_cmbOp.SetCurSel(-1);
m_cmbOp.SetWindowText("");
m_edVal.SetWindowText("");
}
/******************************
** 对"执行统计询"对话框的操作 **
******************************/
// compile select-sentence
void CStatPage3::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Get parents
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
CDemo2005Dlg *pDemo = (CDemo2005Dlg*)pParent->GetParent();
// compile select-sentence
CString sent;
int i, j, n;
struct inst *pinst;
// statistics field
sent = "Select ";
switch(pParent->sType)
{
case _COUNT: sent += "COUNT("; break;
case _SUM: sent += "SUM("; break;
case _AVG: sent += "AVG("; break;
}
sent += pDemo->pFieldDsc[pParent->fIndex].fName;
sent += ")\r\n";
// from fileName
sent += "from ";
sent += pDemo->m_fileName;
sent += "\r\n";
//where condition
pinst = pParent->qcond.queryInst;
sent += "where ";
for( i=1, j=0; i<=pParent->qcond.ornumth; i++)
{
sent += "( ";
while( j<pParent->qcond.orIndex[i]-1 )
{
sent += pinst[j].fName;
sent += pinst[j].op;
sent += pinst[j++].val;
sent += " and ";
}
sent += pinst[j].fName;
sent += pinst[j].op;
sent += pinst[j++].val;
sent += " ) \r\n or ";
}
// show select-sentence in m_SelSentence
if( pParent->qcond.ornumth>=1 )
n = sent.GetLength()-9;
else
n = sent.GetLength()-6;
m_StatSentence.SetWindowText( (LPCTSTR)sent.Left(n) );
// Do not call CPropertyPage::OnPaint() for painting messages
}
/******************************
** 对"统计结果"对话框的操作 **
******************************/
BOOL CStatPage4::OnInitDialog()
{
CPropertyPage::OnInitDialog();
// execute statistics
ExecStat();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
// execute statistics
void CStatPage4::ExecStat()
{
BYTE *rbuf,*p;
int rlen, rcnt;
CString sztmp = "";
int nSum, nAvg;
double fSum, fAvg;
int ncnt;
// Get parents
CStatisticsSheet *pParent = (CStatisticsSheet*)GetParent();
CDemo2005Dlg *pDemo = (CDemo2005Dlg*)pParent->GetParent();
// show statistics caption
switch(pParent->sType)
{
case _COUNT: sztmp = "COUNT("; break;
case _SUM: sztmp = "SUM("; break;
case _AVG: sztmp = "AVG("; break;
}
sztmp += pDemo->pFieldDsc[pParent->fIndex].fName;
sztmp += ") :";
m_staStatCaption.SetWindowText((LPCTSTR)sztmp);
// local variable
rcnt = pDemo->m_fileHeader.rcnt;
rlen = pDemo->m_fileHeader.rlen;
rbuf = (BYTE*)malloc(rlen);
// init result-value
nSum = 0;
nAvg = 0;
fSum = 0.0;
fAvg = 0.0;
ncnt = 0;
// execute statistics
fseek( pDemo->m_fhd,
sizeof(pDemo->m_fileHeader)+pDemo->fcnt*sizeof(fieldDesc),
SEEK_SET );
for(int i=0; i<rcnt; i++)
{
// read a record
fread( rbuf, rlen, 1, pDemo->m_fhd);
// if the record meet with the condition
if( FindRecord(rbuf, pParent->qcond) )
{
switch(pParent->sType)
{
case _SUM:
case _AVG:
p = rbuf+pDemo->pFieldDsc[pParent->fIndex].offset;
switch( pDemo->pFieldDsc[pParent->fIndex].fType )
{
case _INTEGER: nSum += *(int*)p; break;
case _DOUBLE: fSum += *(double*)p; break;
}
case _COUNT:
ncnt++;
break;
}
}
}//out for end
// show result
switch(pParent->sType)
{
case _SUM:
switch( pDemo->pFieldDsc[pParent->fIndex].fType )
{
case _INTEGER: sztmp.Format("%d",nSum); break;
case _DOUBLE: sztmp.Format("%f",fSum); break;
}
break;
case _AVG:
switch( pDemo->pFieldDsc[pParent->fIndex].fType )
{
case _INTEGER: sztmp.Format("%d",nSum/ncnt); break;
case _DOUBLE: sztmp.Format("%f",fSum/ncnt); break;
}
break;
case _COUNT:
sztmp.Format("%d",ncnt);
break;
}
m_edStatVal.SetWindowText(sztmp);
// free variable
free(rbuf);
rbuf = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -