// ShowCut.cpp : implementation file // #include "stdafx.h" #include "ylgl.h" #include "ShowCut.h" #include "JPEG.h" #include "MyLock.h" #include "ShowPhoto.h" #define THUMBNAIL_WIDTH 100 #define THUMBNAIL_HEIGHT 90 ///////////////////////////////////////////////////////////////////////////// // ShowCut dialog IDC_BU Tdelall ShowCut::ShowCut(CWnd* pParent /*=NULL*/) : CDialog(ShowCut::IDD, pParent) { //{{AFX_DATA_INIT(ShowCut) //}}AFX_DATA_INIT m_bTerminate=false; m_bRunning=false; m_hThread = NULL; m_mode=0; } void ShowCut::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(ShowCut) DDX_Control(pDX, IDC_LIST1, m_List1); DDX_Control(pDX, IDC_COMBO1, m_combo1); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(ShowCut, CDialog) //{{AFX_MSG_MAP(ShowCut) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTdelall, OnBUTdelall) ON_BN_CLICKED(IDC_BUTdel, OnBUTdel) ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // ShowCut message handlers BOOL ShowCut::OnInitDialog() { CDialog::OnInitDialog(); CRect rc2; GetWindowRect(rc2); ::MoveWindow(m_hWnd,g_rc.left,g_rc.top,g_rc.Width(),g_rc.Height(),TRUE); EnumChildWindows(m_hWnd,(WNDENUMPROC)EnumChildProc,0); GetClientRect(rc2); if(m_mode==0) rc2.top +=35*g_fscaley; else { GetDlgItem(IDC_BUTdelall)->ShowWindow(0); GetDlgItem(IDC_BUTdel)->ShowWindow(0); GetDlgItem(IDOK)->ShowWindow(0); } m_List1.MoveWindow (rc2); int right=rc2.right ; GetDlgItem(IDOK)->GetWindowRect(rc2); ScreenToClient(rc2); int hei=rc2.Height (); int dtx=right-rc2.right-5 ; rc2.top =5; rc2.bottom =rc2.top +hei; rc2.left += dtx; rc2.right +=dtx; GetDlgItem(IDOK)->MoveWindow(rc2); GetDlgItem(IDC_BUTdel)->GetWindowRect(rc2); ScreenToClient(rc2); rc2.top =5; rc2.bottom =rc2.top +hei; rc2.left += dtx; rc2.right +=dtx; GetDlgItem(IDC_BUTdel)->MoveWindow(rc2); GetDlgItem(IDC_BUTdelall)->GetWindowRect(rc2); ScreenToClient(rc2); rc2.top =5; rc2.bottom =rc2.top +hei; rc2.left += dtx; rc2.right +=dtx; GetDlgItem(IDC_BUTdelall)->MoveWindow(rc2); if(GetSize()==0) { CDialog::OnCancel (); return 0; } if(g_bAllBranch) { CString branchpath=GetBranchPhotoPath(m_branch); m_path=g_path1+branchpath+"\\"; } else m_path=g_path1+"\\"; m_path+=m_id; m_path +="\\"; m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1); m_List1.SetImageList(&m_ImageListThumb, LVSIL_NORMAL); LoadPhotos(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } unsigned __stdcall LoadThumbNail15(LPVOID lpParam) { try { ShowCut *pDlg=(ShowCut*)lpParam; CListCtrl *ListCtrl=&pDlg->m_List1; CImageList* pImgList=&pDlg->m_ImageListThumb; // 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_vFileName.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_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=true; iter++, nIndex++) { /* str=*iter; spath.Empty (); if(str.Left (str.ReverseFind ('\\')+1)!=pDlg->m_path) { spath=str.Left (str.ReverseFind ('\\')+1); spath= spath.Right (spath.GetLength ()-pDlg->m_path.GetLength ()); } str=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1); str=spath+str.Left (str.GetLength ()-4);*/ ListCtrl->InsertItem(nIndex, pDlg->m_titlearray.ElementAt (nIndex), nIndex); // ListCtrl->InsertItem(nIndex, str, 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_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=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(::CheckFileExist (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); float scale=(float)nDestWidth/(float)image->GetWidth(); int pos=pDlg->FindArray2 (pDlg->m_titlearray.ElementAt (nIndex) ); if(pos!=-1) { int nIndex=pos; CRect rc=pDlg->m_rcarray.ElementAt (nIndex); int wid=pDlg->m_widtharray.ElementAt (nIndex).left; int hei=pDlg->m_widtharray.ElementAt (nIndex).top; scale*=(float)image->GetWidth()/(float)wid; rc.left *=scale; rc.right *=scale; rc.top *=scale; rc.bottom *=scale; rc.left +=XDest ; rc.right +=XDest; rc.top +=YDest ; rc.bottom +=YDest ; Pen blackPen(Color(255, 255, 0, 0), 1); graph.DrawRectangle(&blackPen, rc.left, rc.top, rc.Width (), rc.Height ()); } 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_bRunning=false; pDlg->m_bTerminate=false; _endthreadex( 0 ); return 0; } catch(...) { } } void ShowCut::LoadThumbImages() { TerminateThread(); m_hThread = (HANDLE)_beginthreadex(NULL,0, LoadThumbNail15,(LPVOID)this,0/* CREATE_SUSPENDED*/,&m_dwThreadID); m_bRunning=true; } BOOL ShowCut::TerminateThread() { if ( !m_bRunning ) return TRUE; m_bTerminate=true; for( ; ; ) { if ( ::WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0 ) break; MSG msg; while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) { if (!AfxGetApp()->PumpMessage()) break; } } ::CloseHandle(m_hThread); return TRUE; } void ShowCut::OnDestroy() { TerminateThread(); CDialog::OnDestroy(); // TODO: Add your message handler code here } void ShowCut::LoadPhotos() { if(m_bRunning)return; m_patharray1.RemoveAll (); m_path.MakeLower (); CStringArray dirarray; CString str,path2,no; 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(); str.MakeLower(); if(str==m_path)continue; if(FindArray(&dirarray, str)==-1) { dirarray.Add (str); } } } if(1) { for(int i=0; iGetSize (); i++) { CString filePath=pArray->ElementAt (i); filePath.MakeLower(); m_vFileName.push_back(filePath); } LoadThumbImages(); } void ShowCut::OnBUTdelall() { if(AfxMessageBox("清除裁剪后将无法恢复, 确认清除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return; CString sql; sql.Format ("update dindan set size='' where id='%s'", m_id); g_sendhead.bsql=1; if(m_branch!="" && g_bAllBranch) { g_branchip=GetIP(m_branch); g_pMainWnd->OnDisconnect(); g_branchname=m_branch; g_bBranchModify=1; g_pMainWnd->ProcessChatMessageRequest2(sql); g_pMainWnd->OnDisconnect();g_bBranchModify=0; g_branchip=g_branchname=""; } else g_pMainWnd->ProcessChatMessageRequest2(sql); if(g_bSendOK==0)return; AfxMessageBox("保存成功!"); GetSize(); LoadPhotos(); } void ShowCut::OnBUTdel() { // TODO: Add your control notification handler code here POSITION pos; pos=m_List1.GetFirstSelectedItemPosition(); if(pos==NULL) { AfxMessageBox("请先选中您要清除裁剪的照片!", MB_ICONINFORMATION); return; } if(AfxMessageBox("清除裁剪后将无法恢复, 确认清除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return; int iItem=m_List1.GetNextSelectedItem(pos); CString title = m_List1.GetItemText (iItem, 0); CString no=title.Left (title.Find ("\r")); CString name=title.Right (title.GetLength ()-title.ReverseFind ('\n')-1); { CString header=no+":"+name+","; int pos=m_size.Find (header); if(pos!=-1) { CString left,right; left=m_size.Left (pos); m_size=m_size.Right (m_size.GetLength ()-pos-header.GetLength ()); pos=m_size.Find (";"); right=m_size.Right (m_size.GetLength ()-pos-1); m_size=left+right; } } CString sql; sql.Format ("update dindan set size='%s' where id='%s'", m_size, m_id); g_sendhead.bsql=1; if(m_branch!="" && g_bAllBranch) { g_branchip=GetIP(m_branch); g_pMainWnd->OnDisconnect(); g_branchname=m_branch; g_bBranchModify=1; g_pMainWnd->ProcessChatMessageRequest2(sql); g_pMainWnd->OnDisconnect(); g_bBranchModify=0; g_branchip=g_branchname=""; } else g_pMainWnd->ProcessChatMessageRequest2(sql); if(g_bSendOK==0)return; AfxMessageBox("保存成功!"); GetSize(); LoadPhotos(); } void ShowCut::AddSize(CString size) { int pos=size.Find (":"); if(pos!=-1) { CString str; CRect rc; str=size.Left (pos); m_noarray.Add (str); str=size.Right (size.GetLength ()-pos-1); pos=str.Find (","); m_namearray.Add(str.Left (pos));str.Delete (0, pos+1); pos=str.Find (","); rc.left=atoi(str.Left (pos));str.Delete (0, pos+1); pos=str.Find (","); rc.top=atoi(str.Left (pos));str.Delete (0, pos+1); m_widtharray.Add (rc); pos=str.Find (","); rc.left=atoi(str.Left (pos));str.Delete (0, pos+1); pos=str.Find (","); rc.top=atoi(str.Left (pos));str.Delete (0, pos+1); pos=str.Find (","); rc.right=atoi(str.Left (pos));str.Delete (0, pos+1); rc.bottom=atoi(str); m_rcarray.Add (rc); } } BOOL ShowCut::GetSize() { g_sendhead.bsql=0; g_sendhead.code[0]=11; g_sendhead.tabcount=1; CString filter="id='"+m_id+"';"; if(m_branch!="") { g_branchip=GetIP(m_branch); g_pMainWnd->OnDisconnect(); g_branchname=m_branch; g_bBranchModify=1; g_pMainWnd->ProcessChatMessageRequest2(filter); g_pMainWnd->OnDisconnect(); g_bBranchModify=0; g_branchip=g_branchname=""; } else g_pMainWnd->ProcessChatMessageRequest2(filter); if(g_bSendOK==0) { return 0; } CArraym_List1array; DataToArray(&m_List1array); CenterWindow(); m_noarray.RemoveAll(); m_widtharray.RemoveAll(); m_rcarray.RemoveAll(); if( m_List1array.GetSize () ) { m_size= m_List1array.ElementAt (0).ElementAt (60);// int pos=m_size.Find (";"); while(pos!=-1) { AddSize(m_size.Left (pos)); m_size=m_size.Right (m_size.GetLength ()-pos-1); pos=m_size.Find (";"); } m_size= m_List1array.ElementAt (0).ElementAt (60);return 1; } return 0; } int ShowCut::FindArray2(CString title) { CString no=title.Left (title.Find ("\r")); CString name=title.Right (title.GetLength ()-title.ReverseFind ('\n')-1); for(int i=0; iRotateFlip( Rotate270FlipNone ); else if(orientation==6) image->RotateFlip( Rotate90FlipNone ); if(image->GetWidth()==0) return; Graphics graph(image); // Rect desRect(XDest, YDest, nDestWidth, nDestHeight); // Draw Image // graph.DrawImage(image, desRect, 0,0,image->GetWidth(),image->GetHeight(),UnitPixel); float scale=1.0;//(float)nDestWidth/(float)image->GetWidth(); int pos2=FindArray2 (m_titlearray.ElementAt (iItem) ); if(pos2!=-1) { int nIndex=pos2; CRect rc=m_rcarray.ElementAt (nIndex); int wid=m_widtharray.ElementAt (nIndex).left; int hei=m_widtharray.ElementAt (nIndex).top; scale*=(float)image->GetWidth()/(float)wid; rc.left *=scale; rc.right *=scale; rc.top *=scale; rc.bottom *=scale; /* rc.left +=XDest ; rc.right +=XDest; rc.top +=YDest ; rc.bottom +=YDest ;*/ Pen blackPen(Color(255, 255, 0, 0), 1); graph.DrawRectangle(&blackPen, rc.left, rc.top, rc.Width (), rc.Height ()); ShowPhoto dlg; dlg.m_pImg=image; dlg.DoModal(); } delete image; *pResult = 0; }