ShowSelPhoto2.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // ShowSelPhoto2.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ylgl.h"
  5. #include "ShowSelPhoto2.h"
  6. #include "ShowPic22.h"
  7. #include "JPEG.h"
  8. #include "ComparePhoto.h"
  9. #define THUMBNAIL_WIDTH 100
  10. #define THUMBNAIL_HEIGHT 90
  11. ShowSelPhoto2::ShowSelPhoto2(CWnd* pParent /*=NULL*/) : CDialog(ShowSelPhoto2::IDD, pParent)
  12. {
  13. m_bTerminate2=false;
  14. m_bRunning2=false;
  15. m_hThread2 = NULL;
  16. }
  17. void ShowSelPhoto2::DoDataExchange(CDataExchange* pDX)
  18. {
  19. CDialog::DoDataExchange(pDX);
  20. DDX_Control(pDX, IDC_LIST4, m_List4);
  21. }
  22. BEGIN_MESSAGE_MAP(ShowSelPhoto2, CDialog)
  23. ON_WM_DESTROY()
  24. ON_NOTIFY(NM_DBLCLK, IDC_LIST4, OnDblclkList4)
  25. END_MESSAGE_MAP()
  26. BOOL ShowSelPhoto2::OnInitDialog()
  27. {
  28. CDialog::OnInitDialog();
  29. CRect rc;
  30. rc=m_rc;
  31. rc.right -=m_rc.Width ()*0.5;
  32. MoveWindow(rc);
  33. GetClientRect(rc);
  34. m_List4.MoveWindow (rc);
  35. m_ImageListThumb2.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1);
  36. m_List4.SetImageList(&m_ImageListThumb2, LVSIL_NORMAL);
  37. m_path=g_path1+"\\";
  38. m_path+=m_id;
  39. m_path +="\\";
  40. LoadPhotos();
  41. return TRUE;
  42. }
  43. unsigned __stdcall LoadThumbNail10(LPVOID lpParam)
  44. {
  45. ShowSelPhoto2 *pDlg=(ShowSelPhoto2*)lpParam;
  46. CImageList* pImgList=&pDlg->m_ImageListThumb2;
  47. CListCtrl *ListCtrl=&pDlg->m_List4;
  48. try
  49. {
  50. // reset our image list
  51. for(int i=0; i<pImgList->GetImageCount(); i++)
  52. pImgList->Remove(i);
  53. // remove all items from list view
  54. ListCtrl->DeleteAllItems();
  55. pImgList->SetImageCount(pDlg->m_vFileName2.size());
  56. TCHAR path[MAX_PATH];
  57. vector<CString>::iterator iter;
  58. // Set redraw to FALSE to avoid flickering during adding new items
  59. ListCtrl->SetRedraw(FALSE);
  60. int nIndex=0;
  61. CString str, spath;
  62. for(iter=pDlg->m_vFileName2.begin(); iter!=pDlg->m_vFileName2.end() && pDlg->m_bTerminate2!=true; iter++, nIndex++)
  63. {
  64. ListCtrl->InsertItem(nIndex, pDlg->m_patharray2.ElementAt (nIndex), nIndex);
  65. }
  66. ListCtrl->SetRedraw(TRUE);
  67. ListCtrl->Invalidate();
  68. const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
  69. int XDest, YDest, nDestWidth, nDestHeight;
  70. nIndex=0;
  71. SolidBrush whitebrush(Color(255,255,255,255));
  72. for(iter=pDlg->m_vFileName2.begin(); iter!=pDlg->m_vFileName2.end() && pDlg->m_bTerminate2!=true; iter++, nIndex++)
  73. {
  74. // Load Image File
  75. Image *image=NULL;
  76. Bitmap *pBmp=NULL;
  77. str=*iter;
  78. spath=str.Left (str.ReverseFind ('\\')+1);
  79. spath+="s";
  80. spath+=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
  81. if (PathFileExists(spath))
  82. ::LoadImageFromBuf (&image, spath);
  83. else
  84. ::LoadImageFromBuf (&image, str);
  85. int orientation=GetOrientation(image);
  86. if(orientation==8)
  87. image->RotateFlip( Rotate270FlipNone );
  88. else if(orientation==6)
  89. image->RotateFlip( Rotate90FlipNone );
  90. if(image->GetWidth()==0)
  91. continue;
  92. // Calculate Rect to fit to canvas
  93. const float fImgRatio=(float)image->GetHeight()/image->GetWidth();
  94. if(fImgRatio > fRatio)
  95. {
  96. nDestWidth=THUMBNAIL_HEIGHT/fImgRatio;
  97. XDest=(THUMBNAIL_WIDTH-nDestWidth)/2;
  98. YDest=0;
  99. nDestHeight=THUMBNAIL_HEIGHT;
  100. }
  101. else
  102. {
  103. XDest=0;
  104. nDestWidth=THUMBNAIL_WIDTH;
  105. nDestHeight=THUMBNAIL_WIDTH*fImgRatio;
  106. YDest=(THUMBNAIL_HEIGHT-nDestHeight)/2;
  107. }
  108. pBmp=new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat24bppRGB );
  109. Graphics graph2(pBmp);
  110. graph2.Clear(Color(255,192,192,192));
  111. graph2.FillRectangle (&whitebrush,1, 1, THUMBNAIL_WIDTH-2, THUMBNAIL_HEIGHT-2);
  112. Graphics graph(pBmp);
  113. Rect desRect(XDest, YDest, nDestWidth, nDestHeight);
  114. // Draw Image
  115. graph.DrawImage(image, desRect, 0,0,image->GetWidth(),image->GetHeight(),UnitPixel);
  116. delete image;
  117. // Attach to Bitmap and Replace image in CImageList
  118. CBitmap bitmap;
  119. HBITMAP hbmp;
  120. pBmp->GetHBITMAP(Color(255,255,255,255), &hbmp);
  121. bitmap.Attach(hbmp);
  122. pImgList->Replace(nIndex, &bitmap, NULL);
  123. delete pBmp;
  124. // Redraw only a current item for removing flickering and fast speed.
  125. ListCtrl->RedrawItems(nIndex, nIndex);
  126. // Release used DC and Object
  127. }
  128. ListCtrl->Invalidate();
  129. pDlg->m_bRunning2=false;
  130. pDlg->m_bTerminate2=false;
  131. _endthreadex( 0 );
  132. return 0;
  133. }
  134. catch(...)
  135. {
  136. ListCtrl->Invalidate();
  137. pDlg->m_bRunning2=false;
  138. pDlg->m_bTerminate2=false;
  139. _endthreadex( 0 );
  140. }
  141. }
  142. void ShowSelPhoto2::OnDestroy()
  143. {
  144. TerminateThread2();
  145. CDialog::OnDestroy();
  146. }
  147. void ShowSelPhoto2::LoadPhotos()
  148. {
  149. m_patharray1.RemoveAll ();
  150. m_path.MakeLower ();
  151. CStringArray dirarray;
  152. CString str;
  153. if(1)
  154. {
  155. using namespace helper_coffs;
  156. ffsco o;
  157. o.dirs(1);
  158. o.find(LPCSTR(m_path), LPCSTR("*.*"));
  159. ffsco::typeT coo;
  160. ffsco::typeT::iterator it;
  161. coo = o.co_dir();
  162. for (it = coo.begin();
  163. coo.end() != it;
  164. it ++)
  165. {
  166. str=(*it).c_str();
  167. if(FindArray(&dirarray, str)==-1)
  168. {
  169. dirarray.Add (str);
  170. }
  171. }
  172. }
  173. if(1)
  174. {
  175. CString spath,name;
  176. for(int i=0; i<dirarray.GetSize (); i++)
  177. {
  178. using namespace helper_coffs;
  179. ffsco o;
  180. o.dirs(1);
  181. o.find(LPCSTR(dirarray.ElementAt (i)), LPCSTR("*.jpg"));
  182. ffsco::typeT coo;
  183. ffsco::typeT::iterator it;
  184. coo = o.co_file();
  185. for (it = coo.begin(); coo.end() != it; it++)
  186. {
  187. str=(*it).c_str();
  188. if(str.GetAt (str.ReverseFind ('\\')+1)=='s')continue;
  189. if(str.GetAt (str.ReverseFind ('\\')+1)=='m')continue;
  190. str.MakeLower ();
  191. m_patharray1.Add (str);
  192. {
  193. spath.Empty ();
  194. if(str.Left (str.ReverseFind ('\\')+1)!=m_path)
  195. {
  196. spath=str.Left (str.ReverseFind ('\\')+1);
  197. spath= spath.Right (spath.GetLength ()-m_path.GetLength ());
  198. }
  199. name=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
  200. name=spath+name.Left (name.GetLength ()-4);
  201. m_patharray2.Add (name);
  202. }
  203. str.Replace ("¿Í»§Ô­Æ¬", "ÐÞºÃµÄÆ¬");
  204. int pos1=str.Find (m_id);
  205. pos1+=m_id.GetLength ();
  206. int pos2=str.ReverseFind ('\\');
  207. str=str.Left (pos1)+str.Right (str.GetLength ()-pos2);
  208. if(::PathFileExists (str))
  209. {
  210. m_patharray1.Add (str);
  211. m_patharray2.Add (name+"-ÐÞÆ¬");
  212. }
  213. }
  214. }
  215. }
  216. if(m_bRunning2)return;
  217. CStringArray *pArray=&m_patharray1;
  218. m_vFileName2.clear();
  219. for(int i=0; i<pArray->GetSize (); i++)
  220. {
  221. CString filePath=pArray->ElementAt (i);
  222. filePath.MakeLower();
  223. m_vFileName2.push_back(filePath);
  224. }
  225. TerminateThread2();
  226. m_hThread2 = (HANDLE)_beginthreadex(NULL,0, LoadThumbNail10,(LPVOID)this,0/* CREATE_SUSPENDED*/,&m_dwThreadID2);
  227. m_bRunning2=true;
  228. }
  229. BOOL ShowSelPhoto2::TerminateThread2()
  230. {
  231. if ( !m_bRunning2 )
  232. return TRUE;
  233. m_bTerminate2=true;
  234. for( ; ; )
  235. {
  236. if ( ::WaitForSingleObject(m_hThread2, 0) == WAIT_OBJECT_0 )
  237. break;
  238. MSG msg;
  239. while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
  240. {
  241. if (!AfxGetApp()->PumpMessage())
  242. break;
  243. }
  244. }
  245. ::CloseHandle(m_hThread2);
  246. return TRUE;
  247. }
  248. void ShowSelPhoto2::OnDblclkList4(NMHDR* pNMHDR, LRESULT* pResult)
  249. {
  250. POSITION pos;
  251. pos=m_List4.GetFirstSelectedItemPosition();
  252. if(pos==NULL)
  253. {
  254. return;
  255. }
  256. int iItem=m_List4.GetNextSelectedItem(pos);
  257. ShowPic22 dlg;
  258. dlg.m_pos=iItem;
  259. dlg.m_pPathArray=&m_patharray1;
  260. dlg.DoModal ();
  261. *pResult = 0;
  262. }
  263. BOOL ShowSelPhoto2::PreTranslateMessage(MSG* pMsg)
  264. {
  265. if(pMsg->message==WM_KEYDOWN)
  266. {
  267. switch (pMsg->wParam)
  268. {
  269. case VK_F1:
  270. {
  271. ShowWindow(0);
  272. }
  273. break;
  274. case VK_RETURN:
  275. {
  276. POSITION pos;
  277. pos=m_List4.GetFirstSelectedItemPosition();
  278. if(pos==NULL)
  279. {
  280. return 1;
  281. }
  282. CArray<int,int>posarray;
  283. int iItem=m_List4.GetNextSelectedItem(pos);
  284. posarray.Add (iItem);
  285. while (pos)
  286. {
  287. int iItem=m_List4.GetNextSelectedItem(pos);
  288. posarray.Add (iItem);
  289. if(posarray.GetSize ()>1)break;
  290. }
  291. if(posarray.GetSize ()>=2)
  292. {
  293. CComparePhoto dlg;
  294. dlg.m_path1=m_patharray1.ElementAt (posarray.ElementAt(0));
  295. dlg.m_path2=m_patharray1.ElementAt (posarray.ElementAt(1));
  296. dlg.DoModal();
  297. return 1;
  298. }
  299. }
  300. return 1;
  301. }
  302. }
  303. return CDialog::PreTranslateMessage(pMsg);
  304. }