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

📄 细菌污染量监测view.cpp

📁 是自己帮一家医院做的小程序,还不完善,但很值得大家参考
💻 CPP
字号:
// 细菌污染量监测View.cpp : implementation of the CMyView class
//

#include "stdafx.h"
#include "细菌污染量监测.h"
#include "accountdlg.h"
#include "dlg.h"
#include "mainfrm.h"
#include "细菌污染量监测Doc.h"
#include "细菌污染量监测View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CListView)

BEGIN_MESSAGE_MAP(CMyView, CListView)
	//{{AFX_MSG_MAP(CMyView)
	ON_COMMAND(ID_ACCOUNT, OnAccount)
	ON_COMMAND(ID_DATE, OnDate)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
	ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
	ON_COMMAND(ID_REFRESH, OnRefresh)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction

CMyView::CMyView()
{
	// TODO: add construction code here
 count[0]=0;
  sun[0]=0; 
count[1]=0;
  sun[1]=0;
 count[2]=0;
  sun[2]=0; 
count[3]=0;
  sun[3]=0;
   month=-2;
}
CMyView::~CMyView()
{
}

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyView drawing

void CMyView::OnDraw(CDC* pDC)
{
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
//	CString strdate;
//strdate.Format("细菌污染监测   今天日期:%d-%d-%d",t2->tm_year+1900,t2->tm_mon+1,t2->tm_mday);
//SetWindowText(strdate);


	// TODO: add draw code for native data here
}

void CMyView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();
	
	CMainFrame *frm=(CMainFrame *)::AfxGetMainWnd();


    m_pRecordset.CreateInstance(__uuidof(Recordset));
	
	try{
    
		m_pRecordset->Open("SELECT * FROM tbl",                // 查询tbl表中所有字段
							frm->m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
							adOpenDynamic,
							adLockOptimistic,
							adCmdText);	
		
							
	}
   
	catch(_com_error * e)
	{	
	return;
	
	} 
	
	FieldsPtr pFields;

   pFields= m_pRecordset->GetFields();
	long ncol ;
   pFields->get_Count(&ncol);
	CListCtrl &plist=GetListCtrl();
CString value;
	FieldPtr pf;

for (long i = 0; i < ncol; i++)
	{  
		pf=pFields->GetItem(_variant_t(i));
	  value = LPCTSTR(_bstr_t(pf->GetName()));
	  if(i==1)
     plist.InsertColumn(i,value,LVCFMT_LEFT,180);
	 else
plist.InsertColumn(i,value,LVCFMT_LEFT,180);
	}
	plist.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
	plist.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
   if(m_pRecordset->GetState())
   {
	  m_pRecordset->Close();
	  CString date,date2;
	  time_t t1;
	  t2=NULL;

	time(&t1);
	
    t2=gmtime(&t1);
	date.Format("%d-%d-01",t2->tm_year+1900,t2->tm_mon+1);
	date2.Format("%d-%d-",t2->tm_year+1900,t2->tm_mon+1);
	date2=date2+ReturnDay(t2->tm_mon+1);
 CString sql="SELECT * FROM tbl where 采集时间>=#" + date + "# and 采集时间 <=#" + date2 + "#";
 
	m_pRecordset->Open(_variant_t(sql),               
							frm->m_pConnection.GetInterfacePtr(),
							adOpenDynamic,
							adLockOptimistic,
							adCmdText);	
}
   if(m_pRecordset->BOF)
   {
	   return;
   }
   int index=0;
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{   variant_t var;
    CString fn;
    var=m_pRecordset->GetCollect(long(0));
	if(var.vt !=NULL)
	fn=(LPCSTR)_bstr_t(var);
    plist.InsertItem(index,fn);
   for (long i = 1; i < ncol; i++)
   {  
 var=m_pRecordset->GetCollect(i);
	   if(var.vt!=VT_NULL)
	   fn=(LPCSTR)_bstr_t(var);
	   else
		   fn=_T("");
    plist.SetItemText(index,i,fn);

   }
   index++;
   m_pRecordset->MoveNext();

}

	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
}

/////////////////////////////////////////////////////////////////////////////
// CMyView printing

BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics

#ifdef _DEBUG
void CMyView::AssertValid() const
{
	CListView::AssertValid();
}

void CMyView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
	return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers

void CMyView::OnAccount() 
{
	// TODO: Add your command handler code here
	// CListCtrl &plist=GetListCtrl(); 
     //plist.DeleteAllItems();
	CMainFrame *frm=(CMainFrame *)::AfxGetMainWnd();
	 count[0]=0;
  sun[0]=0; 
count[1]=0;
  sun[1]=0;
 count[2]=0;
  sun[2]=0; 
count[3]=0;
  sun[3]=0;
count[4]=0;
  sun[4]=0;
CAccountDlg dlg;
    CString date;
	CString date2;
	CString sql;
if(dlg.DoModal()==IDOK)
{
month=dlg.m_radio;
if(month==4)
{  time_t t1;
	time(&t1);
	tm *t2=NULL;
    t2=gmtime(&t1);
	date.Format("%d-01-01",t2->tm_year+1900);
	date2.Format("%d-12-31",t2->tm_year+1900);
	sql="SELECT * FROM tbl where 采集时间>=#" + date + "# and 采集时间 <=#" + date2 + "#";
 }
else if(month==-1||month==-2)
{return;
}
else
{time_t t1;
	time(&t1);
	tm *t2=NULL;
    t2=gmtime(&t1);
date.Format("%d-%d-01",t2->tm_year+1900,month*3+1);
date2.Format("%d-%d-",t2->tm_year+1900,month*3+3);
date2=date2+ReturnDay(month*3+3);
	sql="SELECT * FROM tbl where 采集时间>=#" + date + "# and 采集时间 <=#" + date2 + "#";

}
	if(m_pRecordset->GetState())
   {
	  m_pRecordset->Close();
	  
	}
	try{
    m_pRecordset->Open(_variant_t(sql),               
							frm->m_pConnection.GetInterfacePtr(),
							adOpenDynamic,
							adLockOptimistic,
							adCmdText);	
	}
	catch(_com_error * e)
	{	
	return;
	
	} 
   if(m_pRecordset->BOF)
   {
	   return;
   }
   	FieldsPtr pFields;

   pFields= m_pRecordset->GetFields();
   long ncol ;
   pFields->get_Count(&ncol);
   
  
  m_pRecordset->MoveFirst();
//	 int icount;
	 sunn=0;
		 countt=0;
   while(!m_pRecordset->adoEOF)
   {

	   for(long i=1;i<=5;i++)
	   { 
//   icount=plist.GetItemCount();
	   
   variant_t var;
   CString str=_T("");
   var=m_pRecordset->GetCollect(i);
   if(var.vt!=VT_NULL)
	   str=(LPCSTR)_bstr_t(var);
   
	   if(str!="")
	   {
		   count[i-1]++;
		   if((str.FindOneOf("(+)")!=-1) && (str.FindOneOf("(+)")!=-1))
			   sun[i-1]++;
	   }
	   //
   }
m_pRecordset->MoveNext();
   } 
   	for(long j=0;j<=4;j++)
	{
 
sunn+=sun[j];
countt+=count[j];
}
   CView::OnFilePrintPreview();

}	
}

CString CMyView::ReturnDay(int i)
{
	CString s;
if(i==4||i==6||i==9||i==11)
s="30";
else if(i==2)
{time_t t1;
	time(&t1);
	tm *t2=NULL;
    t2=gmtime(&t1);
	int m=t2->tm_year+1900;
	if(m%4==0||m%100!=0||m%400==0)
		s="29";
	else
		s="28";

}
else
s="31";
return s;
}

BOOL CMyView::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	

	if(m_pRecordset->GetState())
     m_pRecordset->Close();
	
	return CListView::DestroyWindow();
}

void CMyView::OnDate() 
{
	// TODO: Add your command handler code here
	CDlg dlg;
	dlg.DoModal();
	OnRefresh();
	
}

void CMyView::OnFilePrintPreview() 
{
	// TODO: Add your command handler code here
	CView::OnFilePrintPreview();
}

void CMyView::OnFilePrint() 
{
	// TODO: Add your command handler code here
	
}

void CMyView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(month>4||month<0) return;
	CRect rect=pInfo->m_rectDraw;
	CString col[4]={_T("样品"),_T("采样数量"),_T("阴性(/例)"),_T("阴性率(%)")};
    CString mater[5]={_T("物表"),_T("空气"),_T("手"),_T("医疗设备"),_T("消毒液")};

	int x=0,y=0;
	int alige[4];
    CFont font;
	font.CreateFont(-120,0,0,0,400,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,DEFAULT_PITCH|FF_MODERN,"隶书");
	CFont *oldf=(CFont *)(pDC->SelectObject(&font));
    CString str;
    str="温州眼视光细菌污染情况统计";
    CSize size=pDC->GetTextExtent(str);
	CPoint point((rect.Width()-size.cx)/2,y);
	pDC->TextOut(point.x,12,str);
	TEXTMETRIC tm1;
	pDC->GetTextMetrics(&tm1);
	y+=y+3*tm1.tmHeight+tm1.tmExternalLeading;;
    CFont font2;
	font2.CreateFont(-70,0,0,0,400,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,DEFAULT_PITCH|FF_MODERN,"Courier New");
    pDC->SelectObject(&font2);
	for(int i=0;i<=3;i++)
	{size=pDC->GetTextExtent(col[i]);
	alige[i]=(i*rect.Width()/4-size.cx)/2+rect.Width()/5+i*180+col[i].GetLength()-30;
	pDC->TextOut(alige[i],y,col[i]);

	}
	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	
    for(int j=0;j<=4;j++)
	{y=y+2*tm.tmHeight+tm.tmExternalLeading;
	pDC->TextOut(alige[0],y,mater[j]);
	CString s;
	s.Format("%d",count[j]);
    pDC->TextOut(alige[1],y,s);

    s.Format("%d",(count[j]-sun[j]));
	pDC->TextOut(alige[2],y,s);

	if(count[j]!=0)
   s.Format("%1.2lf",(float)(count[j]-sun[j])/count[j]);
	else
		s="0";
	pDC->TextOut(alige[3],y,s);

	}
	y=y+2*tm.tmHeight+tm.tmExternalLeading;
	pDC->TextOut(alige[0],y,"总计:");
    CString s;
	s.Format("%d",countt);
    pDC->TextOut(alige[1],y,s);

    s.Format("%d",(countt-sunn));
	pDC->TextOut(alige[2],y,s);

	if(count[j]!=0)
   s.Format("%1.2lf",(float)(countt-sunn)/countt);
	else
		s="0";
	pDC->TextOut(alige[3],y,s);
	y=y+4*tm.tmHeight+tm.tmExternalLeading;
	if(month==4)
	s.Format("%d年",t2->tm_year+1900);
	else
    	s.Format("%d年第%d季度",t2->tm_year+1900,month+1);
   
    pDC->TextOut(rect.Width()/5,y,s);
	s="温州医学院附属眼视光医院";
    pDC->TextOut(3*rect.Width()/7+50,y,s);

    pDC->SelectObject(oldf);
	CListView::OnPrint(pDC, pInfo);
}

void CMyView::OnRefresh() 
{
	// TODO: Add your command handler code here
CMainFrame *frm=(CMainFrame *)::AfxGetMainWnd();
	  if(m_pRecordset->GetState())
   {
	  m_pRecordset->Close();
	  }
	  	FieldsPtr pFields;

    pFields= m_pRecordset->GetFields();
	long ncol ;
    pFields->get_Count(&ncol);
	CListCtrl &plist=GetListCtrl();


	  CString date,date2;
	  date.Format("%d-%d-01",t2->tm_year+1900,t2->tm_mon+1);
	  date2.Format("%d-%d-",t2->tm_year+1900,t2->tm_mon+1);
	  date2=date2+ReturnDay(t2->tm_mon+1);
      CString sql="SELECT * FROM tbl where 采集时间>=#" + date + "# and 采集时间 <=#" + date2 + "#";
      m_pRecordset->Open(_variant_t(sql),               
							frm->m_pConnection.GetInterfacePtr(),
							adOpenDynamic,
							adLockOptimistic,
							adCmdText);	

   if(m_pRecordset->BOF)
   {
	   return;
   }
   int index=0;
plist.DeleteAllItems();
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{   variant_t var;
    CString fn;
    var=m_pRecordset->GetCollect(long(0));
	if(var.vt !=NULL)
	fn=(LPCSTR)_bstr_t(var);
    plist.InsertItem(index,fn);
   for (long i = 1; i < ncol; i++)
   {  
 var=m_pRecordset->GetCollect(i);
	   if(var.vt!=VT_NULL)
	   fn=(LPCSTR)_bstr_t(var);
	   else
		   fn=_T("");
    plist.SetItemText(index,i,fn);

   }
   index++;
   m_pRecordset->MoveNext();

}

	
}

⌨️ 快捷键说明

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