www.pudn.com > MyRecord.rar > RecordNew.cpp, change:2008-03-02,size:17622b


// RecordNew.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "MyRecord.h" 
#include "RecordNew.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
extern CMyRecordApp theApp;       // 在此引用应用类中的theApp来获取库连接指针 
///////////////////////////////////////////////////////////////////////////// 
// CRecordNew dialog 
 
 
CRecordNew::CRecordNew(CWnd* pParent /*=NULL*/) 
	: CDialog(CRecordNew::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CRecordNew) 
	m_newitem = _T(""); 
	m_newname = _T(""); 
	m_newnote = _T(""); 
	m_picture1 = _T(""); 
	m_picture2 = _T(""); 
	m_picture3 = _T(""); 
	//}}AFX_DATA_INIT 
	RecordNum=0; 
} 
 
 
void CRecordNew::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CRecordNew) 
	DDX_Control(pDX, IDC_LIST, m_list); 
	DDX_Control(pDX, IDC_NEWTIME, m_newtime); 
	DDX_Text(pDX, IDC_NEWITEM, m_newitem); 
	DDX_Text(pDX, IDC_NEWNAME, m_newname); 
	DDX_Text(pDX, IDC_NEWNOTE, m_newnote); 
	DDX_Text(pDX, IDC_PICTURE1, m_picture1); 
	DDX_Text(pDX, IDC_PICTURE2, m_picture2); 
	DDX_Text(pDX, IDC_PICTURE3, m_picture3); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CRecordNew, CDialog) 
	//{{AFX_MSG_MAP(CRecordNew) 
	ON_BN_CLICKED(IDC_SAVE, OnSave) 
	ON_BN_CLICKED(IDC_NEWPICTURE1BUTTON, OnNewpicture1button) 
	ON_BN_CLICKED(IDC_NEWPICTURE2BUTTON, OnNewpicture2button) 
	ON_BN_CLICKED(IDC_NEWPICTURE3BUTTON, OnNewpicture3button) 
	ON_WM_PAINT() 
	ON_BN_CLICKED(IDC_REWRITE, OnRewrite) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CRecordNew message handlers 
 
 
 
void CRecordNew::OnSave()  
{ 
	// TODO: Add your control notification handler code here 
	 
	if(MessageBox("确定保存吗?","确认",MB_OKCANCEL)==IDOK) 
	{ 
		UpdateData(TRUE); 
		CString strnewname,strnewtime,strnewitem,strnewnote; 
		CFile f; 
		CFileException e; 
		if(m_newname.IsEmpty()) 
		{ 
			AfxMessageBox("请输入病人姓名!"); 
			return; 
		} 
		strnewname=m_newname; 
		CTime time; 
		m_newtime.GetTime(time); 
		strnewtime=time.Format("%y-%m-%d"); 
		strnewitem=m_newitem; 
		if(m_newitem.IsEmpty()) 
		{ 
			MessageBox("请输入手术项目!"); 
			return; 
		} 
		strnewnote=m_newnote; 
		RecordNum+=1; 
		GName[RecordNum-1]=strnewname; 
		GTime[RecordNum-1]=strnewtime; 
		GItem[RecordNum-1]=strnewitem; 
		GNote[RecordNum-1]=strnewnote; 
		//------------------------向列表中插入记录-------------------------------------// 
		//-----------------------------------------------------------------------------// 
		m_list.DeleteAllItems(); 
		for(int i=0;iAddNew(); 
			m_pRecordset->PutCollect("patientname",_variant_t(strnewname)); 
			m_pRecordset->PutCollect("operationtime",_variant_t(strnewtime)); 
			m_pRecordset->PutCollect("operationitem",_variant_t(strnewitem)); 
			m_pRecordset->PutCollect("note",_variant_t(strnewnote)); 
	 
			//---------------------保存图象内容进数据库(重要!)-------------------------------// 
			if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData(); // Important - Avoid Leaks... 
 
			if(f.Open(m_strPhotoPath1, CFile::modeRead | CFile::typeBinary, &e)) //打开了一个jpg文件 
			{	 
 
				nSize = f.GetLength();          //先得到jpg文件长度 
				BYTE * pBuffer = new BYTE [nSize];  //按文件的大小在堆上申请一块内存 
	  
				if (f.Read(pBuffer, nSize) > 0 )    //把jpg文件读到pBuffer(堆上申请一块内存) 
				{    
					BYTE *pBuf = pBuffer;     //下面这一大段是把pBuffer里的jpg数据放到库中 
					VARIANT			varBLOB; 
					SAFEARRAY		*psa; 
					SAFEARRAYBOUND	rgsabound[1]; 
					 
					if(pBuf) 
					{     
						rgsabound[0].lLbound = 0; 
						rgsabound[0].cElements = nSize; 
						psa = SafeArrayCreate(VT_UI1, 1, rgsabound); 
						for (long i = 0; i < (long)nSize; i++) 
						SafeArrayPutElement (psa, &i, pBuf++); 
						varBLOB.vt = VT_ARRAY | VT_UI1; 
						varBLOB.parray = psa; 
						m_pRecordset->GetFields()->GetItem("picture1")->AppendChunk(varBLOB); 
					} 
						 
					(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示 
					delete [] pBuffer;     //删掉堆上申请的那一块内存 
					pBuf=0;                //以防二次乱用 
				} 
				f.Close(); 
			} 
			//---------------------------------------------------------------------------------// 
			//---------------------保存图象内容进数据库(重要!)-------------------------------// 
			if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData(); // Important - Avoid Leaks... 
 
			if(f.Open(m_strPhotoPath2, CFile::modeRead | CFile::typeBinary, &e)) //打开了一个jpg文件 
			{	 
 
				nSize = f.GetLength();          //先得到jpg文件长度 
				BYTE * pBuffer = new BYTE [nSize];  //按文件的大小在堆上申请一块内存 
	  
				if (f.Read(pBuffer, nSize) > 0 )    //把jpg文件读到pBuffer(堆上申请一块内存) 
				{    
					BYTE *pBuf = pBuffer;     //下面这一大段是把pBuffer里的jpg数据放到库中 
					VARIANT			varBLOB; 
					SAFEARRAY		*psa; 
					SAFEARRAYBOUND	rgsabound[1]; 
					 
					if(pBuf) 
					{     
						rgsabound[0].lLbound = 0; 
						rgsabound[0].cElements = nSize; 
						psa = SafeArrayCreate(VT_UI1, 1, rgsabound); 
						for (long i = 0; i < (long)nSize; i++) 
						SafeArrayPutElement (psa, &i, pBuf++); 
						varBLOB.vt = VT_ARRAY | VT_UI1; 
						varBLOB.parray = psa; 
						m_pRecordset->GetFields()->GetItem("picture2")->AppendChunk(varBLOB); 
					} 
						 
					(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示 
					delete [] pBuffer;     //删掉堆上申请的那一块内存 
					pBuf=0;                //以防二次乱用 
				} 
				f.Close(); 
			} 
			//---------------------------------------------------------------------------------// 
			//---------------------保存图象内容进数据库(重要!)-------------------------------// 
			if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData(); // Important - Avoid Leaks... 
 
			if(f.Open(m_strPhotoPath3, CFile::modeRead | CFile::typeBinary, &e)) //打开了一个jpg文件 
			{	 
 
				nSize = f.GetLength();          //先得到jpg文件长度 
				BYTE * pBuffer = new BYTE [nSize];  //按文件的大小在堆上申请一块内存 
	  
				if (f.Read(pBuffer, nSize) > 0 )    //把jpg文件读到pBuffer(堆上申请一块内存) 
				{    
					BYTE *pBuf = pBuffer;     //下面这一大段是把pBuffer里的jpg数据放到库中 
					VARIANT			varBLOB; 
					SAFEARRAY		*psa; 
					SAFEARRAYBOUND	rgsabound[1]; 
					 
					if(pBuf) 
					{     
						rgsabound[0].lLbound = 0; 
						rgsabound[0].cElements = nSize; 
						psa = SafeArrayCreate(VT_UI1, 1, rgsabound); 
						for (long i = 0; i < (long)nSize; i++) 
						SafeArrayPutElement (psa, &i, pBuf++); 
						varBLOB.vt = VT_ARRAY | VT_UI1; 
						varBLOB.parray = psa; 
						m_pRecordset->GetFields()->GetItem("picture3")->AppendChunk(varBLOB); 
					} 
						 
					(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示 
					delete [] pBuffer;     //删掉堆上申请的那一块内存 
					pBuf=0;                //以防二次乱用 
				} 
				f.Close(); 
			} 
			//---------------------------------------------------------------------------------// 
			m_pRecordset->Update();		//更新数据库 
			m_pRecordset->MoveLast();	//当前记录移动到最后 
		} 
		catch(_com_error e) 
		{ 
			CString strError; 
			strError.Format("警告:插入信息是发生异常。错误信息: %s",\ 
				e.ErrorMessage()); 
			AfxMessageBox(strError); 
		} 
	} 
	OnRewrite(); 
} 
 
void CRecordNew::OnRewrite()  
{ 
	// TODO: Add your control notification handler code here 
	m_newitem = _T(""); 
	m_newname = _T(""); 
	m_newnote = _T(""); 
	 
	UpdateData(FALSE); 
} 
 
void CRecordNew::OnNewpicture1button()  
{ 
	// TODO: Add your control notification handler code here 
	//	InsertPhoto(); 
	//	if (m_Picture.Load(m_strPhotoPath))//载入图片路径,在picturebox中显示图片 
	//	m_Picture.Draw(); 
	CFile file; 
	CString  FilePathName; 
	CFileException e; 
	HRESULT hr; 
	//获得picture控件的大小 
	CWnd *pWnd=GetDlgItem(IDC_PICTURE1);  
	CDC *pDC=pWnd->GetDC();  
	CRect rect;  
	pWnd->GetClientRect(&rect);  
	//填充背景 
	HBRUSH   brush   =   (HBRUSH)::GetStockObject(GRAY_BRUSH);      
    ::FillRect(pDC->m_hDC,rect,brush);  
	CFileDialog dlg(TRUE,NULL,NULL,0,"BMP Files (*.bmp)|*.bmp|jpg Files (*.jpg)|*.jpg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 
    if(dlg.DoModal()==IDOK) 
    { 
		FilePathName=dlg.GetPathName(); 
		m_strPhotoPath1=FilePathName; 
		file.Open(FilePathName, CFile::modeRead | CFile::shareDenyNone );  // 读入文件内容 
		//获得picture大小 
		DWORD dwSize = file.GetLength(); 
 
		HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, dwSize ); 
		LPVOID lpBuf = ::GlobalLock( hMem ); 
		file.ReadHuge( lpBuf, dwSize ); 
		file.Close(); 
		::GlobalUnlock( hMem ); 
 
		// 由 HGLOBAL 得到 IStream,参数 TRUE 表示释放 IStream 的同时,释放内存 
		hr = ::CreateStreamOnHGlobal( hMem, TRUE, &pStream ); 
		hr = ::OleLoadPicture( pStream, dwSize, TRUE, IID_IPicture, ( LPVOID * )&pPicture ); 
		 
		long nWidth,nHeight;  // 宽高,MM_HIMETRIC 模式,单位是0.01毫米 
		 
		pPicture->get_Width( &nWidth );    // 宽 
		pPicture->get_Height( &nHeight );  // 高 
 
		CSize sz( nWidth, nHeight ); 
		pDC->HIMETRICtoDP( &sz );  // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位 
		long x, y, cx, cy; 
		//自动适应窗口 
		double fRatePic, fRateWnd; 
		fRatePic = (double)sz.cx / (double)sz.cy; 
		fRateWnd = (double)rect.Width() / (double)rect.Height(); 
 
		if (fRatePic > fRateWnd) 
		{ 
			cx = rect.Width(); 
			cy = (long)(rect.Width() / fRatePic); 
        } 
		else 
		{ 
			cx = (long)(rect.Height() * fRatePic); 
			cy = rect.Height(); 
		} 
		if (cx == rect.Width()) 
		{ 
			x = 0; 
			y = rect.Height() / 2 - cy / 2; 
		} 
		if (cy == rect.Height()) 
		{ 
			x = rect.Width() / 2 - cx / 2; 
			y = 0; 
		} 
		pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, nHeight, nWidth, -nHeight, &rect); 
		if ( pPicture ) pPicture->Release();// 释放 IPicture 指针 
		if ( pStream ) pStream->Release();  // 释放 IStream 指针,同时释放了 hMem 
	} 
} 
 
/*void CRecordNew::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
}*/ 
 
void CRecordNew::OnNewpicture2button()  
{ 
	// TODO: Add your control notification handler code here 
	//	InsertPhoto(); 
	//	if (m_Picture.Load(m_strPhotoPath))//载入图片路径,在picturebox中显示图片 
	//	m_Picture.Draw(); 
	CFile file; 
	CString  FilePathName; 
	CFileException e; 
	HRESULT hr; 
	//获得picture控件的大小 
	CWnd *pWnd=GetDlgItem(IDC_PICTURE2);  
	CDC *pDC=pWnd->GetDC();  
	CRect rect;  
	pWnd->GetClientRect(&rect);  
	//填充背景 
	HBRUSH   brush   =   (HBRUSH)::GetStockObject(GRAY_BRUSH);      
    ::FillRect(pDC->m_hDC,rect,brush);  
	CFileDialog dlg(TRUE,NULL,NULL,0,"BMP Files (*.bmp)|*.bmp|jpg Files (*.jpg)|*.jpg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 
    if(dlg.DoModal()==IDOK) 
    { 
		FilePathName=dlg.GetPathName(); 
		m_strPhotoPath2=FilePathName; 
		file.Open(FilePathName, CFile::modeRead | CFile::shareDenyNone );  // 读入文件内容 
		//获得picture大小 
		DWORD dwSize = file.GetLength(); 
 
		HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, dwSize ); 
		LPVOID lpBuf = ::GlobalLock( hMem ); 
		file.ReadHuge( lpBuf, dwSize ); 
		file.Close(); 
		::GlobalUnlock( hMem ); 
 
		// 由 HGLOBAL 得到 IStream,参数 TRUE 表示释放 IStream 的同时,释放内存 
		hr = ::CreateStreamOnHGlobal( hMem, TRUE, &pStream ); 
		hr = ::OleLoadPicture( pStream, dwSize, TRUE, IID_IPicture, ( LPVOID * )&pPicture ); 
		 
		long nWidth,nHeight;  // 宽高,MM_HIMETRIC 模式,单位是0.01毫米 
		 
		pPicture->get_Width( &nWidth );    // 宽 
		pPicture->get_Height( &nHeight );  // 高 
 
		CSize sz( nWidth, nHeight ); 
		pDC->HIMETRICtoDP( &sz );  // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位 
		long x, y, cx, cy; 
		//自动适应窗口 
		double fRatePic, fRateWnd; 
		fRatePic = (double)sz.cx / (double)sz.cy; 
		fRateWnd = (double)rect.Width() / (double)rect.Height(); 
 
		if (fRatePic > fRateWnd) 
		{ 
			cx = rect.Width(); 
			cy = (long)(rect.Width() / fRatePic); 
        } 
		else 
		{ 
			cx = (long)(rect.Height() * fRatePic); 
			cy = rect.Height(); 
		} 
		if (cx == rect.Width()) 
		{ 
			x = 0; 
			y = rect.Height() / 2 - cy / 2; 
		} 
		if (cy == rect.Height()) 
		{ 
			x = rect.Width() / 2 - cx / 2; 
			y = 0; 
		} 
		pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, nHeight, nWidth, -nHeight, &rect); 
		if ( pPicture ) pPicture->Release();// 释放 IPicture 指针 
		if ( pStream ) pStream->Release();  // 释放 IStream 指针,同时释放了 hMem 
	} 
} 
 
void CRecordNew::OnNewpicture3button()  
{ 
	// TODO: Add your control notification handler code here 
	//	InsertPhoto(); 
	//	if (m_Picture.Load(m_strPhotoPath))//载入图片路径,在picturebox中显示图片 
	//	m_Picture.Draw(); 
	CFile file; 
	CString  FilePathName; 
	CFileException e; 
	HRESULT hr; 
	//获得picture控件的大小 
	CWnd *pWnd=GetDlgItem(IDC_PICTURE3);  
	CDC *pDC=pWnd->GetDC();  
	CRect rect;  
	pWnd->GetClientRect(&rect);  
	//填充背景 
	HBRUSH   brush   =   (HBRUSH)::GetStockObject(GRAY_BRUSH);      
    ::FillRect(pDC->m_hDC,rect,brush);  
	CFileDialog dlg(TRUE,NULL,NULL,0,"BMP Files (*.bmp)|*.bmp|jpg Files (*.jpg)|*.jpg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 
    if(dlg.DoModal()==IDOK) 
    { 
		FilePathName=dlg.GetPathName(); 
		m_strPhotoPath3=FilePathName; 
		file.Open(FilePathName, CFile::modeRead | CFile::shareDenyNone );  // 读入文件内容 
		//获得picture大小 
		DWORD dwSize = file.GetLength(); 
 
		HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, dwSize ); 
		LPVOID lpBuf = ::GlobalLock( hMem ); 
		file.ReadHuge( lpBuf, dwSize ); 
		file.Close(); 
		::GlobalUnlock( hMem ); 
 
		// 由 HGLOBAL 得到 IStream,参数 TRUE 表示释放 IStream 的同时,释放内存 
		hr = ::CreateStreamOnHGlobal( hMem, TRUE, &pStream ); 
		hr = ::OleLoadPicture( pStream, dwSize, TRUE, IID_IPicture, ( LPVOID * )&pPicture ); 
		 
		long nWidth,nHeight;  // 宽高,MM_HIMETRIC 模式,单位是0.01毫米 
		 
		pPicture->get_Width( &nWidth );    // 宽 
		pPicture->get_Height( &nHeight );  // 高 
 
		CSize sz( nWidth, nHeight ); 
		pDC->HIMETRICtoDP( &sz );  // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位 
		long x, y, cx, cy; 
		//自动适应窗口 
		double fRatePic, fRateWnd; 
		fRatePic = (double)sz.cx / (double)sz.cy; 
		fRateWnd = (double)rect.Width() / (double)rect.Height(); 
 
		if (fRatePic > fRateWnd) 
		{ 
			cx = rect.Width(); 
			cy = (long)(rect.Width() / fRatePic); 
        } 
		else 
		{ 
			cx = (long)(rect.Height() * fRatePic); 
			cy = rect.Height(); 
		} 
		if (cx == rect.Width()) 
		{ 
			x = 0; 
			y = rect.Height() / 2 - cy / 2; 
		} 
		if (cy == rect.Height()) 
		{ 
			x = rect.Width() / 2 - cx / 2; 
			y = 0; 
		} 
		pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, nHeight, nWidth, -nHeight, &rect); 
		if ( pPicture ) pPicture->Release();// 释放 IPicture 指针 
		if ( pStream ) pStream->Release();  // 释放 IStream 指针,同时释放了 hMem 
	} 
} 
 
BOOL CRecordNew::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	// TODO: Add extra initialization here 
//------------------------初始化ListCtrl控件----------------------------------// 
//----------------------------------------------------------------------------// 
	 
	DWORD dwStyle = m_list.GetExtendedStyle(); 
	dwStyle |= LVS_EX_GRIDLINES;		//网格线(只适用与report风格的listctrl) 
	m_list.SetExtendedStyle(dwStyle);	//设置扩展风格 
	CString Field[4]={"姓名","手术时间","手术项目","备注"}; 
	for(int j=0;j<4;j++) 
	{ 
		m_list.InsertColumn(j,Field[j],LVCFMT_LEFT,100); 
	} 
/*	for(int i=0;iOpen("SELECT * FROM info",                // 查询DemoTable表中所有字段 
					theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针 
							adOpenDynamic, 
							adLockOptimistic, 
							adCmdText); 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	} 
//-------------------------------------------------------------------------// 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
}