ShowPic.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // ShowPic.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "LYFZIPManage.h"
  5. #include "ShowPic.h"
  6. #include "ShowPic22.h"
  7. #include "JPEG.h"
  8. #define THUMBNAIL_WIDTH 100
  9. #define THUMBNAIL_HEIGHT 90
  10. /////////////////////////////////////////////////////////////////////////////
  11. // ShowPic dialog
  12. ShowPic::ShowPic(CWnd* pParent /*=NULL*/)
  13. : CDialog(ShowPic::IDD, pParent)
  14. {
  15. //{{AFX_DATA_INIT(ShowPic)
  16. // NOTE: the ClassWizard will add member initialization here
  17. //}}AFX_DATA_INIT
  18. m_bTerminate=false;
  19. m_bRunning=false;
  20. m_hThread = NULL;
  21. m_mode=0;
  22. }
  23. void ShowPic::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(ShowPic)
  27. DDX_Control(pDX, IDC_LIST1, m_List1);
  28. DDX_Control(pDX, IDC_COMBO1, m_combo1);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(ShowPic, CDialog)
  32. //{{AFX_MSG_MAP(ShowPic)
  33. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
  34. ON_WM_DESTROY()
  35. ON_BN_CLICKED(IDC_BUTdelall, OnBUTdelall)
  36. ON_BN_CLICKED(IDC_BUTdel, OnBUTdel)
  37. //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // ShowPic message handlers
  41. BOOL ShowPic::OnInitDialog()
  42. {
  43. CDialog::OnInitDialog();
  44. CRect rc2;
  45. GetWindowRect(rc2);
  46. ::MoveWindow(m_hWnd,g_rc.left,g_rc.top,g_rc.Width(),g_rc.Height(),TRUE);
  47. EnumChildWindows(m_hWnd,(WNDENUMPROC)EnumChildProc,0);
  48. GetClientRect(rc2);
  49. rc2.top +=35*g_fscaley;
  50. m_List1.MoveWindow (rc2);
  51. GetDlgItem(IDC_BUTdelall)->GetWindowRect(rc2);
  52. int hei=rc2.Height ();
  53. ScreenToClient(rc2);
  54. rc2.top =5;
  55. rc2.bottom =rc2.top +hei;
  56. GetDlgItem(IDC_BUTdelall)->MoveWindow(rc2);
  57. GetDlgItem(IDC_BUTdel)->GetWindowRect(rc2);
  58. ScreenToClient(rc2);
  59. rc2.top =5;
  60. rc2.bottom =rc2.top +hei;
  61. GetDlgItem(IDC_BUTdel)->MoveWindow(rc2);
  62. GetDlgItem(IDOK)->GetWindowRect(rc2);
  63. ScreenToClient(rc2);
  64. rc2.top =5;
  65. rc2.bottom =rc2.top +hei;
  66. GetDlgItem(IDOK)->MoveWindow(rc2);
  67. if(m_mode==1)//拍照
  68. {
  69. GetDlgItem(IDC_BUTdelall)->EnableWindow(IsHasRights2new(34));
  70. GetDlgItem(IDC_BUTdel)->EnableWindow(IsHasRights2new(34));
  71. }
  72. else if(m_mode==2)//初修
  73. {
  74. GetDlgItem(IDC_BUTdelall)->EnableWindow(IsHasRights2new(35));
  75. GetDlgItem(IDC_BUTdel)->EnableWindow(IsHasRights2new(35));
  76. }
  77. else if(m_mode==3)//精修
  78. {
  79. GetDlgItem(IDC_BUTdelall)->EnableWindow(IsHasRights2new(36));
  80. GetDlgItem(IDC_BUTdel)->EnableWindow(IsHasRights2new(36));
  81. }
  82. /* CString path;
  83. // TODO: Add extra initialization here
  84. using namespace helper_coffs;
  85. ffsco o;
  86. o.dirs(0);
  87. o.find(LPCSTR(m_path), LPCSTR("*.jpg"));
  88. ffsco::typeT coo;
  89. ffsco::typeT::iterator it;
  90. coo = o.co_file();
  91. for (it = coo.begin();
  92. coo.end() != it;
  93. it ++)
  94. {
  95. path=(*it).c_str();
  96. if(path.GetAt (path.ReverseFind ('\\')+1)!='s')
  97. {
  98. path.MakeLower ();
  99. m_patharray1.Add (path);
  100. }
  101. }*/
  102. //////////
  103. LoadPhotos();
  104. return TRUE; // return TRUE unless you set the focus to a control
  105. // EXCEPTION: OCX Property Pages should return FALSE
  106. }
  107. void LoadImageFromFile(Image **img, CString path)
  108. {
  109. try
  110. {
  111. if(!::PathFileExists (path))return;
  112. if(*img)delete *img;
  113. *img=NULL;
  114. BSTR bstr= path.AllocSysString();
  115. *img= Image::FromFile(bstr);
  116. SysFreeString(bstr);
  117. }
  118. catch(...)
  119. {
  120. }
  121. }
  122. unsigned __stdcall LoadThumbNail(LPVOID lpParam)
  123. {
  124. try
  125. {
  126. ShowPic *pDlg=(ShowPic*)lpParam;
  127. CListCtrl *ListCtrl=&pDlg->m_List1;
  128. CImageList* pImgList=&pDlg->m_ImageListThumb;
  129. // reset our image list
  130. for(int i=0; i<pImgList->GetImageCount(); i++)
  131. pImgList->Remove(i);
  132. // remove all items from list view
  133. ListCtrl->DeleteAllItems();
  134. pImgList->SetImageCount(pDlg->m_vFileName.size());
  135. TCHAR path[MAX_PATH];
  136. vector<CString>::iterator iter;
  137. // Set redraw to FALSE to avoid flickering during adding new items
  138. ListCtrl->SetRedraw(FALSE);
  139. int nIndex=0;
  140. CString str, spath;
  141. for(iter=pDlg->m_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=true; iter++, nIndex++)
  142. {
  143. str=*iter;
  144. spath.Empty ();
  145. if(str.Left (str.ReverseFind ('\\')+1)!=pDlg->m_path)
  146. {
  147. spath=str.Left (str.ReverseFind ('\\')+1);
  148. spath= spath.Right (spath.GetLength ()-pDlg->m_path.GetLength ());
  149. }
  150. str=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
  151. str=spath+str.Left (str.GetLength ()-4);
  152. ListCtrl->InsertItem(nIndex, str, nIndex);
  153. }
  154. ListCtrl->SetRedraw(TRUE);
  155. ListCtrl->Invalidate();
  156. const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
  157. int XDest, YDest, nDestWidth, nDestHeight;
  158. nIndex=0;
  159. SolidBrush whitebrush(Color(255,255,255,255));
  160. for(iter=pDlg->m_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=true; iter++, nIndex++)
  161. {
  162. // Load Image File
  163. Image *image=NULL;
  164. Bitmap *pBmp=NULL;
  165. str=*iter;
  166. spath=str.Left (str.ReverseFind ('\\')+1);
  167. spath+="s";
  168. spath+=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
  169. if(::PathFileExists (spath))
  170. ::LoadImageFromBuf (&image, spath);
  171. else
  172. ::LoadImageFromBuf (&image, str);
  173. int orientation=GetOrientation(image);
  174. if(orientation==8)
  175. image->RotateFlip( Rotate270FlipNone );
  176. else if(orientation==6)
  177. image->RotateFlip( Rotate90FlipNone );
  178. if(image->GetWidth()==0)
  179. continue;
  180. // Calculate Rect to fit to canvas
  181. const float fImgRatio=(float)image->GetHeight()/image->GetWidth();
  182. if(fImgRatio > fRatio)
  183. {
  184. nDestWidth=THUMBNAIL_HEIGHT/fImgRatio;
  185. XDest=(THUMBNAIL_WIDTH-nDestWidth)/2;
  186. YDest=0;
  187. nDestHeight=THUMBNAIL_HEIGHT;
  188. }
  189. else
  190. {
  191. XDest=0;
  192. nDestWidth=THUMBNAIL_WIDTH;
  193. nDestHeight=THUMBNAIL_WIDTH*fImgRatio;
  194. YDest=(THUMBNAIL_HEIGHT-nDestHeight)/2;
  195. }
  196. pBmp=new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat24bppRGB );
  197. Graphics graph2(pBmp);
  198. graph2.Clear(Color(255,192,192,192));
  199. graph2.FillRectangle (&whitebrush,1, 1, THUMBNAIL_WIDTH-2, THUMBNAIL_HEIGHT-2);
  200. Graphics graph(pBmp);
  201. Rect desRect(XDest, YDest, nDestWidth, nDestHeight);
  202. // Draw Image
  203. graph.DrawImage(image, desRect, 0,0,image->GetWidth(),image->GetHeight(),UnitPixel);
  204. delete image;
  205. // Attach to Bitmap and Replace image in CImageList
  206. CBitmap bitmap;
  207. HBITMAP hbmp;
  208. pBmp->GetHBITMAP(Color(255,255,255,255), &hbmp);
  209. bitmap.Attach(hbmp);
  210. pImgList->Replace(nIndex, &bitmap, NULL);
  211. delete pBmp;
  212. // Redraw only a current item for removing flickering and fast speed.
  213. ListCtrl->RedrawItems(nIndex, nIndex);
  214. // Release used DC and Object
  215. }
  216. ListCtrl->Invalidate();
  217. pDlg->m_bRunning=false;
  218. pDlg->m_bTerminate=false;
  219. return 0;
  220. }
  221. catch(...)
  222. {
  223. }
  224. }
  225. void ShowPic::LoadThumbImages()
  226. {
  227. }
  228. BOOL ShowPic::TerminateThread()
  229. {
  230. if ( !m_bRunning )
  231. return TRUE;
  232. m_bTerminate=true;
  233. for( ; ; )
  234. {
  235. if ( ::WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0 )
  236. break;
  237. MSG msg;
  238. while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
  239. {
  240. if (!AfxGetApp()->PumpMessage())
  241. break;
  242. }
  243. }
  244. ::CloseHandle(m_hThread);
  245. return TRUE;
  246. }
  247. void ShowPic::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
  248. {
  249. // TODO: Add your control notification handler code here
  250. POSITION pos;
  251. pos=m_List1.GetFirstSelectedItemPosition();
  252. if(pos==NULL)
  253. {
  254. return;
  255. }
  256. int iItem=m_List1.GetNextSelectedItem(pos);
  257. ShowPic22 dlg;
  258. dlg.m_pos=iItem;
  259. dlg.m_pPathArray=&m_patharray1;
  260. dlg.DoModal ();
  261. *pResult = 0;
  262. }
  263. void ShowPic::OnDestroy()
  264. {
  265. CDialog::OnDestroy();
  266. TerminateThread();
  267. // TODO: Add your message handler code here
  268. }
  269. void DeleteDirectory(CString strDir)
  270. {
  271. try
  272. {
  273. // 首先删除文件及子文件夹
  274. CFileFind ff;
  275. BOOL bFound = ff.FindFile(strDir+"\\*", 0);
  276. while(bFound)
  277. {
  278. bFound = ff.FindNextFile();
  279. if(ff.GetFileName()=="."||ff.GetFileName()=="..")
  280. continue;
  281. // 去掉文件(夹)只读等属性
  282. SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
  283. if(ff.IsDirectory()) { // 递归删除子文件夹
  284. DeleteDirectory(ff.GetFilePath());
  285. RemoveDirectory(ff.GetFilePath());
  286. }
  287. else {
  288. DeleteFile(ff.GetFilePath()); // 删除文件
  289. }
  290. }
  291. ff.Close();
  292. // 然后删除该文件夹
  293. RemoveDirectory(strDir);
  294. }
  295. catch(...)
  296. {
  297. }
  298. // int SHFileOperation( LPSHFILEOPSTRUCT lpFileOp );
  299. }
  300. void ShowPic::LoadPhotos()
  301. {
  302. m_patharray1.RemoveAll ();
  303. m_path.MakeLower ();
  304. CStringArray dirarray;
  305. CString str;
  306. if(1)
  307. {
  308. using namespace helper_coffs;
  309. ffsco o;
  310. o.dirs(1);
  311. o.find(LPCSTR(m_path), LPCSTR("*.*"));
  312. ffsco::typeT coo;
  313. ffsco::typeT::iterator it;
  314. coo = o.co_dir();
  315. for (it = coo.begin();
  316. coo.end() != it;
  317. it ++)
  318. {
  319. str=(*it).c_str();
  320. if(FindArray(&dirarray, str)==-1)
  321. {
  322. dirarray.Add (str);
  323. }
  324. }
  325. }
  326. if(1)
  327. {
  328. for(int i=0; i<dirarray.GetSize (); i++)
  329. {
  330. using namespace helper_coffs;
  331. ffsco o;
  332. o.dirs(0);
  333. o.find(LPCSTR(dirarray.ElementAt (i)), LPCSTR("*.jpg"));
  334. ffsco::typeT coo;
  335. ffsco::typeT::iterator it;
  336. coo = o.co_file();
  337. for (it = coo.begin();
  338. coo.end() != it;
  339. it ++)
  340. {
  341. str=(*it).c_str();
  342. if(str.GetAt (str.ReverseFind ('\\')+1)=='s')continue;
  343. str.MakeLower ();
  344. m_patharray1.Add (str);
  345. }
  346. }
  347. }
  348. m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1);
  349. m_List1.SetImageList(&m_ImageListThumb, LVSIL_NORMAL);
  350. if(m_bRunning)return;
  351. CStringArray *pArray=&m_patharray1;
  352. m_vFileName.clear();
  353. for(int i=0; i<pArray->GetSize (); i++)
  354. {
  355. CString filePath=pArray->ElementAt (i);
  356. filePath.MakeLower();
  357. m_vFileName.push_back(filePath);
  358. }
  359. LoadThumbImages();
  360. }
  361. void ShowPic::OnBUTdelall()
  362. {
  363. /* static BOOL bmode=0;
  364. bmode=!bmode;
  365. CDC *pDC=GetDC();
  366. if(1)
  367. {
  368. Graphics graph(pDC->GetSafeHdc ());
  369. DWORD begin=::GetTickCount ();
  370. CString str;
  371. for(int i=0; i<m_patharray1.GetSize (); i++)
  372. {
  373. Image *img=NULL;
  374. ::LoadImageFromBuf(&img, m_patharray1.ElementAt (i));
  375. Image *m_pTemp=img->GetThumbnailImage(800,600, NULL, NULL);
  376. graph.DrawImage(m_pTemp, 0, 0);
  377. delete img;
  378. delete m_pTemp;
  379. }
  380. str.Format ("gdi+:%d, count:%d", ::GetTickCount ()-begin, m_patharray1.GetSize ());
  381. MessageBox(str);
  382. }
  383. else
  384. {
  385. CRect rc(0,0,100,100);
  386. DWORD begin=::GetTickCount ();
  387. CString str;
  388. for(int i=0; i<m_patharray1.GetSize (); i++)
  389. {
  390. CPicture pic;
  391. pic.Load (m_patharray1.ElementAt (i));
  392. pic.RenderBmp (pDC, rc);
  393. pic.UnloadPicture ();
  394. }
  395. str.Format ("jpg:%d, count:%d", ::GetTickCount ()-begin, m_patharray1.GetSize ());
  396. MessageBox(str);
  397. }
  398. ReleaseDC(pDC);
  399. return; */
  400. // TODO: Add your control notification handler code here
  401. if(AfxMessageBox("删除后将无法恢复, 确认删除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return;
  402. CString path=m_path;
  403. path.TrimRight ('\\');
  404. DeleteDirectory(path);
  405. LoadPhotos();
  406. }
  407. void ShowPic::OnBUTdel()
  408. {
  409. // TODO: Add your control notification handler code here
  410. POSITION pos;
  411. pos=m_List1.GetFirstSelectedItemPosition();
  412. if(pos==NULL)
  413. {
  414. AfxMessageBox("请先选中您要删除的照片!", MB_ICONINFORMATION);
  415. return;
  416. }
  417. if(AfxMessageBox("删除后将无法恢复, 确认删除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return;
  418. while (pos)
  419. {
  420. int iItem=m_List1.GetNextSelectedItem(pos);
  421. CString title=m_List1.GetItemText (iItem, 0);
  422. ::DeleteFile (m_path+title+".jpg");
  423. ::DeleteFile (m_path+"s"+title+".jpg");
  424. }
  425. LoadPhotos();
  426. }