// SetDesignNo.cpp : implementation file // #include "stdafx.h" #include "ylgl.h" #include "SetDesignNo.h" #include "ShowPic22.h" #include "JPEG.h" #define THUMBNAIL_WIDTH 80 #define THUMBNAIL_HEIGHT 70 ///////////////////////////////////////////////////////////////////////////// // SetDesignNo dialog IDC_BU Tdelall SetDesignNo::SetDesignNo(CWnd* pParent /*=NULL*/) : CDialog(SetDesignNo::IDD, pParent) { //{{AFX_DATA_INIT(SetDesignNo) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_bTerminate = false; m_bRunning = false; m_hThread = NULL; m_bTerminate2 = false; m_bRunning2 = false; m_hThread2 = NULL; m_image = NULL; } void SetDesignNo::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(SetDesignNo) DDX_Control(pDX, IDC_LIST4, m_List4); DDX_Control(pDX, IDC_LIST1, m_List1); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(SetDesignNo, CDialog) //{{AFX_MSG_MAP(SetDesignNo) ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1) ON_WM_DESTROY() ON_NOTIFY(NM_DBLCLK, IDC_LIST4, OnDblclkList4) ON_WM_CTLCOLOR() ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1) ON_NOTIFY(NM_CLICK, IDC_LIST4, OnClickList4) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // SetDesignNo message handlers extern BOOL CALLBACK EnumChildProc2(HWND hwnd, LPARAM lParam); BOOL SetDesignNo::OnInitDialog() { CDialog::OnInitDialog(); CRect rc2, rc; GetWindowRect(rc2); MoveWindow(g_rc); g_WidthScale2 = (float)g_rc.Width() / (float)rc2.Width(); // new/Old g_HeightScale2 = (float)g_rc.Height() / (float)rc2.Height(); EnumChildWindows(m_hWnd, (WNDENUMPROC)EnumChildProc2, 0); GetDlgItem(IDC_STATIC1)->GetWindowRect(rc2); rc2.right -= rc2.left; rc2.bottom -= rc2.top; rc2.left = rc2.top = 0; GetDlgItem(IDC_STATIC1)->MoveWindow(rc2); GetDlgItem(IDC_STATIC2)->MoveWindow(rc2); GetDlgItem(IDC_STATIC3)->GetWindowRect(rc); ScreenToClient(rc); rc.top = rc2.bottom; GetDlgItem(IDC_STATIC3)->MoveWindow(rc); CString str; str = "\r\n 功能简介:设置每一张设计版面所用的客人照片编号\r\n 1. 点选左侧要设置的一张设计版 2. Ctrl+点选右侧此设计版所用的原片 3. 重复前两步全部设置完成点击保存"; GetDlgItem(IDC_STATIC1)->SetWindowText(str); m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1); m_List1.SetImageList(&m_ImageListThumb, LVSIL_NORMAL); m_ImageListThumb2.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1); m_List4.SetImageList(&m_ImageListThumb2, LVSIL_NORMAL); CString filter = "id='" + m_id + "'"; g_sendhead.code[0] = 11; g_sendhead.tabcount = 1; g_sendhead.bsql = 0; g_pMainWnd->ProcessChatMessageRequest2(filter); if (g_bSendOK == 0) { CDialog::OnCancel(); return 0; } DataToArray(&g_List1array); if (g_List1array.GetSize()) { m_designno = g_List1array.ElementAt(0).ElementAt(58); } LoadPhotos(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } unsigned __stdcall LoadThumbNail11(LPVOID lpParam) { try { SetDesignNo *pDlg = (SetDesignNo*)lpParam; CListCtrl *ListCtrl = &pDlg->m_List1; CImageList* pImgList = &pDlg->m_ImageListThumb; // reset our image list for (int i = 0; i < pImgList->GetImageCount(); 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_path2) { spath = str.Left(str.ReverseFind('\\') + 1); spath = spath.Right(spath.GetLength() - pDlg->m_path2.GetLength()); } str = str.Right(str.GetLength() - str.ReverseFind('\\') - 1); str = spath + str.Left(str.GetLength() - 4); 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 (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_bRunning = false; pDlg->m_bTerminate = false; _endthreadex(0); return 0; } catch (...) { } return true; } unsigned __stdcall LoadThumbNail12(LPVOID lpParam) { try { SetDesignNo *pDlg = (SetDesignNo*)lpParam; CImageList* pImgList = &pDlg->m_ImageListThumb2; CListCtrl *ListCtrl = &pDlg->m_List4; // reset our image list for (int i = 0; i < pImgList->GetImageCount(); 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++) { 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, 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_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 (...) { } return true; } void SetDesignNo::LoadThumbImages() { TerminateThread(); m_hThread = (HANDLE)_beginthreadex(NULL, 0, LoadThumbNail11, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &m_dwThreadID); m_bRunning = true; } BOOL SetDesignNo::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 SetDesignNo::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here POSITION pos; pos = m_List1.GetFirstSelectedItemPosition(); if (pos == NULL) { return; } int iItem = m_List1.GetNextSelectedItem(pos); ShowPic22 dlg; dlg.m_nCurImgPos = iItem; dlg.m_AryOfImgPath = &m_patharray1; dlg.DoModal(); *pResult = 0; } void SetDesignNo::OnDestroy() { if (m_image)delete m_image; m_image = NULL; TerminateThread(); TerminateThread2(); CDialog::OnDestroy(); // TODO: Add your message handler code here } void SetDesignNo::LoadPhotos() { m_path2 = g_path3 + "\\"; m_path2 += m_id; m_path2 += "\\"; int i = 0; m_patharray1.RemoveAll(); m_path2.MakeLower(); CStringArray dirarray; CString str; if (1) { using namespace helper_coffs; ffsco o; o.dirs(1); o.find(LPCSTR(m_path2), 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) { for ( i = 0; i < dirarray.GetSize(); i++) { using namespace helper_coffs; ffsco o; o.dirs(0); o.find(LPCSTR(dirarray.ElementAt(i)), LPCSTR("*.jpg")); ffsco::typeT coo; ffsco::typeT::iterator it; coo = o.co_file(); for (it = coo.begin();coo.end() != it;it++) { str = (*it).c_str(); if (str.GetAt(str.ReverseFind('\\') + 1) == 's')continue; if (str.GetAt(str.ReverseFind('\\') + 1) == 'm')continue; str.MakeLower(); m_patharray1.Add(str); } } } CStringArray *pArray = &m_patharray1; m_vFileName.clear(); for ( i = 0; i < pArray->GetSize(); i++) { CString filePath = pArray->ElementAt(i); filePath.MakeLower(); m_vFileName.push_back(filePath); } LoadThumbImages(); ///////////// m_path = g_path1 + "\\"; m_path += m_id; m_path += "\\"; m_patharray2.RemoveAll(); m_path.MakeLower(); dirarray.RemoveAll(); 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) { for ( i = 0; i < dirarray.GetSize(); i++) { using namespace helper_coffs; ffsco o; o.dirs(0); o.find(LPCSTR(dirarray.ElementAt(i)), LPCSTR("*.jpg")); ffsco::typeT coo; ffsco::typeT::iterator it; coo = o.co_file(); for (it = coo.begin();coo.end() != it;it++) { str = (*it).c_str(); if (str.GetAt(str.ReverseFind('\\') + 1) == 's')continue; if (str.GetAt(str.ReverseFind('\\') + 1) == 'm')continue; str.MakeLower(); m_patharray2.Add(str); } } } if (m_bRunning2)return; pArray = &m_patharray2; m_vFileName2.clear(); for (i = 0; i < pArray->GetSize(); i++) { CString filePath = pArray->ElementAt(i); filePath.MakeLower(); m_vFileName2.push_back(filePath); } TerminateThread2(); m_hThread2 = (HANDLE)_beginthreadex(NULL, 0, LoadThumbNail12, (LPVOID)this, 0, &m_dwThreadID2); m_bRunning2 = true; } BOOL SetDesignNo::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 SetDesignNo::OnDblclkList4(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here POSITION pos; pos = m_List4.GetFirstSelectedItemPosition(); if (pos == NULL) { return; } int iItem = m_List4.GetNextSelectedItem(pos); ShowPic22 dlg; dlg.m_nCurImgPos = iItem; dlg.m_AryOfImgPath = &m_patharray2; dlg.DoModal(); *pResult = 0; } HBRUSH SetDesignNo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here if (nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_STATIC1) { pDC->SetBkMode(TRANSPARENT); //设置字体背景为透明 return (HBRUSH)::GetStockObject(WHITE_BRUSH); // 设置背景色 } // TODO: Return a different brush if the default is not desired // TODO: Return a different brush if the default is not desired return hbr; } void SetDesignNo::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here POSITION ppos; ppos = m_List1.GetFirstSelectedItemPosition(); if (ppos == NULL) { return; } int iItem = m_List1.GetNextSelectedItem(ppos); CString title = "$" + m_List1.GetItemText(iItem, 0) + ":"; CString str, spath; if (m_image)delete m_image; m_image = NULL; str = m_patharray1.ElementAt(iItem); spath = str.Left(str.ReverseFind('\\') + 1); spath += "s"; spath += str.Right(str.GetLength() - str.ReverseFind('\\') - 1); if (PathFileExists(spath)) ::LoadImageFromBuf(&m_image, spath); else ::LoadImageFromBuf(&m_image, str); CRect rc; GetDlgItem(IDC_STATIC3)->GetWindowRect(rc); ScreenToClient(rc); InvalidateRect(rc); CString sel; // m_designno="$10x8寸晶透水晶\\005:001,002,003"; //$xxxx:001,002,003,004$ int pos = m_designno.Find(title); if (pos != -1) { sel = m_designno.Right(m_designno.GetLength() - pos); sel = sel.Right(sel.GetLength() - title.GetLength()); pos = sel.Find("$"); if (pos != -1) sel = sel.Left(pos); sel.TrimLeft(","); sel.TrimRight(","); sel = "," + sel + ","; } for (int i = 0; i < m_List4.GetItemCount(); i++) { title = "," + m_List4.GetItemText(i, 0) + ","; if (sel.Find(title) != -1) m_List4.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED); else m_List4.SetItemState(i, 0, LVIS_SELECTED); } m_List4.SetFocus(); *pResult = 0; } void SetDesignNo::OnClickList4(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here POSITION ppos; ppos = m_List1.GetFirstSelectedItemPosition(); if (ppos == NULL) { return; } int iItem = m_List1.GetNextSelectedItem(ppos); CString title = "$" + m_List1.GetItemText(iItem, 0) + ":"; CString sel, temp; {// POSITION pos; pos = m_List4.GetFirstSelectedItemPosition(); while (pos) { iItem = m_List4.GetNextSelectedItem(pos); sel += m_List4.GetItemText(iItem, 0); sel += ","; } sel.TrimRight(","); }// // m_designno="$10x18寸晶透水晶\\005:001,002,003$10x28寸晶透水晶\\005:001,002,003$10x18寸晶透水晶\\005:001,002,003"; //$xxxx:001,002,003,004$ int pos = m_designno.Find(title); if (pos != -1) { temp = m_designno.Right(m_designno.GetLength() - pos - title.GetLength()); int pos2 = temp.Find("$"); if (pos2 != -1) temp = temp.Right(temp.GetLength() - pos2); else temp.Empty(); m_designno = m_designno.Left(pos + title.GetLength()); m_designno += sel; m_designno += temp; } else { m_designno += title; m_designno += sel; } *pResult = 0; } void SetDesignNo::OnOK() { // TODO: Add extra validation here CString sql = "update dindan set designno='" + m_designno + "' where id='" + m_id + "'"; g_sendhead.bsql = 1; g_pMainWnd->ProcessChatMessageRequest2(sql); if (g_bSendOK == 0)return; AfxMessageBox("保存成功!", MB_ICONINFORMATION); } void SetDesignNo::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here if (m_image) { CRect rc; GetDlgItem(IDC_STATIC3)->GetWindowRect(rc); ScreenToClient(rc); RectFitDes(m_image->GetWidth(), m_image->GetHeight(), rc); Graphics dcgraph(dc.GetSafeHdc()); Rect destinationRect(rc.left, rc.top, rc.Width(), rc.Height()); dcgraph.DrawImage(m_image, destinationRect, 0, 0, m_image->GetWidth(), m_image->GetHeight(), UnitPixel); } // Do not call CDialog::OnPaint() for painting messages }