GuiMain.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "GuiThumbnail.h"
  2. #include "GuiMain.h"
  3. #include <wchar.h>
  4. #include <regex>
  5. #include "xcgui.h"
  6. // 全局变量
  7. char ConfigFile[MAX_PATH] = {0};
  8. WCHAR keyFile[MAX_PATH] = L"";
  9. WCHAR savePath[MAX_PATH] = L"D:\\Thumbs缩略图";
  10. // 全局句柄
  11. HWINDOW hWindow;
  12. HELE keyEdit, pathEdit, hList, hStatic;
  13. HELE hPic1, hPic2, hPic3;
  14. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  15. {
  16. Everything_Start();
  17. XInitXCGUI(); // 初始化
  18. extern char ConfigFile[MAX_PATH];
  19. GetAppDir(ConfigFile);
  20. strcat(ConfigFile, "/ACThumbs.ini");
  21. if (IsFileExist(ConfigFile))
  22. LoadConfigFile();
  23. hWindow = XWnd_CreateWindow(0, 0, 960, 600, L"Adobe AI EPS INDD PDF和CorelDRAW DWG 缩略图工具 版权所有 2013-2024 Hongwenjun (蘭雅)"); // 创建窗口
  24. if (hWindow) {
  25. // 设置图标
  26. HICON logo_hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ICON1);
  27. XWnd_SetIcon(hWindow, logo_hIcon, false);
  28. InitXC_Window(hWindow); // 窗口布局
  29. XWnd_ShowWindow(hWindow, SW_SHOW); //显示窗口
  30. XRunXCGUI(); //运行
  31. }
  32. return 0;
  33. }
  34. // 读取和保存配置
  35. void LoadConfigFile()
  36. {
  37. wfstream wFile(ConfigFile, fstream::in);
  38. wFile.imbue(locale("chs"));
  39. wFile.getline(keyFile, MAX_PATH);
  40. wFile.getline(savePath, MAX_PATH);
  41. wFile.close();
  42. }
  43. void SaveConfigFile()
  44. {
  45. wfstream wFile(ConfigFile, fstream::out);
  46. wFile.imbue(locale("chs"));
  47. wFile << keyFile << endl << savePath << endl;
  48. wFile.close();
  49. }
  50. // 文本框回写配置
  51. bool edit_text(void)
  52. {
  53. XEdit_GetText(keyEdit, keyFile, MAX_PATH);
  54. XEdit_GetText(pathEdit, savePath, MAX_PATH);
  55. return true;
  56. }
  57. // 窗口布局
  58. void InitXC_Window(HWINDOW& hWindow)
  59. {
  60. // XWnd_SetImage(hWindow, XImage_LoadFile(L"GuiBG.dll")); // 设置窗口背景图片
  61. // 创建编辑框
  62. keyEdit = XEdit_Create(1, 2, 300, 22, hWindow);
  63. pathEdit = XEdit_Create(410, 2, 241, 22, hWindow);
  64. // 读取配置到文本框
  65. XEdit_SetText(keyEdit, keyFile);
  66. XEdit_SetText(pathEdit, savePath);
  67. // 工具提示按钮
  68. HELE keyButton = XBtn_Create(306, 2, 98, 22, L"关键字 --> 搜索", hWindow);
  69. XEle_EnableToolTips(keyButton, true);
  70. XEle_SetToolTips(keyButton, L"请输入关键字,用空格隔开");
  71. HELE pathButton = XBtn_Create(656, 2, 90, 32, L"选择目录", hWindow);
  72. XEle_EnableToolTips(pathButton, true);
  73. XEle_SetToolTips(pathButton, L"选择缩略图保存的目录");
  74. HELE runButton = XBtn_Create(656, 40, 90, 40, L"开始执行", hWindow);
  75. XEle_EnableToolTips(runButton, true);
  76. XEle_SetToolTips(runButton, L"开始执行提取缩略图");
  77. HELE closeButton = XBtn_Create(656, 88, 90, 32, L"保存关闭", hWindow);
  78. HELE removeButton = XBtn_Create(656, 128, 90, 32, L"清理缩略图", hWindow);
  79. // // 注册文本框输入事件
  80. // XEle_RegisterEvent(keyEdit, XE_EDIT_CHANGE, keyEditChange);
  81. // 注册按钮事件
  82. XEle_RegisterEvent(keyButton, XE_BNCLICK, keyBtnClick);
  83. XEle_RegisterEvent(pathButton, XE_BNCLICK, pathBtnClick);
  84. XEle_RegisterEvent(runButton, XE_BNCLICK, runBtnClick);
  85. XEle_RegisterEvent(closeButton, XE_BNCLICK, closeBtnClick);
  86. XEle_RegisterEvent(removeButton, XE_BNCLICK, removeBtnClick);
  87. //创建列表元素
  88. hList = XList_Create(1, 25, 650, 518, hWindow);
  89. //添加列表头
  90. XList_AddColumn(hList, 260, L"名称", 2);
  91. XList_AddColumn(hList, 260, L"路径", 2);
  92. XList_AddColumn(hList, 100, L"文件版本", 2);
  93. // 创建静态文本
  94. hStatic = XStatic_Create(1, 545, 650, 22, L"Adobe AI EPS INDD PDF和CorelDRAW DWG 缩略图工具 版权所有 2013-2024 Hongwenjun (蘭雅)", hWindow);
  95. XStatic_Create(760, 165, 160, 18, L"兰雅VBA代码分享 lyvba.com", hWindow);
  96. //创建图片元素
  97. hPic1 = XPic_Create(656, 225, 290, 315, hWindow);
  98. XPic_SetImage(hPic1, XImage_LoadFile(L"ACThumbs.png")); //设置显示图片
  99. hPic2 = XPic_Create(760, 5, 150, 150, hWindow);
  100. XPic_SetImage(hPic2, XImage_LoadFile(L"logo.jpg")); //设置显示图片
  101. // hPic3 = XPic_Create(654, 416, 128, 128, hWindow);
  102. // XPic_SetImage(hPic3, XImage_LoadFile(L"Thumbnail.dat")); //设置显示图片
  103. //注册列表元素项选择事件
  104. XEle_RegisterEvent(hList, XE_LIST_SELECT, MyEventListSelect);
  105. XEle_RegisterMessage(hList, XM_MOUSEDBCLICK, MyEventList_MouseDBClick);
  106. XEle_RegisterMessage(hList, XM_RBUTTONUP, MyEventList_RButtonUp);
  107. XEle_SetFocus(keyEdit, true);
  108. }
  109. // 事件响应
  110. //bool CALLBACK keyEditChange(HELE hEle, HELE hEventEle)
  111. //{
  112. // return true;
  113. //}
  114. bool CALLBACK keyBtnClick(HELE hEle, HELE hEventEle)
  115. {
  116. wchar_t keyWord[MAX_PATH] = {0};
  117. XEdit_GetText(keyEdit, keyWord, MAX_PATH);
  118. Everything_SetSearch(keyWord);
  119. bool EQRet = Everything_Query(TRUE);
  120. if (EQRet == false) {
  121. return false;
  122. }
  123. // 删除所有项目
  124. XList_DeleteAllItems(hList);
  125. const wchar_t* rs = L"(.+)(\\.(?:ai|AI|indd|INDD|Indd|eps|EPS|Eps|pdf|PDF|cdr|CDR|Cdr|dwg|DWG|Dwg|DWg|dWG))"; // 正则字符串,exp开始的单词
  126. std::wregex expression(rs); // 字符串传递给构造函数,建立正则表达式
  127. //添加列表项 // Display results.
  128. int id = 0;
  129. for (int i = 0 ; i < Everything_GetNumResults(); i++) {
  130. bool ret = std::regex_match((wchar_t*)Everything_GetResultFileName(i), expression);
  131. if (!ret) continue;
  132. //添加项
  133. XList_AddItem(hList, (wchar_t*)Everything_GetResultFileName(i), 0);
  134. //设置子项内容
  135. XList_SetItemText(hList, id++, 1, (wchar_t*)Everything_GetResultPath(i), 1);
  136. // XList_SetItemText(hList, i, 2, keyWord, 2);
  137. }
  138. wchar_t pText[MAX_PATH] = {0};
  139. wsprintfW(pText, L"匹配: %d 个文档(AI CDR EPS INDD PDF DWG格式), 鼠标双击: 打开文件,右键: 打开路径", id);
  140. XStatic_SetText(hStatic, pText);
  141. XWnd_RedrawWnd(hWindow, true);
  142. return true;
  143. }
  144. bool CALLBACK pathBtnClick(HELE hEle, HELE hEventEle)
  145. {
  146. char buf[MAX_PATH];
  147. if (GetPath(NULL, buf))
  148. charToWCHAR(savePath, buf);
  149. XEdit_SetText(pathEdit, savePath);
  150. return true;
  151. }
  152. bool CALLBACK runBtnClick(HELE hEle, HELE hEventEle)
  153. {
  154. // 从文本框获得参数
  155. edit_text();
  156. // // 执行提取缩略图 主功能
  157. bool ret = GuiThumbnail(keyFile, savePath);
  158. if (ret == false)
  159. return ret;
  160. // 打开文件夹
  161. ShellExecuteW(NULL, L"open", savePath, NULL, NULL, SW_SHOW);
  162. return ret;
  163. }
  164. bool CALLBACK closeBtnClick(HELE hEle, HELE hEventEle)
  165. {
  166. // 文本框回写配置
  167. edit_text();
  168. SaveConfigFile();
  169. ExitProcess(0); // 退出程序
  170. return true;
  171. }
  172. // 清理缩略图目录下图片索引文件
  173. bool CALLBACK removeBtnClick(HELE hEle, HELE hEventEle)
  174. {
  175. int ret = MessageBoxW(NULL, savePath, L"目前只能手工清理", MB_OK);
  176. ShellExecuteW(NULL, L"open", savePath, NULL, NULL, SW_SHOW);
  177. return true;
  178. }
  179. // 选择的文件全名 和路径
  180. wstring select_filename, select_filepath;
  181. BOOL CALLBACK MyEventListSelect(HELE hEle, HELE hEventEle, int id)
  182. {
  183. if (id < 0)
  184. return false;
  185. //hEle:列表元素句柄 //id:当前选择项索引
  186. select_filename = wstring(XList_GetItemText(hEle, id, 1)) + L"\\" + XList_GetItemText(hEle, id, 0);
  187. select_filepath = wstring(XList_GetItemText(hEle, id, 1));
  188. XPic_SetImage(hPic1, XImage_LoadFile(L"")); //设置图片为空,释放图片文件
  189. wchar_t tmppng[MAX_PATH] = L"Thumbnail.png";
  190. bool flag = Thumbnail_TempPng(select_filename.c_str(), tmppng); // 生成新的临时预览图
  191. if (flag) {
  192. XPic_SetImage(hPic1, XImage_LoadFile(tmppng, true)); //设置显示图片
  193. }
  194. XWnd_RedrawWnd(hWindow, true);
  195. return true;
  196. }
  197. BOOL CALLBACK MyEventList_RButtonUp(HELE hEle, UINT flags, POINT* pPt)
  198. {
  199. ShellExecuteW(NULL, L"open", select_filepath.c_str(), NULL, NULL, SW_SHOW);
  200. return false;
  201. }
  202. BOOL CALLBACK MyEventList_MouseDBClick(HELE hEle, POINT* pPt)
  203. {
  204. ShellExecuteW(NULL, L"open", select_filename.c_str(), NULL, NULL, SW_SHOW);
  205. return false;
  206. }
  207. // 选择一个目录
  208. int GetPath(HWND hWnd, char* pBuffer)
  209. {
  210. BROWSEINFO bf;
  211. LPITEMIDLIST lpitem;
  212. memset(&bf, 0, sizeof(BROWSEINFO));
  213. bf.hwndOwner = hWnd;
  214. bf.lpszTitle = L"选择路径 ";
  215. bf.ulFlags = BIF_RETURNONLYFSDIRS; //属性你可自己选择
  216. lpitem = SHBrowseForFolder(&bf);
  217. if (lpitem == NULL) //如果没有选择路径则返回 0
  218. return 0;
  219. //如果选择了路径则复制路径,返回路径长度
  220. SHGetPathFromIDListA(lpitem, pBuffer);
  221. return lstrlen(pBuffer);
  222. }
  223. bool Everything_Start(void)
  224. {
  225. Everything_SetSearch(L"Everything.exe");
  226. bool EQRet = Everything_Query(TRUE);
  227. if (EQRet == false) {
  228. MessageBoxA(NULL, "请先运行Everything软件!\n本软件全盘搜索功能需要Everything SDK.", "Everything SDK", MB_OK);
  229. return EQRet;
  230. }
  231. // if (EQRet == false) {
  232. //
  233. // STARTUPINFO si;
  234. // PROCESS_INFORMATION pi;
  235. // ZeroMemory(&si, sizeof(si));
  236. // si.cb = sizeof(si);
  237. // // 后台隐藏
  238. // si.dwFlags = STARTF_USESHOWWINDOW;
  239. // si.wShowWindow = SW_HIDE;
  240. // ZeroMemory(&pi, sizeof(pi));
  241. // // Start the child process.
  242. // CreateProcess(NULL, "Everything.exe", NULL, NULL, FALSE, 0,
  243. // NULL, NULL, &si, &pi);
  244. // }
  245. return true;
  246. }