www.pudn.com > PressMonitor_q.zip > ReportFormView.cpp
// ReportFormView.cpp : implementation file
//
#include "stdafx.h"
#include "monitor.h"
#include "ReportFormView.h"
#include "MonitorDoc.h"
#include "DlgSelectClass.h"
#include "DlgMenuClassStat.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ROWS_PER_PAGE 40
static CString StrItemStatic[]={"序号","代 号","轴 号","熔 炼 号","始锻温度","终锻温度","记 事"};
int col_width[7]={48,96,96,96,96,96,0 };
/////////////////////////////////////////////////////////////////////////////
// CReportFormView
IMPLEMENT_DYNCREATE(CReportFormView, CFormView)
CReportFormView::CReportFormView()
: CFormView(CReportFormView::IDD)
{
//{{AFX_DATA_INIT(CReportFormView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_pGridCtrl = NULL;
m_nRowHighView=30;//17;
m_nRowNumView=14;//26;
m_nRowNumDis=0;
m_nFontTitle=20;
m_nFontClass=15;
m_bHaveInitForm=FALSE;
m_pQueryDef=NULL;
m_pQueryDatabase=NULL;
m_strQueryMonth="" ;
m_strSQLselsectToFrom=_T("SELECT [序号],[代号],[轴号],[熔炼号],[始锻温度],[终锻温度],[记事] FROM ") ;
}
CReportFormView::~CReportFormView()
{
SaveReportForm();
if (m_pGridCtrl) {
delete m_pGridCtrl;
}
CloseQueryDatabase();
}
void CReportFormView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReportFormView)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CReportFormView, CFormView)
//{{AFX_MSG_MAP(CReportFormView)
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_MENU_CLASS_OUTPUT, OnMenuClassOutput)
ON_WM_PAINT()
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
//}}AFX_MSG_MAP
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReportFormView diagnostics
#ifdef _DEBUG
void CReportFormView::AssertValid() const
{
CFormView::AssertValid();
}
void CReportFormView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CReportFormView message handlers
void CReportFormView::OnDraw(CDC* pDC)
{
// TODO: Add your specialized code here and/or call the base class
}
void CReportFormView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CMonitorDoc *pDoc = (CMonitorDoc*) GetDocument();
pDoc->m_bUpdateView=false;
if ( (m_pGridCtrl == NULL) )
{
m_pGridCtrl=new CGridCtrl;
if (!m_pGridCtrl) return;
CRect rect;
GetClientRect(rect);
m_pGridCtrl->Create(rect, this, 100);
// fill it up with stuff
m_pGridCtrl->SetEditable(TRUE);
m_pGridCtrl->EnableDragAndDrop(TRUE);
InitialReportForm();
}
}
void CReportFormView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (m_pGridCtrl->GetSafeHwnd())
{
//CRect rect;
//GetClientRect(rect);
GetClientRect(m_rectForm);
m_nRowHighView=(int)( (m_rectForm.Height()-37)/(m_nRowNumView+2) ) ;
m_nRowNumDis=(m_rectForm.Height()-37)%(m_nRowNumView+2);
// m_nFontClass=m_nRowHighView;
m_rectForm.DeflateRect(20,30,20,5+2*m_nRowHighView);
m_pGridCtrl->MoveWindow(m_rectForm);
}
}
BOOL CReportFormView::SaveReportForm()
{
if (m_pGridCtrl==NULL) {
AfxMessageBox(" 无法保存数据库") ;
return FALSE;
}
if (m_pGridCtrl->GetIfQueryStatus()) {
// AfxMessageBox("处在查询状态不能进行存盘!");
return FALSE;
}
CMonitorDoc *pDoc = (CMonitorDoc*) GetDocument();
COleDateTime time=COleDateTime::GetCurrentTime();
if( m_pGridCtrl->GetModifiedForm() )
{
if (!pDoc->m_bFormOpen) {
AfxMessageBox("报表数据库没有打开,无法添加记录");
return FALSE;
}
//修改记录在数据库中以前的版本
if ( (pDoc->m_nCurrentFormNum!=0)&&(pDoc->m_FormRecordSet.GetRecordCount()!=0) ) {
pDoc->m_FormRecordSet.MoveLast();
for(int i=1; (im_nCurrentFormNum)&&(!pDoc->m_FormRecordSet.IsBOF()) ;i++ )
{
pDoc->m_FormRecordSet.MovePrev();
}
for(i=1; (i<=pDoc->m_nCurrentFormNum)&&(!pDoc->m_FormRecordSet.IsEOF()); i++ )
{
pDoc->m_FormRecordSet.Edit();
SetDatabaseRow(i,time);
pDoc->m_FormRecordSet.MoveNext();
}
}
//添加新的版本
if ( pDoc->m_FormRecordSet.GetRecordCount()!=0 ) {
pDoc->m_FormRecordSet.MoveLast();
}
int myCount=m_pGridCtrl->GetFormRealCout();
for(int j=pDoc->m_nCurrentFormNum+1;j<=myCount;j++)
{
pDoc->m_FormRecordSet.SetBookmark( pDoc->m_FormRecordSet.GetLastModifiedBookmark( ));
pDoc->m_FormRecordSet.AddNew();
SetDatabaseRow(j,time);
pDoc->m_FormRecordSet.MoveLast();
}
pDoc->m_nCurrentFormNum=myCount ;
}
return TRUE;
}
void CReportFormView::SetDatabaseRow(int whichRow,COleDateTime time)
{
CMonitorDoc *pDoc = (CMonitorDoc*) GetDocument();
CString strSeral;
CString strMyTimeYear,strMyTimeMonth,strMyTimeDay,strMyTime;
strMyTimeYear.Format("%d",time.GetYear());
strMyTimeMonth.Format("%d",time.GetMonth()) ;
strMyTimeDay.Format("%d",time.GetDay());
strMyTime=strMyTimeYear+strMyTimeMonth+strMyTimeDay ;
GRID_ROW *grid_row;
grid_row=m_pGridCtrl->m_RowData.GetAt(whichRow);
CGridCell *grid_cell;
strSeral.Format("%d",pDoc->m_nCurrentClass);
try{
pDoc->m_FormRecordSet.SetFieldValue(0,(LPCSTR)strSeral);
}
catch(CDaoException *e)
{
AfxMessageBox("数据库添加中错误:"+e->m_pErrorInfo->m_strDescription );
e->Delete();
}
for(int kk=0;kk<7;kk++)
{
grid_cell=grid_row->GetAt(kk);
if( grid_cell->szText.GetLength()==0 )
strSeral=" ";
else
strSeral=grid_cell->szText ;
try
{
pDoc->m_FormRecordSet.SetFieldValue(kk+1,(LPCSTR)strSeral);
}
catch(CDaoException *e)
{
AfxMessageBox("数据库添加中错误:"+e->m_pErrorInfo->m_strDescription );
e->Delete();
}
}
try{
pDoc->m_FormRecordSet.SetFieldValue(8,time);
}
catch(CDaoException *e)
{
AfxMessageBox("数据库添加中错误:"+e->m_pErrorInfo->m_strDescription );
e->Delete();
}
try{
pDoc->m_FormRecordSet.SetFieldValue(9,(LPCSTR)strMyTime);
}
catch(CDaoException *e)
{
AfxMessageBox("数据库添加中错误:"+e->m_pErrorInfo->m_strDescription );
e->Delete();
}
try
{
pDoc->m_FormRecordSet.Update();
}
catch (CDaoException *e)
{
AfxMessageBox("数据库添加中错误:"+e->m_pErrorInfo->m_strDescription );
e->Delete();
}
}
void CReportFormView::InputOrInitData()
{
CMonitorDoc *pDoc = (CMonitorDoc*) GetDocument();
col_width[6]=m_rectForm.Width()-col_width[0]-col_width[1]-col_width[2]-col_width[3]
-col_width[4]-col_width[5]-20;
///////////////////////////////////////// Initialize
GV_ITEM Item;
for (int row = 0; row < m_pGridCtrl->GetRowCount(); row++)
{
Item.mask = GVIF_TEXT|GVIF_FORMAT;
Item.row = row;
Item.col = 0;
Item.nFormat = DT_CENTER|DT_WORDBREAK|DT_VCENTER;
if ( row >0 )
{
Item.szText.Format(_T("%d"),row);
}
if (rowSetRowHeight(row,m_nRowHighView+1);
}
else
m_pGridCtrl->SetRowHeight(row,m_nRowHighView);
m_pGridCtrl->SetItem(&Item);
}
for (int col = 0; col < m_pGridCtrl->GetColumnCount(); col++)
{
Item.mask = GVIF_TEXT|GVIF_FORMAT;
Item.row = 0;
Item.col = col;
Item.nFormat = DT_CENTER|DT_WORDBREAK|DT_VCENTER;
Item.szText=StrItemStatic[col];
m_pGridCtrl->SetColumnWidth(col,col_width[col]);
m_pGridCtrl->SetItem(&Item);
}
///////////////////////////////////////////// Input data
m_pGridCtrl->SetRealFormNum(pDoc->m_nCurrentFormNum) ;
COleVariant VariantSet;
// COleDateTime dataTime;
CString strUnit;
int docCurrentFormNum=pDoc->m_nCurrentFormNum ;
if ( (pDoc->m_FormRecordSet.GetRecordCount()==0)||(docCurrentFormNum==0) ) {
return;
}
pDoc->m_FormRecordSet.MoveLast();
for(int i=docCurrentFormNum; (!pDoc->m_FormRecordSet.IsBOF()) && (i>0) ;i-- )
{
for(int kk=1;kk<=7;kk++)
{
VariantSet=pDoc->m_FormRecordSet.GetFieldValue(kk);
strUnit.Format("%s",VariantSet.bstrVal);
m_pGridCtrl->SetItemText(i,kk-1,strUnit);
}
pDoc->m_FormRecordSet.MovePrev();
}
if ( docCurrentFormNum<(m_nRowNumView-1) )
{
CString strTemp;
pDoc->m_FormRecordSet.MoveLast();
VariantSet=pDoc->m_FormRecordSet.GetFieldValue(1);
strTemp.Format("%s",VariantSet.bstrVal);
int numTemp=m_pGridCtrl->StringToInt(strTemp);
numTemp++;
for(int mm=docCurrentFormNum+1; mmSetItemText(mm,0,strTemp);
}
}
//////////////////////////////////////////
}
void CReportFormView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// CFormView::OnLButtonDown(nFlags, point);
CMonitorDoc *pDoc=(CMonitorDoc*)GetDocument();
CRect rectClass;
rectClass.SetRect( m_rectForm.Width()-160,11,m_rectForm.right-30,11+m_nFontClass);
if ( rectClass.PtInRect(point) )
{
CDlgSelectClass dlg;
if (dlg.DoModal()==IDOK) {
BOOL bMY=m_pGridCtrl->GetIfQueryStatus() ;
if ( m_pGridCtrl->GetIfQueryStatus() ) {
FromQueryStatusToProducingStatus(); //将查询方式改为生产方式!
}
if ( dlg.m_nClassType !=pDoc->m_nCurrentClass ) {
SaveReportForm();
m_pGridCtrl->DeleteAllItems();
InitialReportForm();
pDoc->m_nCurrentFormNum=0;
m_pGridCtrl->SetRealFormNum(0);
InputOrInitData();
pDoc->m_nCurrentClass=dlg.m_nClassType ;
CTime time=CTime::GetCurrentTime();
m_strClassAndTime.Format("%d年%d月%d日%d班",time.GetYear(),time.GetMonth(),time.GetDay(),pDoc->m_nCurrentClass );
Invalidate();
}
else
return ;
}
}
}
void CReportFormView::InitialReportForm()
{
CMonitorDoc *pDoc = (CMonitorDoc*) GetDocument();
int setRowNum=max( pDoc->m_nCurrentFormNum+1 , m_nRowNumView );
CTime time;
time=time.GetCurrentTime();
m_strTitleDisplay="锻 轴 中 间 检 查 记 录" ;
m_strClassAndTime.Format("%d年%d月%d日%d班",time.GetYear(),time.GetMonth(),time.GetDay(),pDoc->m_nCurrentClass );
try {
m_pGridCtrl->SetFixedRowCount(1);
m_pGridCtrl->SetFixedColumnCount(0);
m_pGridCtrl->SetRowCount(setRowNum);
m_pGridCtrl->SetColumnCount(7);
}
catch (CMemoryException* e)
{
e->ReportError();
e->Delete();
return;
}
}
//首先判断查询表是否存在,若存在先删除,创建一个新的查询表,
BOOL CReportFormView::CreateFormQuerySys(CDaoDatabase * pFormDB)
{
if (!pFormDB) {
AfxMessageBox("数据库不存在,不能查询");
return FALSE;
}
ClosePreQuerySys(); //关闭查询表
m_pQueryDef=new CDaoQueryDef(pFormDB);
try
{
m_pQueryDef->Open( LPCTSTR("MYquery") ) ;
}
catch (CDaoException *e) { //
try{
m_pQueryDef->Create("MYquery");
}
catch (CDaoException *e) {
AfxMessageBox("数据库创建查询表时错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
e->Delete();
return TRUE;
}
if (m_pQueryDef->IsOpen()) {
m_pQueryDef->Close();
}
pFormDB->DeleteQueryDef( LPCTSTR("MYquery") ) ;
try{
m_pQueryDef->Create("MYquery");
}
catch (CDaoException *e) {
AfxMessageBox("数据库创建查询表时错误:"+e->m_pErrorInfo->m_strDescription);
return FALSE;
}
return TRUE;
}
void CReportFormView::ClosePreQuerySys() //关闭查询表
{
if (m_FormQueryRecordSet.IsOpen()) {
m_FormQueryRecordSet.Close();
}
if (m_pQueryDef) {
if (m_pQueryDef->IsOpen()) {
m_pQueryDef->Close();
}
delete m_pQueryDef;
}
}
void CReportFormView::OnMenuClassOutput()
{
// TODO: Add your command handler code here
CMonitorDoc *pDoc=(CMonitorDoc*)GetDocument();
CTime currentTime=CTime::GetCurrentTime();
CDlgMenuClassStat dlg;
if (dlg.DoModal()==IDOK) {
SaveReportForm();
//////////////////////////////首先确立SQL语句中的条件部分(m_strSQLwhereContion),即WHERE及其以后的语句(若存在)
if ( (dlg.m_bClassSelect)||(dlg.m_bAxisType)||(dlg.m_bDataDay)||(dlg.m_bAxisType2) )
{
m_strSQLwhereContion=" WHERE ";
if (dlg.m_bClassSelect) {
CString strTemp;
strTemp.Format("%d", dlg.m_nClassSelect);
m_strSQLwhereContion=m_strSQLwhereContion+" [班]='"+strTemp+"' " +" AND";//
}
if (dlg.m_bAxisType2) {
m_strSQLwhereContion=m_strSQLwhereContion+" [轴号]='"+dlg.m_strAxisType2+"' "+" AND";//
}
if (dlg.m_bAxisType) {
m_strSQLwhereContion=m_strSQLwhereContion+" [代号]='"+dlg.m_strAxisType+"' "+" AND";//
}
TRACE(m_strSQLwhereContion+"\n");
if (dlg.m_bDataDay) {
CString strMyTimeYear,strMyTimeMonth,strMyTimeDay,strMyTime;
strMyTimeYear.Format("%d",dlg.m_timeDay.GetYear());
strMyTimeMonth.Format("%d",dlg.m_timeDay.GetMonth()) ;
strMyTimeDay.Format("%d",dlg.m_timeDay.GetDay());
strMyTime=strMyTimeYear+strMyTimeMonth+strMyTimeDay ;
m_strSQLwhereContion=m_strSQLwhereContion+" [我的日期]='"+strMyTime+"'" +" AND" ;
}
TRACE(m_strSQLwhereContion+"\n");
m_strSQLwhereContion=m_strSQLwhereContion.Left(m_strSQLwhereContion.GetLength()-4) ;//在SQL的WHERE语句中去掉最后一个AND
}
else
m_strSQLwhereContion=" " ;
TRACE(m_strSQLwhereContion+"\n");
////////////////////////////////////////////////选择数据库和要查询的基本表--建立SQL语句中基本表部分
if (dlg.m_bDataDay)
{
// 判断查询的数据库是否存在,并建立查询表与数据库的连接。
if ( !CreateDatabaseFormQuery(dlg.m_timeDay.GetYear()) ) {
return ;
}
//判断要查询的数据库中的基本表是否存在
if ( !IsExistBasicForm(dlg.m_timeDay.GetYear(),dlg.m_timeDay.GetMonth()) ) {
return;
}
}
else if ( dlg.m_bDataMonth ) {
// 判断查询的数据库是否存在,并建立查询表与数据库的连接。
if ( !CreateDatabaseFormQuery(dlg.m_timeMonth.GetYear()) ) {
return ;
}
//判断要查询的数据库中的基本表是否存在
if ( !IsExistBasicForm(dlg.m_timeMonth.GetYear(),dlg.m_timeMonth.GetMonth()) ) {
return;
}
}
else if ( dlg.m_bDataYear ) {
// 判断查询的数据库是否存在,并建立查询表与数据库的连接。
if ( !CreateDatabaseFormQuery( dlg.m_nTimeYear ) ) {
return ;
}
//判断要查询的数据库中的基本表是否存在
if ( !IsExistBasicForm(dlg.m_nTimeYear,13) ) {
return;
}
QueryAloneYear(dlg.m_nTimeYear);
return;
}
else
{
CreateFormQuerySys( pDoc->m_pFormDB );
CString strMonth;
strMonth.Format(" %d ",currentTime.GetMonth());
m_strQueryMonth=strMonth ;
}
/////////////////////////设置SQL进行查询,并将查询表记录到数据库!
CString strSQL;
strSQL=m_strSQLselsectToFrom+m_strQueryMonth+m_strSQLwhereContion;
TRACE(strSQL+"\n");
try
{
m_pQueryDef->SetSQL(LPCTSTR(strSQL));
}
catch (CDaoException *e) {
AfxMessageBox("SQL错误:"+e->m_pErrorInfo->m_strDescription);
return ;
}
m_pQueryDef->Append();
//try
// {
// m_pQueryDef->Execute();
// }
//
// catch (CDaoException *e) {
// AfxMessageBox("查询表执行查询时错误:"+e->m_pErrorInfo->m_strDescription);
// return ;
// }
//////////////////////////建立查询表的记录集,从而方便对查询表的各种操作!
try{
m_FormQueryRecordSet.Open(m_pQueryDef,dbOpenDynaset );
}
catch (CDaoException *e) {
AfxMessageBox("查询表与记录集连接时错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return ;
}
if (m_FormQueryRecordSet.GetRecordCount()!=0) {
m_FormQueryRecordSet.MoveLast();
CString strNum;
strNum.Format("%d",m_FormQueryRecordSet.GetRecordCount()) ;
if ( AfxMessageBox("查询到满足条件的记录为:"+strNum+"个\n\n要显示查询结果吗?" ,MB_YESNO )==IDYES ) {
m_strTitleDisplay="锻 轴 中 间 检 查 记 录" ;
// if (dlg.m_bAxisType2) {
// m_strTitleDisplay=m_strTitleDisplay+"轴号"+dlg.m_strAxisType2+"轴" ;
// }
// if (dlg.m_bAxisType) {
// m_strTitleDisplay=m_strTitleDisplay+"代号"+dlg.m_strAxisType+"轴" ;
// }
// if ( (!dlg.m_bAxisType2)&&(!dlg.m_bAxisType) ) {
// m_strTitleDisplay=m_strTitleDisplay+"所有锻件" ;
// }
// m_strTitleDisplay=m_strTitleDisplay+"的锻造记录" ;
if (dlg.m_bDataDay) {
m_strClassAndTime.Format("%d年%d月%d日",
dlg.m_timeDay.GetYear(),dlg.m_timeDay.GetMonth(),dlg.m_timeDay.GetDay()) ;
}
else if (dlg.m_bDataMonth) {
m_strClassAndTime.Format("%d年%d月",dlg.m_timeMonth.GetYear(),dlg.m_timeMonth.GetMonth() );
}
else if (dlg.m_bDataYear) {
m_strClassAndTime.Format("%d年",dlg.m_nTimeYear);
}
else{
m_strClassAndTime.Format("%d年%d月%d日",
currentTime.GetYear(),currentTime.GetMonth(),currentTime.GetDay()) ;
}
CString strClass;
if (dlg.m_bClassSelect) {
strClass.Format("%d",dlg.m_nClassSelect);
}
else
strClass="所有" ;
m_strClassAndTime=m_strClassAndTime+strClass+"班" ;
DisplayQuery();
m_pGridCtrl->SetQueryStatus(TRUE) ;//设定表格处于查询状态,不能对表格进行任何修改!
}
}
else
AfxMessageBox("记录为:0个");
}
}
///根据查询系统的要求找到相应的数据库,看是否能打开;并在数据库中建立一个查询表(查询表m_pQueryDatabase)!
BOOL CReportFormView::CreateDatabaseFormQuery(int queryTime)
{
CMonitorDoc *pDoc=(CMonitorDoc*)GetDocument();
CTime currentTime=CTime::GetCurrentTime();
CString strYear;
if ( queryTime != currentTime.GetYear() ) { //要查询非今年的数据信息
strYear.Format("db%d.mdb",queryTime ) ;
//判断是否存在数据库
if ( IsExistDatabase(strYear) ) {
//存在就在数据库中建立查询表
if ( !CreateFormQuerySys(m_pQueryDatabase) ) {
return FALSE;
}
}
else
{
return FALSE;
}
}
else //要查询今年的数据信息
{
if ( !CreateFormQuerySys( pDoc->m_pFormDB ) ) {
return FALSE;
}
}
return TRUE ;
}
//对于非当前(年)数据库,判断是否存在,若存在则打开(由m_pQueryDatabase负责).
BOOL CReportFormView::IsExistDatabase(CString databaseName)
{
CMonitorDoc *pDoc=(CMonitorDoc*)GetDocument();
CString strFile;
strFile=pDoc->GetDatabaseSaveDirectory()+databaseName ;
CFile dbFile;
if (dbFile.Open(strFile,CFile::modeRead)) {
dbFile.Close();
if (m_pQueryDatabase==NULL) {
m_pQueryDatabase=new CDaoDatabase ;
}
////////////////////////////////////////////Open Database
try
{
m_pQueryDatabase->Open( (LPCTSTR)strFile );
}
catch (CDaoException *e) {
AfxMessageBox("查询的数据库不能打开:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return FALSE;
}
}
else
{
AfxMessageBox("要查询的数据库不存在") ;
return FALSE ;
}
return TRUE ;
}
//判断基本表是否存在,若存在就设定相应的查询范围.if whichmonth=13 就是查询全年的
BOOL CReportFormView::IsExistBasicForm(int queryYear,int whichmonth)
{
CDaoTableDef * pTempTable=NULL;
CMonitorDoc *pDoc=(CMonitorDoc*)GetDocument();
CTime currentTime=CTime::GetCurrentTime();
if ( currentTime.GetYear()==queryYear ) {
pTempTable=new CDaoTableDef( pDoc->m_pFormDB ) ;
}
else
{
if (m_pQueryDatabase) {
pTempTable=new CDaoTableDef( m_pQueryDatabase ) ;
}
else
{
AfxMessageBox("查询数据库错误1");
return FALSE;
}
}
CString strMonth;
if ( (whichmonth<13)&&(whichmonth>0) ) {
strMonth.Format("%d",whichmonth);
try{
pTempTable->Open( LPCTSTR(strMonth) ) ;
}
catch (CDaoException *e) {
AfxMessageBox("要查询的数据库中没有"+strMonth+"月份的记录");
e->Delete();
return FALSE ;
}
if (pTempTable) {
if (pTempTable->IsOpen()) {
pTempTable->Close();
}
delete pTempTable;
}
m_strQueryMonth=strMonth;
return TRUE ;
}
else if ( whichmonth==13 ) //要查询一年的记录
{
m_strArrayQueryMonth.RemoveAll();
CString strNoMonth;
for(int i=1;i<=12;i++)
{
strMonth.Format("%d",i);
try{
pTempTable->Open( LPCTSTR(strMonth) ) ;
}
catch (CDaoException *e) {
strNoMonth=strNoMonth+strMonth+" , ";
e->Delete();
continue ;
}
if (pTempTable->IsOpen()) {
m_strArrayQueryMonth.Add(strMonth);
pTempTable->Close();
}
}
int nStrLen=m_strArrayQueryMonth.GetSize();
if (nStrLen==0) {
AfxMessageBox("数据库中没有任何月份的记录");
return FALSE;
}
if (pTempTable) {
if (pTempTable->IsOpen()) {
pTempTable->Close();
}
delete pTempTable;
}
strNoMonth=strNoMonth.Left(strNoMonth.GetLength()-2);
AfxMessageBox("注意: 数据库中没有\n"+strNoMonth+"\n月份的记录");
return TRUE;
}
else{
AfxMessageBox("输入要查询的月份错误");
return FALSE ;
}
return FALSE;
}
void CReportFormView::CloseQueryDatabase()
{
ClosePreQuerySys(); //关闭查询表
if (m_pQueryDatabase) {
if (m_pQueryDatabase->IsOpen()) {
m_pQueryDatabase->Close();
}
delete m_pQueryDatabase;
}
}
void CReportFormView::QueryAloneYear(int nWhichYear)
{
//AfxMessageBox("等待定义语句");
// return;
CMonitorDoc *pDoc=(CMonitorDoc*)GetDocument();
CTime time=CTime::GetCurrentTime();
int recordCount=0;
int currentYear=time.GetYear() ;
CDaoDatabase * pFormDB ;
if (nWhichYear==currentYear) {
pFormDB=pDoc->m_pFormDB ;
}
else
{
pFormDB=m_pQueryDatabase ;
}
for(int i=0;iSetSQL(LPCTSTR(strSQL));
}
catch (CDaoException *e) {
AfxMessageBox("SQL错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return ;
}
m_pQueryDef->Append();
//////////////////////////建立查询表的记录集,从而方便对查询表的各种操作!
try{
m_FormQueryRecordSet.Open(m_pQueryDef,dbOpenDynaset );
}
catch (CDaoException *e) {
AfxMessageBox("查询表与记录集连接时错误:"+e->m_pErrorInfo->m_strDescription);
e->Delete();
return ;
}
if (m_FormQueryRecordSet.GetRecordCount()!=0) {
m_FormQueryRecordSet.MoveLast();
}
recordCount+=m_FormQueryRecordSet.GetRecordCount() ;
}
CString strCount;
strCount.Format("%d",recordCount) ;
CString strYear ;
strYear.Format("%d",nWhichYear);
AfxMessageBox("对于"+strYear+"年记录查询中,满足条件的记录为:"+strCount+"个");
}
void CReportFormView::DisplayQuery()
{
//AfxMessageBox("等待定义");
if (m_FormQueryRecordSet.GetRecordCount()==0) {
return ;
}
m_FormQueryRecordSet.MoveLast();
int recordCount=m_FormQueryRecordSet.GetRecordCount();
m_pGridCtrl->SetRowCount( recordCount+1 ) ;
COleVariant VariantSet;
CString strUnit;
m_FormQueryRecordSet.MoveFirst() ;
for(int i=1;i<=recordCount;i++)
{
for(int j=0;jGetColumnCount();j++)
{
VariantSet=m_FormQueryRecordSet.GetFieldValue(j);
strUnit.Format("%s",VariantSet.bstrVal);
m_pGridCtrl->SetItemText(i,j,strUnit);
}
if (m_FormQueryRecordSet.IsEOF()) {
break ;
}
m_FormQueryRecordSet.MoveNext() ;
}
Invalidate();
}
void CReportFormView::FromQueryStatusToProducingStatus()
{
m_pGridCtrl->DeleteAllItems();
InitialReportForm();
InputOrInitData();
m_pGridCtrl->SetQueryStatus(FALSE) ;
Invalidate();
}
void CReportFormView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CFormView::OnPaint() for painting messages
CFont fontUseTitle,fontUseTime, *pOldFont;
fontUseTitle.CreateFont(m_nFontTitle,0,0,0,400,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "宋体") ;
fontUseTime.CreateFont(m_nFontClass,0,0,0,400,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "宋体") ;
int preBackMode=dc.SetBkMode(TRANSPARENT);
pOldFont=dc.SelectObject(&fontUseTitle);
dc.TextOut(m_rectForm.Width()/2-60,5,m_strTitleDisplay);
dc.SelectObject(&fontUseTime);
dc.TextOut(m_rectForm.Width()-160,11,m_strClassAndTime);
dc.SelectObject(pOldFont);
fontUseTitle.DeleteObject();
dc.SetBkMode(preBackMode);
//////////////////////////////////表格输出///////////////////////////////
if(!m_bHaveInitForm)
{
InputOrInitData();
m_bHaveInitForm=TRUE;
}
m_pGridCtrl->ResetScrollBars();
}
void CReportFormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CMonitorDoc *pDoc=(CMonitorDoc*)GetDocument();
if (m_pGridCtrl==NULL) {
return ;
}
pDC->SetMapMode(MM_HIMETRIC) ;
CRect rectTotal;
CRect rectTitle,rectClass,rectChar,rectPageNum;
rectTotal=pInfo->m_rectDraw;
pDC->DPtoLP(rectTotal);
int leftVerge=rectTotal.Width()/15; //左空留边界
int rightVerge=rectTotal.Width()/15;//右边空留边界
int topVerge=rectTotal.Height()/20; //负数
int bottomVerge=rectTotal.Height()/30; //负数
rectTitle.left=rectTotal.left;
rectTitle.right=rectTotal.right;
rectTitle.top=rectTotal.top+topVerge;
rectTitle.bottom=rectTitle.top+rectTotal.Height()/32; //title high
TRACE("rectTitle的高度为:%d\n",rectTitle.Height());
rectClass.left=rectTotal.left+leftVerge;
rectClass.right=rectTotal.right-rightVerge;
rectClass.top=rectTitle.bottom;
rectClass.bottom=rectClass.top+rectTotal.Height()/(ROWS_PER_PAGE+4); //class high
TRACE("rectClass的高度为:%d\n",rectClass.Height());
rectChar.left=rectTotal.left+leftVerge;
rectChar.right=rectTotal.right-rightVerge;
rectChar.top=rectClass.bottom;
int charHigh=(rectChar.top-rectTotal.bottom+bottomVerge)/(ROWS_PER_PAGE+4) ;
rectChar.bottom=rectTotal.bottom+bottomVerge+charHigh;
TRACE("rectChar的高度为:%d\n",rectChar.Height());
double scale=0.65 ; //备注的比例为0.4
int col_width[8];
col_width[0]=0;
col_width[1]=(int)(rectChar.Width()*scale*4/36) ;
col_width[2]=col_width[1]+(int)(rectChar.Width()*scale*8/36) ;
col_width[3]=col_width[2]+(int)(rectChar.Width()*scale*8/36) ;
col_width[4]=col_width[3]+(int)(rectChar.Width()*scale*8/36);
col_width[5]=col_width[4]+(int)(rectChar.Width()*scale*8/36) ;
col_width[6]=col_width[5]+(int)(rectChar.Width()*scale*8/36) ;
col_width[7]=rectChar.Width();
int fontTitleHigh=rectTitle.Height()*4/5;
int fontCharHigh=charHigh*3/4;
CFont fontUseTitle,fontUseChar, *pOldFont;
fontUseTitle.CreateFont(-1*fontTitleHigh,0,0,0,400,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "宋体") ;
fontUseChar.CreateFont(-1*fontCharHigh,0,0,0,400,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "宋体") ;
int preBackMode=pDC->SetBkMode(TRANSPARENT);
pOldFont=pDC->SelectObject(&fontUseTitle);
//////////////////////////打印标题及项目
pDC->DrawText( m_strTitleDisplay,&rectTitle,DT_CENTER|DT_VCENTER|DT_SINGLELINE ) ;
pDC->SelectObject(&fontUseChar) ;
pDC->DrawText( m_strClassAndTime,&rectClass,DT_CENTER |DT_VCENTER|DT_SINGLELINE ) ;
pDC->DrawText("编号:Q/QCB01200-019-2002",&rectClass,DT_RIGHT|DT_VCENTER|DT_SINGLELINE) ;
CString strLine[7]={"序号","代 号","轴 号","熔 炼 号","始锻温度","终锻温度","记 事"};
pDC->MoveTo(rectChar.left,rectChar.top);
pDC->LineTo(rectChar.right,rectChar.top);
CRect rectCell;
for(int i=0;i<7;i++)
{
pDC->MoveTo(rectChar.left+col_width[i],rectChar.top);
pDC->LineTo(rectChar.left+col_width[i],rectChar.top-2*charHigh);
rectCell.SetRect(rectChar.left+col_width[i],rectChar.top,
rectChar.left+col_width[i+1],rectChar.top-2*charHigh);
pDC->DrawText( strLine[i],&rectCell,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
pDC->MoveTo(rectChar.left+col_width[7],rectChar.top);
pDC->LineTo(rectChar.left+col_width[7],rectChar.top-2*charHigh);
pDC->MoveTo(rectChar.left,rectChar.top-2*charHigh);
pDC->LineTo(rectChar.right,rectChar.top-2*charHigh);
//如果打印查询结果
int k,k1;
if (m_pGridCtrl->GetIfQueryStatus())
{
if (m_FormQueryRecordSet.GetRecordCount()==0) {
return;
}
long lRows;
if (pInfo->m_nCurPage==1) {
lRows=0;
}
else
lRows=(pInfo->m_nCurPage-1)*ROWS_PER_PAGE; //要移动的相对位置!
m_FormQueryRecordSet.MoveFirst();
if (lRows)
{
m_FormQueryRecordSet.Move(lRows) ;
}
COleVariant VariantSet;
CString strUnit;
for(k=2; (kMoveTo(rectChar.left+col_width[m],rectChar.top-k*charHigh);
pDC->LineTo(rectChar.left+col_width[m],rectChar.top-(k+1)*charHigh);
rectCell.SetRect(rectChar.left+col_width[m],rectChar.top-k*charHigh,
rectChar.left+col_width[m+1],rectChar.top-(k+1)*charHigh);
VariantSet=m_FormQueryRecordSet.GetFieldValue(m);
if(m == 0) strUnit.Format("%d",k-1);
else strUnit.Format("%s",VariantSet.bstrVal);
pDC->DrawText( strUnit,&rectCell,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
pDC->MoveTo(rectChar.left+col_width[7],rectChar.top-k*charHigh);
pDC->LineTo(rectChar.left+col_width[7],rectChar.top-(k+1)*charHigh);
pDC->MoveTo(rectChar.left,rectChar.top-(k+1)*charHigh);
pDC->LineTo(rectChar.right,rectChar.top-(k+1)*charHigh);
if (m_FormQueryRecordSet.IsEOF()) {
break ;
}
m_FormQueryRecordSet.MoveNext() ;
}
}
//如果打印当前锻造记录,(非查询打印)
else
{
SaveReportForm();
int printRowNum=m_pGridCtrl->GetFormRealCout();
// if (printRowNum==0) {
// return;
// }
long lRows;
if (pInfo->m_nCurPage==1) {
lRows=0;
}
else
lRows=(pInfo->m_nCurPage-1)*ROWS_PER_PAGE; //要移动的相对位置!
GRID_ROW *grid_row;
CGridCell *grid_cell;
CString strUnit;
// for(k=2; (km_RowData.GetAt(k+lRows-1);
for(int m=0;m<7;m++)
{
if(k1 != -1){
grid_cell=grid_row->GetAt(m);
if( grid_cell->szText.GetLength()==0 )
strUnit=" ";
else
strUnit=grid_cell->szText ;
}else strUnit = " ";
pDC->MoveTo(rectChar.left+col_width[m],rectChar.top-k*charHigh);
pDC->LineTo(rectChar.left+col_width[m],rectChar.top-(k+1)*charHigh);
rectCell.SetRect(rectChar.left+col_width[m],rectChar.top-k*charHigh,
rectChar.left+col_width[m+1],rectChar.top-(k+1)*charHigh);
pDC->DrawText( strUnit,&rectCell,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
pDC->MoveTo(rectChar.left+col_width[7],rectChar.top-k*charHigh);
pDC->LineTo(rectChar.left+col_width[7],rectChar.top-(k+1)*charHigh);
pDC->MoveTo(rectChar.left,rectChar.top-(k+1)*charHigh);
pDC->LineTo(rectChar.right,rectChar.top-(k+1)*charHigh);
}
}
if (pInfo->m_nCurPage==pInfo->GetMaxPage()) {
pDC->LineTo(rectChar.right,rectChar.top-(k+1)*charHigh);
pDC->LineTo(rectChar.left,rectChar.top-(k+1)*charHigh);
pDC->MoveTo(rectChar.left,rectChar.top-(k)*charHigh);
pDC->LineTo(rectChar.left,rectChar.top-(k+1)*charHigh);
///////////////////////////////
CRect rectBottom1,rectBottom2;
rectBottom1.SetRect(rectChar.left,rectChar.top-(k)*charHigh,
rectChar.left+col_width[1],rectChar.top-(k+1)*charHigh) ;
rectBottom2.SetRect(rectChar.left+col_width[6],rectChar.top-(k)*charHigh,
rectChar.right,rectChar.top-(k+1)*charHigh) ;
pDC->DrawText("备注",&rectBottom1,DT_CENTER|DT_VCENTER|DT_SINGLELINE) ;
pDC->DrawText("检查者印",&rectBottom2,DT_LEFT|DT_VCENTER|DT_SINGLELINE) ;
pDC->MoveTo(rectChar.left+col_width[1],rectChar.top-(k)*charHigh) ;
pDC->LineTo(rectChar.left+col_width[1],rectChar.top-(k+1)*charHigh) ;
pDC->MoveTo(rectChar.left+col_width[6],rectChar.top-(k)*charHigh) ;
pDC->LineTo(rectChar.left+col_width[6],rectChar.top-(k+1)*charHigh) ;
CRect rectPage;
rectPage.SetRect(rectChar.left,rectChar.top-(k+2)*charHigh-100,rectChar.right,rectChar.top-(k+3)*charHigh-100);
CString strPageCurrent;
strPageCurrent.Format("第%d页",pInfo->m_nCurPage);
if ( pInfo->GetMaxPage()!=1 ) {
pDC->DrawText(strPageCurrent,&rectPage,DT_RIGHT);
}
}
else
{
CRect rectPage;
rectPage.SetRect(rectChar.left,rectChar.top-(k)*charHigh-100,rectChar.right,rectChar.top-(k+1)*charHigh-100);
CString strPageCurrent;
strPageCurrent.Format("第%d页",pInfo->m_nCurPage);
if ( pInfo->GetMaxPage()!=1 ) {
pDC->DrawText(strPageCurrent,&rectPage,DT_RIGHT);
}
}
pDC->SelectObject(pOldFont);
fontUseTitle.DeleteObject();
fontUseChar.DeleteObject();
pDC->SetBkMode(preBackMode);
}
BOOL CReportFormView::OnPreparePrinting(CPrintInfo* pInfo)
{
// TODO: call DoPreparePrinting to invoke the Print dialog box
int queryCout=0;
//判断是否为查询状态
if ( m_pGridCtrl->GetIfQueryStatus() ) {
if(m_FormQueryRecordSet.GetRecordCount()!=0)
{
m_FormQueryRecordSet.MoveLast();
queryCout=m_FormQueryRecordSet.GetRecordCount();
}
}
else
{
queryCout=m_pGridCtrl->GetFormRealCout() ;
}
int surplus=queryCout%ROWS_PER_PAGE ;
int maxPage=queryCout/ROWS_PER_PAGE ;
if ( surplus!=0 ) {
maxPage+=1;
}
pInfo->SetMaxPage(maxPage) ;
return DoPreparePrinting(pInfo);
// return CFormView::OnPreparePrinting(pInfo);
}
void CReportFormView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CFormView::OnBeginPrinting(pDC, pInfo);
}
void CReportFormView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CFormView::OnEndPrinting(pDC, pInfo);
}
void CReportFormView::OnFileSaveAs()
{
// TODO: Add your command handler code here
// m_pGridCtrl->SetForceEndEditForQuerySave();
SaveReportForm();
CRect rectBottom;
rectBottom.SetRect(m_rectForm.left,m_rectForm.bottom,
m_rectForm.right,m_rectForm.bottom+2*m_nRowHighView);
InvalidateRect(&rectBottom);
}