MFCApplication1Dlg.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. 
  2. // MFCApplication1Dlg.cpp: 实现文件
  3. //
  4. #include "pch.h"
  5. #include "framework.h"
  6. #include "MFCApplication1.h"
  7. #include "MFCApplication1Dlg.h"
  8. #include "afxdialogex.h"
  9. #include "CvxText.h"
  10. #include "BaseUtility.h"
  11. #include "libpdf.h"
  12. #include "AssignWords.h"
  13. #include "lib_common_depence.h"
  14. #include <direct.h>
  15. #include <io.h>
  16. #include "Buffers.h"
  17. #include "Config.h"
  18. #if defined(GNUC)
  19. #pragma GCC diagnostic push
  20. #pragma GCC diagnostic ignored “-Wdeprecated-declarations”
  21. #elif defined(_MSC_VER)
  22. #pragma warning(disable : 4996)
  23. #endif
  24. #include <json/json.h>
  25. #ifdef _DEBUG
  26. #pragma comment(lib, "libpdfd.lib")
  27. #pragma comment(lib, "lib_common_depence_d.lib")
  28. #else
  29. #pragma comment(lib, "libpdf.lib")
  30. #pragma comment(lib, "lib_common_depence.lib")
  31. #endif
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #endif
  35. #define SAFETY_CLOSE_HANDLE( handle ) \
  36. if( NULL != handle ) \
  37. { \
  38. CloseHandle( handle ); \
  39. handle = NULL; \
  40. }
  41. #define SAFETY_EXIT_THREAD( hThread, Timeout ) \
  42. if( NULL != hThread ) \
  43. { \
  44. if( WaitForSingleObject( hThread, Timeout ) == WAIT_TIMEOUT ) \
  45. TerminateThread( hThread, -1 ); \
  46. CloseHandle( hThread ); \
  47. hThread = NULL; \
  48. }
  49. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  50. class CAboutDlg : public CDialogEx
  51. {
  52. public:
  53. CAboutDlg();
  54. // 对话框数据
  55. #ifdef AFX_DESIGN_TIME
  56. enum { IDD = IDD_ABOUTBOX };
  57. #endif
  58. protected:
  59. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  60. // 实现
  61. protected:
  62. DECLARE_MESSAGE_MAP()
  63. };
  64. CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
  65. {
  66. }
  67. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  68. {
  69. CDialogEx::DoDataExchange(pDX);
  70. }
  71. BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
  72. END_MESSAGE_MAP()
  73. // CMFCApplication1Dlg 对话框
  74. vector<string> g_vecFileList;//扫描文件列表
  75. int g_scanCount = 0;//扫描文件个数
  76. HANDLE _threadProcess = NULL;
  77. DWORD WINAPI ProcessThread(void *param)
  78. {
  79. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  80. std::vector<std::string> twoList;
  81. std::vector<std::string> threeList;
  82. std::vector<std::string> fourList;
  83. AssignWordsFromTest(UnicodeToGB2312(pWnd->m_strTextPath), twoList, threeList, fourList);
  84. HMODULE module = GetModuleHandle(0);
  85. TCHAR pFileName[MAX_PATH + 2] = { 0 };
  86. GetModuleFileName(module, pFileName, MAX_PATH);
  87. CString csFullPath(pFileName);
  88. int nPos = csFullPath.ReverseFind(_T('\\'));
  89. CString strFileDir = csFullPath.Left(nPos);
  90. CString strFilePath = strFileDir + L"\\config.ini";
  91. WCHAR wgray[10];
  92. GetPrivateProfileString(L"USER", L"GrayPix", L"100", wgray, 10, strFilePath);
  93. int nLineGrayPix = _wtoi(wgray);
  94. auto CovertPdfFunc = [&](std::vector<std::string>& vctFiles, int col, int& pageNum){
  95. std::string strPage1;
  96. for (size_t i = 0; i < vctFiles.size(); i += max_line * col)
  97. {
  98. std::vector<std::string> vecSmall;
  99. auto last = std::min(vctFiles.size(), i + max_line * col);
  100. vecSmall.insert(vecSmall.begin(), vctFiles.begin() + i, vctFiles.begin() + last);
  101. std::string strPagePath;
  102. dataCollectionPaper(col, pageNum, 30, nLineGrayPix, false, vecSmall, pWnd->m_strPdfDir, strPagePath);
  103. if (strPage1.length() == 0)
  104. {
  105. strPage1 = strPagePath;
  106. }
  107. else
  108. {
  109. CString strPdfPath;
  110. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  111. PdfNewFromImage({ strPage1, strPagePath }, TstringToGB2312(strPdfPath.GetBuffer()));
  112. strPage1 = "";
  113. CString strMsg = L"";
  114. strMsg.Format(L"%s\r\n", strPdfPath);
  115. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  116. strMsg.ReleaseBuffer();
  117. }
  118. pageNum++;
  119. }
  120. if (strPage1.length() > 0)
  121. {
  122. CString strPdfPath;
  123. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  124. PdfNewFromImage({ strPage1 }, TstringToGB2312(strPdfPath.GetBuffer()));
  125. CString strMsg = L"";
  126. strMsg.Format(L"%s\r\n", strPdfPath);
  127. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  128. strMsg.ReleaseBuffer();
  129. }
  130. };
  131. int pageNum = _wtoi(pWnd->m_strPageNumber);
  132. CovertPdfFunc(twoList, 2, pageNum);
  133. CovertPdfFunc(threeList, 3, pageNum);
  134. CovertPdfFunc(fourList, 4, pageNum);
  135. //通知发送消息到窗口
  136. CString strErrorMsg = L"";
  137. strErrorMsg.Format(L"PDF生成完成!\r\n");
  138. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
  139. strErrorMsg.ReleaseBuffer();
  140. return 0L;
  141. }
  142. int IdentifyCallback(result::spinfo& pinfo, void* param)
  143. {
  144. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  145. if (pinfo.choiceRes.size() < 3 || pinfo.vecUrlAPath.size() == 0)
  146. {
  147. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  148. pWnd->FormatScanMsg("矫正图像失败原图为:%s", pinfo.vecUrlPath.size() > 0 ? pinfo.vecUrlPath[0].c_str() : "图像为空");
  149. }
  150. std::string strChoiceRes;
  151. for (auto& row : pinfo.choiceRes)
  152. {
  153. for (auto& col : row.vecRes)
  154. {
  155. strChoiceRes.append(col ? "1" : "0");
  156. }
  157. }
  158. int length = strChoiceRes.length();
  159. int number = 0;
  160. for (int i = 0; i < length; ++i)
  161. {
  162. if (strChoiceRes[i] == '1') // 其中注意if的判断两个条件相等的符号
  163. number += pow(2, length - 1 - i);
  164. }
  165. std::string strJsonDir = CT2A(CConfig::Instance()->m_strMode);
  166. strJsonDir.append("/");
  167. strJsonDir.append(std::to_string(number));
  168. strJsonDir.append(".json");
  169. std::string strSaveDir = CT2A(CConfig::Instance()->m_strCut);
  170. cutPaper(number, strJsonDir, pinfo.vecUrlAPath[0], strSaveDir);
  171. g_scanCount++;
  172. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  173. pWnd->FormatScanMsg("生成切割图像:%s", std::to_string(number).c_str());
  174. return 0;
  175. }
  176. enum EnumSearFileType {
  177. file,//扫描文件
  178. dir,//扫描文件夹
  179. };
  180. void GetImages(CString strPath)
  181. {
  182. queue<CString> queuePath;
  183. queuePath.push(strPath);
  184. EnumSearFileType serchType = file;
  185. CFileFind finder;
  186. do
  187. {
  188. bool bFinished = finder.FindFile(queuePath.front() + _T("\\*.JPG"));
  189. while (serchType == file && bFinished)
  190. {
  191. bFinished = finder.FindNextFile();
  192. if (finder.IsDirectory())continue;
  193. CString strPath = finder.GetFilePath();
  194. g_vecFileList.push_back(UnicodeToGB2312(strPath));
  195. }
  196. serchType = dir;
  197. bFinished = finder.FindFile(queuePath.front() + _T("\\*.*"));
  198. while (bFinished)
  199. {
  200. bFinished = finder.FindNextFile();
  201. if (finder.IsDirectory() && !finder.IsDots())
  202. {
  203. queuePath.push(finder.GetFilePath());
  204. }
  205. }
  206. queuePath.pop();
  207. serchType = file;
  208. } while (queuePath.size() > 0);
  209. }
  210. string GetBatchId()
  211. {
  212. SYSTEMTIME st;
  213. GetLocalTime(&st);
  214. char chBuf[24] = { 0 };
  215. sprintf(chBuf, "%02d%02d%02d%02d%02d%02d",
  216. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  217. return chBuf;
  218. }
  219. HANDLE _threadScan = NULL;
  220. char* g_strIdentifyMsg[7] = { "正常","内部error","批次号异常","保存路径异常","模板为空","图像列表异常","图像列表不是模板整数倍" };
  221. DWORD WINAPI ScanThread(void *param)
  222. {
  223. g_scanCount = 0;
  224. g_vecFileList.clear();
  225. GetImages(CConfig::Instance()->m_strImg);
  226. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  227. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  228. pWnd->FormatScanMsg("启动扫描线程......");
  229. auto exitfun = [&]() {
  230. pWnd->FormatScanMsg("扫描线程退出。");
  231. SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, 0, 1);
  232. };
  233. preinfo::templatesInfo temeplatInfo;
  234. preinfo::SubjChiInfo sujChiInfo;
  235. std::string strTempDir = CT2A(CConfig::Instance()->m_strCut);
  236. /*if (_access(strTempDir.c_str(), 0) == -1)
  237. {
  238. _mkdir(strTempDir.c_str());
  239. }*/
  240. int nRet = PareseModeJson(temeplatInfo);
  241. if (nRet)
  242. {
  243. pWnd->FormatScanMsg("模板解析失败!");
  244. exitfun();
  245. return 0;
  246. }
  247. //生成批次号
  248. string strBatchId = GetBatchId();
  249. pWnd->FormatScanMsg("创建批次id:%s", strBatchId.c_str());
  250. nRet = api_processing_images(strBatchId.c_str(),
  251. strTempDir.c_str(),
  252. temeplatInfo,
  253. sujChiInfo,
  254. g_vecFileList,
  255. LOG_AUTO,
  256. false,
  257. OLNS,
  258. IdentifyCallback,
  259. NULL);
  260. ////启动算法扫描功能
  261. if (nRet != RT_OK)
  262. {
  263. if (nRet >= 0 && nRet <= 6)
  264. pWnd->FormatScanMsg("扫描失败,错误描述:%s, errorCode:%d", g_strIdentifyMsg[nRet], nRet);
  265. else
  266. pWnd->FormatScanMsg("扫描失败,errorCode:%d", nRet);
  267. }
  268. exitfun();
  269. return 0L;
  270. }
  271. CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
  272. : CDialogEx(IDD_MFCAPPLICATION1_DIALOG, pParent)
  273. , m_strMsg(_T("")), m_strScanMsg(_T(""))
  274. {
  275. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  276. }
  277. void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
  278. {
  279. CDialogEx::DoDataExchange(pDX);
  280. DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
  281. DDX_Text(pDX, IDC_EDIT_SCANMSG, m_strScanMsg);
  282. }
  283. BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
  284. ON_WM_SYSCOMMAND()
  285. ON_WM_PAINT()
  286. ON_WM_QUERYDRAGICON()
  287. ON_BN_CLICKED(IDOK, &CMFCApplication1Dlg::OnBnClickedOk)
  288. ON_MESSAGE(WM_SHOWINFO, OnShowInfo)
  289. ON_MESSAGE(WM_SHOWSCANINFO, OnShowScanInfo)
  290. ON_MESSAGE(WM_PROCESS, OnProcessInfo)
  291. ON_BN_CLICKED(IDCANCEL, &CMFCApplication1Dlg::OnBnClickedCancel)
  292. ON_BN_CLICKED(IDC_BTN_TXT, &CMFCApplication1Dlg::OnBnClickedBtnTxt)
  293. ON_BN_CLICKED(IDC_BTN_PDF, &CMFCApplication1Dlg::OnBnClickedBtnPdf)
  294. ON_BN_CLICKED(IDC_BTN_MODEPATH, &CMFCApplication1Dlg::OnBnClickedBtnModepath)
  295. ON_BN_CLICKED(IDC_BTN_IMGPATH, &CMFCApplication1Dlg::OnBnClickedBtnImgpath)
  296. ON_BN_CLICKED(IDC_BTN_CUTPATH, &CMFCApplication1Dlg::OnBnClickedBtnCutpath)
  297. ON_BN_CLICKED(ID_BTN_SCAN, &CMFCApplication1Dlg::OnBnClickedBtnScan)
  298. END_MESSAGE_MAP()
  299. // CMFCApplication1Dlg 消息处理程序
  300. BOOL CMFCApplication1Dlg::OnInitDialog()
  301. {
  302. CDialogEx::OnInitDialog();
  303. // 将“关于...”菜单项添加到系统菜单中。
  304. // IDM_ABOUTBOX 必须在系统命令范围内。
  305. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  306. ASSERT(IDM_ABOUTBOX < 0xF000);
  307. CMenu* pSysMenu = GetSystemMenu(FALSE);
  308. if (pSysMenu != nullptr)
  309. {
  310. BOOL bNameValid;
  311. CString strAboutMenu;
  312. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  313. ASSERT(bNameValid);
  314. if (!strAboutMenu.IsEmpty())
  315. {
  316. pSysMenu->AppendMenu(MF_SEPARATOR);
  317. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  318. }
  319. }
  320. // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
  321. // 执行此操作
  322. SetIcon(m_hIcon, TRUE); // 设置大图标
  323. SetIcon(m_hIcon, FALSE); // 设置小图标
  324. CConfig::Instance()->LoadFromINI();
  325. GetDlgItem(IDC_EDIT_TXT)->SetWindowText(CConfig::Instance()->m_strTxt);
  326. GetDlgItem(IDC_EDIT_PDF)->SetWindowText(CConfig::Instance()->m_strPdf);
  327. GetDlgItem(IDC_EDIT_MODEPATH)->SetWindowText(CConfig::Instance()->m_strMode);
  328. GetDlgItem(IDC_EDIT_IMGPATH)->SetWindowText(CConfig::Instance()->m_strImg);
  329. GetDlgItem(IDC_EDIT_CUTPATH)->SetWindowText(CConfig::Instance()->m_strCut);
  330. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  331. }
  332. void CMFCApplication1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  333. {
  334. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  335. {
  336. CAboutDlg dlgAbout;
  337. dlgAbout.DoModal();
  338. }
  339. else
  340. {
  341. CDialogEx::OnSysCommand(nID, lParam);
  342. }
  343. }
  344. // 如果向对话框添加最小化按钮,则需要下面的代码
  345. // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
  346. // 这将由框架自动完成。
  347. void CMFCApplication1Dlg::OnPaint()
  348. {
  349. if (IsIconic())
  350. {
  351. CPaintDC dc(this); // 用于绘制的设备上下文
  352. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  353. // 使图标在工作区矩形中居中
  354. int cxIcon = GetSystemMetrics(SM_CXICON);
  355. int cyIcon = GetSystemMetrics(SM_CYICON);
  356. CRect rect;
  357. GetClientRect(&rect);
  358. int x = (rect.Width() - cxIcon + 1) / 2;
  359. int y = (rect.Height() - cyIcon + 1) / 2;
  360. // 绘制图标
  361. dc.DrawIcon(x, y, m_hIcon);
  362. }
  363. else
  364. {
  365. CDialogEx::OnPaint();
  366. }
  367. }
  368. //当用户拖动最小化窗口时系统调用此函数取得光标
  369. //显示。
  370. HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
  371. {
  372. return static_cast<HCURSOR>(m_hIcon);
  373. }
  374. void CMFCApplication1Dlg::OnBnClickedOk()
  375. {
  376. ((CEdit*)GetDlgItem(IDC_EDIT_TXT))->GetWindowText(m_strTextPath);
  377. ((CEdit*)GetDlgItem(IDC_EDIT_PDF))->GetWindowText(m_strPdfDir);
  378. ((CEdit*)GetDlgItem(IDC_EDIT_PAGE_NUMBER))->GetWindowText(m_strPageNumber);
  379. if (m_strTextPath.GetLength() == 0)
  380. {
  381. AfxMessageBox(L"语料库文件未选择");
  382. return;
  383. }
  384. if (m_strPdfDir.GetLength() == 0)
  385. {
  386. AfxMessageBox(L"PDF文件夹未选择");
  387. return;
  388. }
  389. m_strPageNumber.Trim();
  390. if (m_strPageNumber.GetLength() == 0)
  391. {
  392. AfxMessageBox(L"启始页码未输入");
  393. return;
  394. }
  395. if (m_strPageNumber.SpanIncluding(_T("0123456789")) != m_strPageNumber)
  396. {
  397. AfxMessageBox(L"启始页码不是数字");
  398. return;
  399. }
  400. if (_wtoi(m_strPageNumber.GetBuffer()) > 20000)
  401. {
  402. AfxMessageBox(L"启始页码小于20000");
  403. return;
  404. }
  405. GetDlgItem(IDOK)->EnableWindow(FALSE);
  406. m_strMsg = "";
  407. CString strJsonDir(m_strPdfDir);
  408. strJsonDir.Append(L"\\json");
  409. if (!PathIsDirectory(strJsonDir))
  410. {
  411. ::CreateDirectory(strJsonDir, 0);
  412. }
  413. CString strPdfDir(m_strPdfDir);
  414. strPdfDir.Append(L"\\pdf");
  415. if (!PathIsDirectory(strPdfDir))
  416. {
  417. ::CreateDirectory(strPdfDir, 0);
  418. }
  419. SAFETY_EXIT_THREAD(_threadProcess, 100);
  420. _threadProcess = CreateThread(NULL, 0, ProcessThread, NULL, 0, NULL);
  421. //CDialogEx::OnOK();
  422. }
  423. void CMFCApplication1Dlg::UpdateMsg(CString strMsg)
  424. {
  425. m_strMsg += strMsg;
  426. UpdateData(FALSE);
  427. }
  428. void CMFCApplication1Dlg::UpdateScanMsg(CString strMsg)
  429. {
  430. m_strScanMsg += strMsg;
  431. UpdateData(FALSE);
  432. }
  433. LRESULT CMFCApplication1Dlg::OnShowInfo(WPARAM wParam, LPARAM lParam)
  434. {
  435. wchar_t* p = (wchar_t*)wParam;
  436. CString str;
  437. str.Format(_T("%s"), p);
  438. m_strMsg += str;
  439. UpdateData(FALSE);
  440. if (lParam == 1)
  441. {
  442. GetDlgItem(IDOK)->EnableWindow(TRUE);
  443. }
  444. return 0;
  445. }
  446. LRESULT CMFCApplication1Dlg::OnShowScanInfo(WPARAM wParam, LPARAM lParam)
  447. {
  448. if (lParam == 1)
  449. {
  450. GetDlgItem(ID_BTN_SCAN)->EnableWindow(TRUE);
  451. return 0;
  452. }
  453. wchar_t* p = (wchar_t*)wParam;
  454. CString str;
  455. str.Format(_T("%s"), p);
  456. m_strScanMsg += str;
  457. UpdateData(FALSE);
  458. return 0;
  459. }
  460. LRESULT CMFCApplication1Dlg::OnProcessInfo(WPARAM wParam, LPARAM lParam)
  461. {
  462. CString strProcess = _T("--");
  463. if (g_vecFileList.size())
  464. {
  465. float fProcess = (float)g_scanCount / (float)g_vecFileList.size();
  466. strProcess.Format(L"%0.0lf%%", fProcess*100);
  467. }
  468. SetDlgItemText(IDC_EDIT_SCANPROCESS, strProcess);
  469. return 0;
  470. }
  471. void CMFCApplication1Dlg::OnBnClickedCancel()
  472. {
  473. SAFETY_EXIT_THREAD(_threadProcess, 100);
  474. SAFETY_EXIT_THREAD(_threadScan, 100);
  475. CConfig::Instance()->SaveSetting();
  476. CDialogEx::OnCancel();
  477. }
  478. void CMFCApplication1Dlg::SelectFolder(HWND hwnd, CString& lpszFolder)
  479. {
  480. TCHAR szFolderPath[MAX_PATH];
  481. szFolderPath[0] = 0;
  482. BROWSEINFO sInfo;
  483. ::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
  484. sInfo.pidlRoot = 0;
  485. sInfo.lpszTitle = _T("请选择一个文件夹");
  486. sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX;
  487. sInfo.lpfn = NULL;
  488. sInfo.hwndOwner = hwnd;
  489. LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);
  490. if (lpidlBrowse != NULL) {
  491. if (::SHGetPathFromIDList(lpidlBrowse, szFolderPath)) {
  492. lpszFolder = szFolderPath;
  493. lpszFolder.Replace('\\', '/');
  494. }
  495. ::CoTaskMemFree(lpidlBrowse);
  496. }
  497. }
  498. void CMFCApplication1Dlg::SelectFile(CString& lpszFile)
  499. {
  500. CString OpenFilter = _T("文本文件(*.txt)|*.txt;|");
  501. CFileDialog FileOpen(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, OpenFilter);
  502. if (IDOK == FileOpen.DoModal())
  503. {
  504. lpszFile = FileOpen.GetPathName();
  505. lpszFile.Replace('\\', '/');
  506. }
  507. }
  508. void CMFCApplication1Dlg::OnBnClickedBtnTxt()
  509. {
  510. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_TXT);
  511. if (!pEdit)
  512. {
  513. return;
  514. }
  515. CString strText;
  516. SelectFile(strText);
  517. if (strText.GetLength() == 0)
  518. {
  519. return;
  520. }
  521. pEdit->SetWindowText(strText);
  522. CConfig::Instance()->m_strTxt = strText;
  523. }
  524. void CMFCApplication1Dlg::OnBnClickedBtnPdf()
  525. {
  526. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_PDF);
  527. if (!pEdit)
  528. {
  529. return;
  530. }
  531. CString strText;
  532. SelectFolder(GetSafeHwnd(), strText);
  533. if (strText.GetLength() == 0)
  534. {
  535. return;
  536. }
  537. pEdit->SetWindowText(strText);
  538. CConfig::Instance()->m_strPdf = strText;
  539. }
  540. void CMFCApplication1Dlg::OnBnClickedBtnModepath()
  541. {
  542. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_MODEPATH);
  543. if (!pEdit)
  544. {
  545. return;
  546. }
  547. CString strText;
  548. SelectFolder(GetSafeHwnd(), strText);
  549. if (strText.GetLength() == 0)
  550. {
  551. return;
  552. }
  553. pEdit->SetWindowText(strText);
  554. CConfig::Instance()->m_strMode = strText;
  555. }
  556. void CMFCApplication1Dlg::OnBnClickedBtnImgpath()
  557. {
  558. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_IMGPATH);
  559. if (!pEdit)
  560. {
  561. return;
  562. }
  563. CString strText;
  564. SelectFolder(GetSafeHwnd(), strText);
  565. if (strText.GetLength() == 0)
  566. {
  567. return;
  568. }
  569. pEdit->SetWindowText(strText);
  570. CConfig::Instance()->m_strImg = strText;
  571. }
  572. void CMFCApplication1Dlg::OnBnClickedBtnCutpath()
  573. {
  574. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CUTPATH);
  575. if (!pEdit)
  576. {
  577. return;
  578. }
  579. CString strText;
  580. SelectFolder(GetSafeHwnd(), strText);
  581. if (strText.GetLength() == 0)
  582. {
  583. return;
  584. }
  585. pEdit->SetWindowText(strText);
  586. CConfig::Instance()->m_strCut = strText;
  587. }
  588. void CMFCApplication1Dlg::OnBnClickedBtnScan()
  589. {
  590. ((CEdit*)GetDlgItem(IDC_EDIT_MODEPATH))->GetWindowText(m_strModePath);
  591. ((CEdit*)GetDlgItem(IDC_EDIT_IMGPATH))->GetWindowText(m_strImgPath);
  592. ((CEdit*)GetDlgItem(IDC_EDIT_CUTPATH))->GetWindowText(m_strCutPath);
  593. if (m_strModePath.GetLength() == 0)
  594. {
  595. AfxMessageBox(L"模板文件夹未选择");
  596. return;
  597. }
  598. if (m_strImgPath.GetLength() == 0)
  599. {
  600. AfxMessageBox(L"答题卡文件夹未选择");
  601. return;
  602. }
  603. if (m_strCutPath.GetLength() == 0)
  604. {
  605. AfxMessageBox(L"切割生成文件夹未选择");
  606. return;
  607. }
  608. GetDlgItem(ID_BTN_SCAN)->EnableWindow(FALSE);
  609. m_strScanMsg = "";
  610. CString strNormal(m_strCutPath);
  611. strNormal.Append(L"/normal");
  612. if (!PathIsDirectory(strNormal))
  613. {
  614. ::CreateDirectory(strNormal, 0);
  615. }
  616. CString strAbnormal(m_strCutPath);
  617. strAbnormal.Append(L"/abnormal");
  618. if (!PathIsDirectory(strAbnormal))
  619. {
  620. ::CreateDirectory(strAbnormal, 0);
  621. }
  622. SAFETY_EXIT_THREAD(_threadScan, 100);
  623. _threadScan = CreateThread(NULL, 0, ScanThread, NULL, 0, NULL);
  624. }
  625. void CMFCApplication1Dlg::FormatScanMsg(const char *szFmt, ...)
  626. {
  627. if (!szFmt)
  628. return;
  629. va_list ap;
  630. va_start(ap, szFmt);
  631. size_t nLength = _vsnprintf(nullptr, 0, szFmt, ap) + 1;
  632. CBuffers buffer;
  633. char* szLogMsg = buffer.GetBuf(nLength);
  634. _vsnprintf(szLogMsg, nLength, szFmt, ap);
  635. va_end(ap);
  636. SYSTEMTIME st;
  637. GetLocalTime(&st);
  638. char chBuf[24] = { 0 };
  639. sprintf(chBuf, "%02d-%02d-%02d %02d:%02d:%02d ",
  640. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  641. CString strMsg;
  642. // 扫描异常
  643. strMsg.Format(L"%s %s\r\n", GB2312ToTstring(chBuf).c_str(), GB2312ToTstring(szLogMsg).c_str());
  644. SendMessage(WM_SHOWSCANINFO, (WPARAM)strMsg.GetBuffer(), 0);
  645. }