ImageDBView.cpp
资源名称:ImageDB.rar [点击查看]
上传用户:bst807
上传日期:2022-07-10
资源大小:74k
文件大小:11k
源码类别:
数据库编程
开发平台:
Visual C++
- // ImageDBView.cpp : implementation of the CImageDBView class
- //
- #include "stdafx.h"
- #include "ImageDB.h"
- #include "ImageDBDoc.h"
- #include "ImageDBView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CImageDBView
- IMPLEMENT_DYNCREATE(CImageDBView, CFormView)
- BEGIN_MESSAGE_MAP(CImageDBView, CFormView)
- //{{AFX_MSG_MAP(CImageDBView)
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CImageDBView construction/destruction
- CImageDBView::CImageDBView()
- : CFormView(CImageDBView::IDD)
- {
- //{{AFX_DATA_INIT(CImageDBView)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- m_bDraw=0;
- m_bImageOpen=FALSE;
- }
- CImageDBView::~CImageDBView()
- {
- }
- void CImageDBView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CImageDBView)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BOOL CImageDBView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- void CImageDBView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CImageDBView printing
- BOOL CImageDBView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CImageDBView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CImageDBView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- void CImageDBView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
- {
- // TODO: add customized printing code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CImageDBView diagnostics
- #ifdef _DEBUG
- void CImageDBView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CImageDBView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CImageDBDoc* CImageDBView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImageDBDoc)));
- return (CImageDBDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CImageDBView message handlers
- void CImageDBView::OnButton1()
- {
- // TODO: Add your control notification handler code here
- CString sPno,sPname;
- GetDlgItemText(IDC_EDIT1,sPno);
- GetDlgItemText(IDC_EDIT2,sPname);
- if (sPno.IsEmpty())
- {
- AfxMessageBox("请输入球员号!");
- GetDlgItem(IDC_EDIT1)->SetFocus();
- return;
- }
- if (sPname.IsEmpty())
- {
- AfxMessageBox("请输入球员名字!");
- GetDlgItem(IDC_EDIT2)->SetFocus();
- return;
- }
- if (!m_bImageOpen)
- {
- AfxMessageBox("请选择球员图片");
- GetDlgItem(IDC_BUTTON3)->SetFocus();
- return;
- }
- _RecordsetPtr pRecordset;
- pRecordset.CreateInstance(__uuidof(Recordset));
- CString strSQL;
- strSQL.Format("select * from Player where Pno='%s'",sPno);
- pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
- if (!pRecordset->BOF)
- {
- AfxMessageBox("您要输入的球员号已存在!");
- pRecordset->Close();
- return;
- }
- pRecordset->Close();
- strSQL.Format("insert into Player values('%s','%s',0x)",sPno,sPname);
- pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
- strSQL.Format("select Photo from player where Pno='%s'",sPno);
- pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
- Image_DB(TRUE,m_FileName,pRecordset,"Photo");
- pRecordset->Close();
- AfxMessageBox("球员信息已成功写入数据库!");
- }
- void CImageDBView::OnButton2()
- {
- // TODO: Add your control notification handler code here
- CString sPno,sPname;
- CString strSQL;
- GetDlgItemText(IDC_EDIT1,sPno);
- GetDlgItemText(IDC_EDIT2,sPname);
- if (sPno.IsEmpty() && sPname.IsEmpty())
- {
- AfxMessageBox("您至少输入一个球员信息!");
- return;
- }
- if (sPname.IsEmpty())
- strSQL.Format("select * from Player where Pno='%s'",sPno);
- else if (sPno.IsEmpty())
- strSQL.Format("select * from Player where Pname='%s'",sPname);
- else
- strSQL.Format("select * from Player where Pno='%s' and Pname='%s'",sPno,sPname);
- _RecordsetPtr pRecordset;
- pRecordset.CreateInstance(__uuidof(Recordset));
- pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
- if(pRecordset->BOF)
- {
- AfxMessageBox("您要找的球员不存在!");
- return;
- }
- _variant_t varPno=pRecordset->GetFields()->GetItem("Pno")->Value;
- _variant_t varPname=pRecordset->GetFields()->GetItem("Pname")->Value;
- CString a;
- SetDlgItemText(IDC_EDIT1,(LPCTSTR)(_bstr_t)varPno);
- SetDlgItemText(IDC_PNAME,(LPCTSTR)(_bstr_t)varPname);
- _StreamPtr pStm;
- pStm.CreateInstance(__uuidof(Stream));
- pStm->PutType(adTypeBinary);
- _variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
- pStm->Open(varOptional,adModeUnknown,adOpenStreamUnspecified,_bstr_t(),_bstr_t());
- _variant_t varBLOB;
- varBLOB=pRecordset->GetFields()->GetItem(_variant_t("Photo"))->Value;
- pStm->Write(varBLOB);
- m_pStream=pStm;
- m_varBLOB=varBLOB;
- DrawPhotoFromDB();
- m_bDraw=2;
- CButton* pBtn=(CButton*)GetDlgItem(IDC_CHECK1);
- if (pBtn->GetCheck()==1)
- {
- CFileDialog FileDlg(FALSE,"*.jpg",NULL,Default(),"图片文件(*.jpg; *.bmp)|*.jpg; *.bmp||",NULL);
- FileDlg.DoModal();
- CString filename;
- filename=FileDlg.GetPathName();
- if (filename.IsEmpty())
- return;
- CFileFind findfile;
- int bExist=findfile.FindFile(filename);
- if (bExist)
- {
- AfxMessageBox("您要保存的文件已存在!");
- return;
- }
- Image_DB(FALSE,filename,pRecordset,"Photo");
- }
- pRecordset->Close();
- }
- void CImageDBView::Image_DB(BOOL bImageToDB, LPCTSTR lpszFileName, _RecordsetPtr pRecordset, LPCTSTR lpszColumn)
- {
- _StreamPtr pStm;
- pStm.CreateInstance(__uuidof(Stream));
- pStm->PutType(adTypeBinary);
- _variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
- pStm->Open(varOptional,adModeUnknown,adOpenStreamUnspecified,_bstr_t(),_bstr_t());
- _variant_t varBLOB;
- if (bImageToDB)
- {
- pStm->LoadFromFile(_bstr_t(lpszFileName));
- varBLOB=pStm->Read(adReadAll);
- pRecordset->GetFields()->GetItem(_variant_t(lpszColumn))->Value=varBLOB;
- //或者用pRecordset->GetFields()->GetItem(_variant_t(lpszColumn))->AppendChunk(varBLOB);
- pRecordset->Update();
- }
- else
- {
- varBLOB=pRecordset->GetFields()->GetItem(_variant_t(lpszColumn))->Value;
- //可以用GetChuck实现上面语句的功能。
- pStm->Write(varBLOB);
- pStm->SaveToFile(_bstr_t(lpszFileName),adSaveCreateOverWrite);
- }
- }
- void CImageDBView::OnDraw(CDC* pDC)
- {
- // TODO: Add your specialized code here and/or call the base class
- //DrawPhoto();
- if(m_bDraw==1)
- DrawPhotoFromFile(m_FileName);
- if(m_bDraw==2)
- DrawPhotoFromDB();
- }
- void CImageDBView::OnButton3()
- {
- // TODO: Add your control notification handler code here
- CFileDialog FileDlg(TRUE,"*.jpg",NULL,OFN_HIDEREADONLY,"图片文件(*.jpg; *.bmp)|*.jpg; *.bmp||",NULL);
- FileDlg.DoModal();
- CString filename;
- filename=FileDlg.GetPathName();
- if (filename.IsEmpty())
- return;
- CFileFind findfile;
- int bExist=findfile.FindFile(filename);
- if (!bExist)
- {
- AfxMessageBox("您要打开的文件不存在!");
- return;
- }
- if (m_bImageOpen==FALSE)
- m_bImageOpen=TRUE;
- m_FileName=filename;
- m_bDraw=1;
- DrawPhotoFromFile(m_FileName);
- }
- void CImageDBView::DrawPhotoFromFile(CString FileName)
- {
- IStream *pStm;
- CFile file;
- if (file.Open(FileName,CFile::modeRead))
- {
- HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,file.GetLength());
- LPVOID pvData=NULL;
- if (hGlobal!=NULL)
- {
- if ((pvData = GlobalLock(hGlobal)) != NULL)
- {
- file.ReadHuge(pvData,file.GetLength());
- GlobalUnlock(hGlobal);
- CreateStreamOnHGlobal(pvData, TRUE, &pStm);
- }
- }
- } //使用void *pBuf=malloc(file.GetLength())代替hGlobal的方法不行,不知为何???
- IPicture* pPic;
- if(FAILED(OleLoadPicture(pStm,file.GetLength(),TRUE,IID_IPicture,(LPVOID*)&pPic)))
- AfxMessageBox("从流中装载图像失败!");
- else
- {
- OLE_XSIZE_HIMETRIC pWidth;
- OLE_YSIZE_HIMETRIC pHeight;
- pPic->get_Width(&pWidth);
- pPic->get_Height(&pHeight);
- CDC *pDC=GetDC();
- CRect rt;
- GetDlgItem(IDC_PHOTO)->GetWindowRect(rt);
- ScreenToClient(rt);
- if(FAILED(pPic->Render(*pDC,rt.left+2,rt.top+2,rt.Width()-5,rt.Height()-5,0,pHeight,pWidth,-pHeight,NULL)))
- AfxMessageBox( "图像渲染失败!");
- //////////////////////////////////////////////////////////////////////////
- /*可以根据图片大小显示
- double fx,fy;
- fx = (double) pDC->GetDeviceCaps(HORZRES)*(double)pWidth/
- ((double)pDC->GetDeviceCaps(HORZSIZE)*300.0); //改变数值可以改变图片显示的比例
- fy = (double)pDC->GetDeviceCaps(VERTRES)*(double)pHeight/
- ((double)pDC->GetDeviceCaps(VERTSIZE)*300.0);
- pPic->Render(*pDC,400,0,(LONG)fx,(LONG)fy,0,pHeight,pWidth,-pHeight,NULL);
- *//////////////////////////////////////////////////////////////////////////
- }
- }
- void CImageDBView::DrawPhotoFromDB()
- {
- //将_StreamPtr *pstream 转为IStream *ps,因为OleLoadPicture需要的流是IStream流,_StreamPtr流不行。
- //LPVOID pvData = NULL;
- IStream *ps;
- void* pBuf=malloc(size_t(m_pStream->GetSize()));
- SafeArrayAccessData(m_varBLOB.parray,(void **) &pBuf);
- SafeArrayUnaccessData(m_varBLOB.parray);
- CreateStreamOnHGlobal(pBuf, TRUE, &ps);
- //不能释放内存pBuf,因为ps只是个流,不具备pBuf的全部信息,离开pBuf,就不行了。
- IPicture *pPic;
- if(SUCCEEDED(OleLoadPicture(ps,m_pStream->GetSize() ,TRUE,IID_IPicture,(LPVOID*)&pPic)))
- {
- OLE_XSIZE_HIMETRIC hmWidth;
- OLE_YSIZE_HIMETRIC hmHeight;
- pPic->get_Width(&hmWidth);
- pPic->get_Height(&hmHeight);
- CRect rt;
- GetDlgItem(IDC_PHOTO)->GetWindowRect(rt);
- ScreenToClient(rt);
- CDC *pDC=GetDC();
- if(FAILED(pPic->Render(*pDC,rt.left+2,rt.top+2,rt.Width()-5,rt.Height()-5,0,hmHeight,hmWidth,-hmHeight,NULL)))
- AfxMessageBox( "图像渲染失败!");
- }
- }