ShowSelPhoto2.cpp 8.6 KB

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