ImageDBView.cpp
上传用户:bst807
上传日期:2022-07-10
资源大小:74k
文件大小:11k
源码类别:

数据库编程

开发平台:

Visual C++

  1. // ImageDBView.cpp : implementation of the CImageDBView class
  2. //
  3. #include "stdafx.h"
  4. #include "ImageDB.h"
  5. #include "ImageDBDoc.h"
  6. #include "ImageDBView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CImageDBView
  14. IMPLEMENT_DYNCREATE(CImageDBView, CFormView)
  15. BEGIN_MESSAGE_MAP(CImageDBView, CFormView)
  16. //{{AFX_MSG_MAP(CImageDBView)
  17. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  18. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  19. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CImageDBView construction/destruction
  28. CImageDBView::CImageDBView()
  29. : CFormView(CImageDBView::IDD)
  30. {
  31. //{{AFX_DATA_INIT(CImageDBView)
  32. // NOTE: the ClassWizard will add member initialization here
  33. //}}AFX_DATA_INIT
  34. // TODO: add construction code here
  35. m_bDraw=0;
  36. m_bImageOpen=FALSE;
  37. }
  38. CImageDBView::~CImageDBView()
  39. {
  40. }
  41. void CImageDBView::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CFormView::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CImageDBView)
  45. // NOTE: the ClassWizard will add DDX and DDV calls here
  46. //}}AFX_DATA_MAP
  47. }
  48. BOOL CImageDBView::PreCreateWindow(CREATESTRUCT& cs)
  49. {
  50. // TODO: Modify the Window class or styles here by modifying
  51. //  the CREATESTRUCT cs
  52. return CFormView::PreCreateWindow(cs);
  53. }
  54. void CImageDBView::OnInitialUpdate()
  55. {
  56. CFormView::OnInitialUpdate();
  57. GetParentFrame()->RecalcLayout();
  58. ResizeParentToFit();
  59. }
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CImageDBView printing
  62. BOOL CImageDBView::OnPreparePrinting(CPrintInfo* pInfo)
  63. {
  64. // default preparation
  65. return DoPreparePrinting(pInfo);
  66. }
  67. void CImageDBView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  68. {
  69. // TODO: add extra initialization before printing
  70. }
  71. void CImageDBView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  72. {
  73. // TODO: add cleanup after printing
  74. }
  75. void CImageDBView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
  76. {
  77. // TODO: add customized printing code here
  78. }
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CImageDBView diagnostics
  81. #ifdef _DEBUG
  82. void CImageDBView::AssertValid() const
  83. {
  84. CFormView::AssertValid();
  85. }
  86. void CImageDBView::Dump(CDumpContext& dc) const
  87. {
  88. CFormView::Dump(dc);
  89. }
  90. CImageDBDoc* CImageDBView::GetDocument() // non-debug version is inline
  91. {
  92. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImageDBDoc)));
  93. return (CImageDBDoc*)m_pDocument;
  94. }
  95. #endif //_DEBUG
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CImageDBView message handlers
  98. void CImageDBView::OnButton1() 
  99. {
  100. // TODO: Add your control notification handler code here
  101. CString sPno,sPname;
  102. GetDlgItemText(IDC_EDIT1,sPno);
  103. GetDlgItemText(IDC_EDIT2,sPname);
  104. if (sPno.IsEmpty())
  105. {
  106. AfxMessageBox("请输入球员号!");
  107. GetDlgItem(IDC_EDIT1)->SetFocus();
  108. return;
  109. }
  110. if (sPname.IsEmpty())
  111. {
  112. AfxMessageBox("请输入球员名字!");
  113. GetDlgItem(IDC_EDIT2)->SetFocus();
  114. return;
  115. }
  116. if (!m_bImageOpen)
  117. {
  118. AfxMessageBox("请选择球员图片");
  119. GetDlgItem(IDC_BUTTON3)->SetFocus();
  120. return;
  121. }
  122. _RecordsetPtr pRecordset;
  123. pRecordset.CreateInstance(__uuidof(Recordset));
  124. CString strSQL;
  125. strSQL.Format("select * from Player where Pno='%s'",sPno);
  126. pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  127. if (!pRecordset->BOF)
  128. {
  129. AfxMessageBox("您要输入的球员号已存在!");
  130. pRecordset->Close();
  131. return;
  132. }
  133. pRecordset->Close();
  134. strSQL.Format("insert into Player values('%s','%s',0x)",sPno,sPname);
  135. pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  136. strSQL.Format("select Photo from player where Pno='%s'",sPno);
  137. pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  138. Image_DB(TRUE,m_FileName,pRecordset,"Photo");
  139. pRecordset->Close();
  140. AfxMessageBox("球员信息已成功写入数据库!");
  141. }
  142. void CImageDBView::OnButton2() 
  143. {
  144. // TODO: Add your control notification handler code here
  145. CString sPno,sPname;
  146. CString strSQL;
  147. GetDlgItemText(IDC_EDIT1,sPno);
  148. GetDlgItemText(IDC_EDIT2,sPname);
  149. if (sPno.IsEmpty() && sPname.IsEmpty())
  150. {
  151. AfxMessageBox("您至少输入一个球员信息!");
  152. return;
  153. }
  154. if (sPname.IsEmpty())
  155. strSQL.Format("select * from Player where Pno='%s'",sPno);
  156. else if (sPno.IsEmpty())
  157. strSQL.Format("select * from Player where Pname='%s'",sPname);
  158. else
  159. strSQL.Format("select * from Player where Pno='%s' and Pname='%s'",sPno,sPname);
  160. _RecordsetPtr pRecordset;
  161. pRecordset.CreateInstance(__uuidof(Recordset));
  162. pRecordset->Open(_variant_t(strSQL),GetDocument()->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  163. if(pRecordset->BOF)
  164. {
  165. AfxMessageBox("您要找的球员不存在!");
  166. return;
  167. }
  168. _variant_t varPno=pRecordset->GetFields()->GetItem("Pno")->Value;
  169. _variant_t varPname=pRecordset->GetFields()->GetItem("Pname")->Value;
  170. CString a;
  171. SetDlgItemText(IDC_EDIT1,(LPCTSTR)(_bstr_t)varPno);
  172. SetDlgItemText(IDC_PNAME,(LPCTSTR)(_bstr_t)varPname);
  173. _StreamPtr pStm;
  174. pStm.CreateInstance(__uuidof(Stream));
  175. pStm->PutType(adTypeBinary);
  176. _variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
  177. pStm->Open(varOptional,adModeUnknown,adOpenStreamUnspecified,_bstr_t(),_bstr_t());
  178. _variant_t varBLOB;
  179. varBLOB=pRecordset->GetFields()->GetItem(_variant_t("Photo"))->Value;
  180. pStm->Write(varBLOB);
  181. m_pStream=pStm;
  182. m_varBLOB=varBLOB;
  183. DrawPhotoFromDB();
  184. m_bDraw=2;
  185. CButton* pBtn=(CButton*)GetDlgItem(IDC_CHECK1);
  186. if (pBtn->GetCheck()==1)
  187. {
  188. CFileDialog FileDlg(FALSE,"*.jpg",NULL,Default(),"图片文件(*.jpg; *.bmp)|*.jpg; *.bmp||",NULL);
  189. FileDlg.DoModal();
  190. CString filename;
  191. filename=FileDlg.GetPathName();
  192. if (filename.IsEmpty())
  193. return;
  194. CFileFind findfile;
  195. int bExist=findfile.FindFile(filename);
  196. if (bExist)
  197. {
  198. AfxMessageBox("您要保存的文件已存在!");
  199. return;
  200. }
  201. Image_DB(FALSE,filename,pRecordset,"Photo");
  202. }
  203. pRecordset->Close();
  204. }
  205. void CImageDBView::Image_DB(BOOL bImageToDB, LPCTSTR lpszFileName, _RecordsetPtr pRecordset, LPCTSTR lpszColumn)
  206. {
  207. _StreamPtr pStm;
  208. pStm.CreateInstance(__uuidof(Stream));
  209. pStm->PutType(adTypeBinary);
  210. _variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
  211. pStm->Open(varOptional,adModeUnknown,adOpenStreamUnspecified,_bstr_t(),_bstr_t());
  212. _variant_t varBLOB;
  213. if (bImageToDB)
  214. {
  215. pStm->LoadFromFile(_bstr_t(lpszFileName));
  216. varBLOB=pStm->Read(adReadAll);
  217. pRecordset->GetFields()->GetItem(_variant_t(lpszColumn))->Value=varBLOB;
  218. //或者用pRecordset->GetFields()->GetItem(_variant_t(lpszColumn))->AppendChunk(varBLOB);
  219. pRecordset->Update();
  220. else
  221. {
  222. varBLOB=pRecordset->GetFields()->GetItem(_variant_t(lpszColumn))->Value;
  223. //可以用GetChuck实现上面语句的功能。
  224. pStm->Write(varBLOB);
  225. pStm->SaveToFile(_bstr_t(lpszFileName),adSaveCreateOverWrite);
  226. }
  227. }
  228. void CImageDBView::OnDraw(CDC* pDC) 
  229. {
  230. // TODO: Add your specialized code here and/or call the base class
  231. //DrawPhoto();
  232. if(m_bDraw==1)
  233. DrawPhotoFromFile(m_FileName);
  234. if(m_bDraw==2)
  235. DrawPhotoFromDB();
  236. }
  237. void CImageDBView::OnButton3() 
  238. {
  239. // TODO: Add your control notification handler code here
  240. CFileDialog FileDlg(TRUE,"*.jpg",NULL,OFN_HIDEREADONLY,"图片文件(*.jpg; *.bmp)|*.jpg; *.bmp||",NULL);
  241. FileDlg.DoModal();
  242. CString filename;
  243. filename=FileDlg.GetPathName();
  244. if (filename.IsEmpty())
  245. return;
  246. CFileFind findfile;
  247. int bExist=findfile.FindFile(filename);
  248. if (!bExist)
  249. {
  250. AfxMessageBox("您要打开的文件不存在!");
  251. return;
  252. }
  253. if (m_bImageOpen==FALSE)
  254. m_bImageOpen=TRUE;
  255. m_FileName=filename;
  256. m_bDraw=1;
  257. DrawPhotoFromFile(m_FileName);
  258. }
  259. void CImageDBView::DrawPhotoFromFile(CString FileName)
  260. {
  261. IStream *pStm; 
  262. CFile file; 
  263. if (file.Open(FileName,CFile::modeRead))
  264. HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,file.GetLength()); 
  265. LPVOID pvData=NULL; 
  266. if (hGlobal!=NULL) 
  267. if ((pvData = GlobalLock(hGlobal)) != NULL) 
  268. file.ReadHuge(pvData,file.GetLength()); 
  269. GlobalUnlock(hGlobal); 
  270. CreateStreamOnHGlobal(pvData, TRUE, &pStm); 
  271. }  //使用void *pBuf=malloc(file.GetLength())代替hGlobal的方法不行,不知为何???
  272. IPicture* pPic;
  273. if(FAILED(OleLoadPicture(pStm,file.GetLength(),TRUE,IID_IPicture,(LPVOID*)&pPic))) 
  274. AfxMessageBox("从流中装载图像失败!");
  275. else
  276. {
  277. OLE_XSIZE_HIMETRIC pWidth;
  278. OLE_YSIZE_HIMETRIC pHeight;
  279. pPic->get_Width(&pWidth);
  280. pPic->get_Height(&pHeight);
  281. CDC *pDC=GetDC();
  282. CRect rt;
  283. GetDlgItem(IDC_PHOTO)->GetWindowRect(rt);
  284. ScreenToClient(rt);
  285. if(FAILED(pPic->Render(*pDC,rt.left+2,rt.top+2,rt.Width()-5,rt.Height()-5,0,pHeight,pWidth,-pHeight,NULL))) 
  286. AfxMessageBox( "图像渲染失败!"); 
  287. //////////////////////////////////////////////////////////////////////////
  288. /*可以根据图片大小显示
  289. double fx,fy;
  290. fx = (double) pDC->GetDeviceCaps(HORZRES)*(double)pWidth/
  291. ((double)pDC->GetDeviceCaps(HORZSIZE)*300.0);  //改变数值可以改变图片显示的比例
  292. fy = (double)pDC->GetDeviceCaps(VERTRES)*(double)pHeight/
  293. ((double)pDC->GetDeviceCaps(VERTSIZE)*300.0); 
  294. pPic->Render(*pDC,400,0,(LONG)fx,(LONG)fy,0,pHeight,pWidth,-pHeight,NULL);
  295. *//////////////////////////////////////////////////////////////////////////
  296. }
  297. }
  298. void CImageDBView::DrawPhotoFromDB()
  299. {
  300. //将_StreamPtr *pstream 转为IStream *ps,因为OleLoadPicture需要的流是IStream流,_StreamPtr流不行。
  301. //LPVOID pvData = NULL; 
  302. IStream *ps; 
  303. void* pBuf=malloc(size_t(m_pStream->GetSize()));
  304. SafeArrayAccessData(m_varBLOB.parray,(void **) &pBuf);
  305. SafeArrayUnaccessData(m_varBLOB.parray);
  306. CreateStreamOnHGlobal(pBuf, TRUE, &ps);
  307. //不能释放内存pBuf,因为ps只是个流,不具备pBuf的全部信息,离开pBuf,就不行了。
  308. IPicture *pPic; 
  309. if(SUCCEEDED(OleLoadPicture(ps,m_pStream->GetSize() ,TRUE,IID_IPicture,(LPVOID*)&pPic))) 
  310. {
  311. OLE_XSIZE_HIMETRIC hmWidth; 
  312. OLE_YSIZE_HIMETRIC hmHeight; 
  313. pPic->get_Width(&hmWidth); 
  314. pPic->get_Height(&hmHeight); 
  315. CRect rt;
  316. GetDlgItem(IDC_PHOTO)->GetWindowRect(rt);
  317. ScreenToClient(rt);
  318. CDC *pDC=GetDC();
  319. if(FAILED(pPic->Render(*pDC,rt.left+2,rt.top+2,rt.Width()-5,rt.Height()-5,0,hmHeight,hmWidth,-hmHeight,NULL))) 
  320. AfxMessageBox( "图像渲染失败!"); 
  321. }
  322. }