MFCApplication1Dlg.cpp 19 KB

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