// ShowSelPhoto2.cpp : implementation file // #include "stdafx.h" #include "ylgl.h" #include "ShowSelPhoto2.h" #include "ShowPic22.h" #include "JPEG.h" #include "ComparePhoto.h" #define THUMBNAIL_WIDTH 100 #define THUMBNAIL_HEIGHT 90 ShowSelPhoto2::ShowSelPhoto2(CWnd* pParent /*=NULL*/) : CDialog(ShowSelPhoto2::IDD, pParent) { m_bTerminate2=false; m_bRunning2=false; m_hThread2 = NULL; } void ShowSelPhoto2::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_LIST4, m_List4); } BEGIN_MESSAGE_MAP(ShowSelPhoto2, CDialog) ON_WM_DESTROY() ON_NOTIFY(NM_DBLCLK, IDC_LIST4, OnDblclkList4) END_MESSAGE_MAP() BOOL ShowSelPhoto2::OnInitDialog() { CDialog::OnInitDialog(); CRect rc; rc=m_rc; rc.right -=m_rc.Width ()*0.5; MoveWindow(rc); GetClientRect(rc); m_List4.MoveWindow (rc); m_ImageListThumb2.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1); m_List4.SetImageList(&m_ImageListThumb2, LVSIL_NORMAL); m_path=g_path1+"\\"; m_path+=m_id; m_path +="\\"; LoadPhotos(); return TRUE; } unsigned __stdcall LoadThumbNail10(LPVOID lpParam) { ShowSelPhoto2 *pDlg=(ShowSelPhoto2*)lpParam; CImageList* pImgList=&pDlg->m_ImageListThumb2; CListCtrl *ListCtrl=&pDlg->m_List4; try { // reset our image list for(int i=0; iGetImageCount(); i++) pImgList->Remove(i); // remove all items from list view ListCtrl->DeleteAllItems(); pImgList->SetImageCount(pDlg->m_vFileName2.size()); TCHAR path[MAX_PATH]; vector::iterator iter; // Set redraw to FALSE to avoid flickering during adding new items ListCtrl->SetRedraw(FALSE); int nIndex=0; CString str, spath; for(iter=pDlg->m_vFileName2.begin(); iter!=pDlg->m_vFileName2.end() && pDlg->m_bTerminate2!=true; iter++, nIndex++) { ListCtrl->InsertItem(nIndex, pDlg->m_patharray2.ElementAt (nIndex), nIndex); } ListCtrl->SetRedraw(TRUE); ListCtrl->Invalidate(); const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH; int XDest, YDest, nDestWidth, nDestHeight; nIndex=0; SolidBrush whitebrush(Color(255,255,255,255)); for(iter=pDlg->m_vFileName2.begin(); iter!=pDlg->m_vFileName2.end() && pDlg->m_bTerminate2!=true; iter++, nIndex++) { // Load Image File Image *image=NULL; Bitmap *pBmp=NULL; str=*iter; spath=str.Left (str.ReverseFind ('\\')+1); spath+="s"; spath+=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1); if (PathFileExists(spath)) ::LoadImageFromBuf (&image, spath); else ::LoadImageFromBuf (&image, str); int orientation=GetOrientation(image); if(orientation==8) image->RotateFlip( Rotate270FlipNone ); else if(orientation==6) image->RotateFlip( Rotate90FlipNone ); if(image->GetWidth()==0) continue; // Calculate Rect to fit to canvas const float fImgRatio=(float)image->GetHeight()/image->GetWidth(); if(fImgRatio > fRatio) { nDestWidth=THUMBNAIL_HEIGHT/fImgRatio; XDest=(THUMBNAIL_WIDTH-nDestWidth)/2; YDest=0; nDestHeight=THUMBNAIL_HEIGHT; } else { XDest=0; nDestWidth=THUMBNAIL_WIDTH; nDestHeight=THUMBNAIL_WIDTH*fImgRatio; YDest=(THUMBNAIL_HEIGHT-nDestHeight)/2; } pBmp=new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat24bppRGB ); Graphics graph2(pBmp); graph2.Clear(Color(255,192,192,192)); graph2.FillRectangle (&whitebrush,1, 1, THUMBNAIL_WIDTH-2, THUMBNAIL_HEIGHT-2); Graphics graph(pBmp); Rect desRect(XDest, YDest, nDestWidth, nDestHeight); // Draw Image graph.DrawImage(image, desRect, 0,0,image->GetWidth(),image->GetHeight(),UnitPixel); delete image; // Attach to Bitmap and Replace image in CImageList CBitmap bitmap; HBITMAP hbmp; pBmp->GetHBITMAP(Color(255,255,255,255), &hbmp); bitmap.Attach(hbmp); pImgList->Replace(nIndex, &bitmap, NULL); delete pBmp; // Redraw only a current item for removing flickering and fast speed. ListCtrl->RedrawItems(nIndex, nIndex); // Release used DC and Object } ListCtrl->Invalidate(); pDlg->m_bRunning2=false; pDlg->m_bTerminate2=false; _endthreadex( 0 ); return 0; } catch(...) { ListCtrl->Invalidate(); pDlg->m_bRunning2=false; pDlg->m_bTerminate2=false; _endthreadex( 0 ); } } void ShowSelPhoto2::OnDestroy() { TerminateThread2(); CDialog::OnDestroy(); } void ShowSelPhoto2::LoadPhotos() { m_patharray1.RemoveAll (); m_path.MakeLower (); CStringArray dirarray; CString str; if(1) { using namespace helper_coffs; ffsco o; o.dirs(1); o.find(LPCSTR(m_path), LPCSTR("*.*")); ffsco::typeT coo; ffsco::typeT::iterator it; coo = o.co_dir(); for (it = coo.begin(); coo.end() != it; it ++) { str=(*it).c_str(); if(FindArray(&dirarray, str)==-1) { dirarray.Add (str); } } } if(1) { CString spath,name; for(int i=0; iGetSize (); i++) { CString filePath=pArray->ElementAt (i); filePath.MakeLower(); m_vFileName2.push_back(filePath); } TerminateThread2(); m_hThread2 = (HANDLE)_beginthreadex(NULL,0, LoadThumbNail10,(LPVOID)this,0/* CREATE_SUSPENDED*/,&m_dwThreadID2); m_bRunning2=true; } BOOL ShowSelPhoto2::TerminateThread2() { if ( !m_bRunning2 ) return TRUE; m_bTerminate2=true; for( ; ; ) { if ( ::WaitForSingleObject(m_hThread2, 0) == WAIT_OBJECT_0 ) break; MSG msg; while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) { if (!AfxGetApp()->PumpMessage()) break; } } ::CloseHandle(m_hThread2); return TRUE; } void ShowSelPhoto2::OnDblclkList4(NMHDR* pNMHDR, LRESULT* pResult) { POSITION pos; pos=m_List4.GetFirstSelectedItemPosition(); if(pos==NULL) { return; } int iItem=m_List4.GetNextSelectedItem(pos); ShowPic22 dlg; dlg.m_pos=iItem; dlg.m_pPathArray=&m_patharray1; dlg.DoModal (); *pResult = 0; } BOOL ShowSelPhoto2::PreTranslateMessage(MSG* pMsg) { if(pMsg->message==WM_KEYDOWN) { switch (pMsg->wParam) { case VK_F1: { ShowWindow(0); } break; case VK_RETURN: { POSITION pos; pos=m_List4.GetFirstSelectedItemPosition(); if(pos==NULL) { return 1; } CArrayposarray; int iItem=m_List4.GetNextSelectedItem(pos); posarray.Add (iItem); while (pos) { int iItem=m_List4.GetNextSelectedItem(pos); posarray.Add (iItem); if(posarray.GetSize ()>1)break; } if(posarray.GetSize ()>=2) { CComparePhoto dlg; dlg.m_path1=m_patharray1.ElementAt (posarray.ElementAt(0)); dlg.m_path2=m_patharray1.ElementAt (posarray.ElementAt(1)); dlg.DoModal(); return 1; } } return 1; } } return CDialog::PreTranslateMessage(pMsg); }