VTM_IL.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // test.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "VTM_IL.h"
  5. #include<string>
  6. #include<regex>
  7. #include <windows.h>
  8. #include <shellapi.h>
  9. #include <fstream>
  10. #include <algorithm>
  11. #include <cctype> // for std::tolower
  12. #include <ShlObj.h>
  13. using namespace std;
  14. #define MAX_LOADSTRING 100
  15. #define ERROR_TITILE "可视柜台终端应用程序"
  16. const int HOTKEY_ID = 1;
  17. double horizontalScale;
  18. double verticalScale;
  19. HWND taskbar;
  20. RECT position;
  21. // 全局变量:
  22. HINSTANCE hInst; // 当前实例
  23. TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
  24. TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
  25. // 此代码模块中包含的函数的前向声明:
  26. ATOM MyRegisterClass(HINSTANCE hInstance);
  27. BOOL InitInstance(HINSTANCE, int);
  28. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  29. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  30. int APIENTRY _tWinMain(HINSTANCE hInstance,
  31. HINSTANCE hPrevInstance,
  32. LPTSTR lpCmdLine,
  33. int nCmdShow)
  34. {
  35. UNREFERENCED_PARAMETER(hPrevInstance);
  36. UNREFERENCED_PARAMETER(lpCmdLine);
  37. MSG msg;
  38. HACCEL hAccelTable;
  39. // 初始化全局字符串
  40. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  41. LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
  42. MyRegisterClass(hInstance);
  43. Sleep(200);
  44. // 执行应用程序初始化:
  45. if (!InitInstance (hInstance, nCmdShow))
  46. {
  47. return FALSE;
  48. }
  49. hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST));
  50. // 主消息循环:
  51. while (GetMessage(&msg, NULL, 0, 0))
  52. {
  53. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  54. {
  55. TranslateMessage(&msg);
  56. DispatchMessage(&msg);
  57. }
  58. }
  59. return (int) msg.wParam;
  60. }
  61. //
  62. // 函数: MyRegisterClass()
  63. //
  64. // 目的: 注册窗口类。
  65. //
  66. // 注释:
  67. //
  68. // 仅当希望
  69. // 此代码与添加到 Windows 95 中的“RegisterClassEx”
  70. // 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
  71. // 这样应用程序就可以获得关联的
  72. // “格式正确的”小图标。
  73. //
  74. ATOM MyRegisterClass(HINSTANCE hInstance)
  75. {
  76. WNDCLASSEX wcex;
  77. wcex.cbSize = sizeof(WNDCLASSEX);
  78. wcex.style = CS_HREDRAW | CS_VREDRAW;
  79. wcex.lpfnWndProc = WndProc;
  80. wcex.cbClsExtra = 0;
  81. wcex.cbWndExtra = 0;
  82. wcex.hInstance = hInstance;
  83. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
  84. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  85. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  86. wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TEST);
  87. wcex.lpszClassName = szWindowClass;
  88. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  89. return RegisterClassEx(&wcex);
  90. }
  91. BOOL ExistsFileA(LPCSTR lpFilePath)
  92. {
  93. DWORD dwRet = GetFileAttributesA(lpFilePath);
  94. return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY);
  95. }
  96. BOOL ExistsDirA(LPCSTR lpDirPath)
  97. {
  98. DWORD dwRet = GetFileAttributesA(lpDirPath);
  99. return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY);
  100. }
  101. UINT GetErrorMessage(CString& retMessage, LPCTSTR lpDefault, DWORD error = GetLastError())
  102. {
  103. if (error == 0) {
  104. retMessage = lpDefault;
  105. return strlen(lpDefault);
  106. }
  107. LPVOID lpMsgBuf;
  108. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
  109. | FORMAT_MESSAGE_FROM_SYSTEM
  110. | FORMAT_MESSAGE_IGNORE_INSERTS,
  111. NULL,
  112. error,
  113. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  114. (LPTSTR)&lpMsgBuf,
  115. 0,
  116. NULL);
  117. if (lpMsgBuf == NULL) {
  118. retMessage = lpDefault;
  119. return strlen(lpDefault);
  120. }
  121. CString strTemp;
  122. strTemp.Format("错误描述:%s", (LPCTSTR)lpMsgBuf);
  123. retMessage = strTemp;
  124. LocalFree((LPVOID)lpMsgBuf);
  125. return retMessage.GetLength();
  126. }
  127. int GetVersion(char** pVersion)
  128. {
  129. char szFileName[MAX_PATH] = {0};
  130. char szFilePath[MAX_PATH] = {0};
  131. GetModuleFileNameA(NULL, szFileName, sizeof(szFileName));
  132. string strFileName = szFileName;
  133. int nPos = strFileName.find_last_of("\\");
  134. string strDir = strFileName.substr(0, nPos);
  135. sprintf_s(szFilePath, MAX_PATH,"%s\\active.txt", strDir.c_str());
  136. if (!ExistsFileA(szFilePath)) {
  137. CString strText;
  138. strText.Format("[RTA0031] 版本文件不存在,请重新安装程序版本!");
  139. MessageBoxA(NULL, strText, ERROR_TITILE, MB_OK | MB_ICONERROR);
  140. return 0;
  141. }
  142. ifstream infile(szFilePath);
  143. if(infile.is_open())
  144. {
  145. std::string line;
  146. if(std::getline(infile, line)){
  147. CString strVersionTxt(line.c_str());
  148. memcpy_s(*pVersion, MAX_PATH, strVersionTxt.GetString(), MAX_PATH);
  149. infile.close();
  150. return 1;
  151. }
  152. else
  153. {
  154. CString strText;
  155. strText.Format("[RTA0032] 版本文件内容为空,请重新安装程序版本!");
  156. MessageBoxA(NULL, strText, ERROR_TITILE, MB_OK | MB_ICONERROR);
  157. infile.close();
  158. return 0;
  159. }
  160. }
  161. else {
  162. const DWORD dwError = GetLastError();
  163. CString strText;
  164. CString strError;
  165. char szDefault[128];
  166. sprintf_s(szDefault, "错误描述:%u", dwError);
  167. GetErrorMessage(strError, szDefault, dwError);
  168. strText.Format("[RTA0033] 版本文件打开失败,请联系分行IT或厂商排查!\r\n\r\n%s", strError.GetBuffer());
  169. MessageBoxA(NULL, strText, ERROR_TITILE, MB_OK | MB_ICONERROR);
  170. return 0;
  171. }
  172. infile.close();
  173. return 0;
  174. }
  175. BOOL GetSpShellPath(char** pPath)
  176. {
  177. char* pVer = new char[MAX_PATH];
  178. memset(pVer, 0, MAX_PATH);
  179. char szFileName[MAX_PATH] = {0};
  180. char szFilePath[MAX_PATH] = {0};
  181. GetModuleFileNameA(NULL, szFileName, sizeof(szFileName));
  182. string strFileName = szFileName;
  183. int nPos = strFileName.find_last_of("\\");
  184. string strDir = strFileName.substr(0, nPos);
  185. if(!GetVersion(&pVer))
  186. {
  187. if (NULL != pVer)
  188. {
  189. delete [] pVer;
  190. pVer = NULL;
  191. }
  192. return FALSE;
  193. }
  194. memset(szFileName, 0, sizeof(szFileName));
  195. sprintf_s(szFileName, MAX_PATH, "%s\\%s", strDir.c_str(), pVer);
  196. if (!ExistsDirA(szFileName)) {
  197. CString strText;
  198. strText.Format("[RTA0034] 版本文件夹 [%s] 不存在,请重新安装程序版本!",pVer);
  199. MessageBoxA(NULL, strText, ERROR_TITILE, MB_OK | MB_ICONERROR);
  200. if (NULL != pVer)
  201. {
  202. delete[] pVer;
  203. pVer = NULL;
  204. }
  205. return FALSE;
  206. }
  207. sprintf_s(szFilePath, MAX_PATH, "%s\\%s\\bin\\SpShell.exe", strDir.c_str(), pVer);
  208. memcpy_s(*pPath, MAX_PATH, szFilePath, MAX_PATH);
  209. if (NULL != pVer)
  210. {
  211. delete[] pVer;
  212. pVer = NULL;
  213. }
  214. if (!ExistsFileA(*pPath)) {
  215. CString strText;
  216. strText.Format("[RTA0035] 程序执行文件不存在,请重新安装程序版本!(文件路径:%s)", *pPath);
  217. MessageBoxA(NULL, strText, ERROR_TITILE, MB_OK | MB_ICONERROR);
  218. return FALSE;
  219. }
  220. return TRUE;
  221. }
  222. BOOL Execute(const char* cExeName)
  223. {
  224. SHELLEXECUTEINFOA sei = {sizeof(SHELLEXECUTEINFOA)};
  225. sei.lpVerb = "runas";
  226. sei.lpFile = cExeName;
  227. sei.nShow = SW_SHOWNORMAL;
  228. if (!ShellExecuteExA(&sei))
  229. {
  230. const DWORD dwError = GetLastError();
  231. CString strText;
  232. CString strError;
  233. char szDefault[128];
  234. sprintf_s(szDefault, "错误描述:%u", dwError);
  235. GetErrorMessage(strError, szDefault, dwError);
  236. strText.Format("[RTA0036] 执行应用程序文件失败,请联系分行IT或厂商排查!\r\n\r\n%s", strError.GetBuffer());
  237. MessageBoxA(NULL, strText, ERROR_TITILE, MB_OK | MB_ICONERROR);
  238. return FALSE;
  239. }
  240. return TRUE;
  241. }
  242. BOOL RunSpshell()
  243. {
  244. BOOL ret = FALSE;
  245. char* pSpPath = new char[MAX_PATH];
  246. memset(pSpPath, 0, MAX_PATH);
  247. do
  248. {
  249. if (!GetSpShellPath(&pSpPath))
  250. {
  251. break;
  252. }
  253. if (!Execute(pSpPath))
  254. {
  255. break;
  256. }
  257. ret = TRUE;
  258. } while (false);
  259. if (NULL != pSpPath)
  260. {
  261. delete[] pSpPath;
  262. pSpPath = NULL;
  263. }
  264. return ret;
  265. }
  266. //校正
  267. int checkActiveTxtAttr()
  268. {
  269. char vtmexePath[MAX_PATH] = {0};
  270. GetModuleFileNameA(NULL, vtmexePath, sizeof(vtmexePath));
  271. string strRunVtmPath = vtmexePath;
  272. string strCheck = vtmexePath;
  273. std::transform(strCheck.begin(), strCheck.end(), strCheck.begin(), ::tolower);
  274. const auto nPosVtm = strCheck.find("\\version\\vtm.exe");
  275. if(nPosVtm == -1)
  276. {
  277. MessageBoxA(NULL,"[RTA0030] 可视柜台应用程序不在规定路径下执行,请重新安装程序版本!", ERROR_TITILE, MB_OK | MB_ICONERROR);
  278. return -1;
  279. }
  280. const int nPos = strRunVtmPath.find_last_of("\\");
  281. string strVersionDir = strRunVtmPath.substr(0, nPos);
  282. CString strActiveTxtPath=strVersionDir.c_str();//+"active.txt";
  283. strActiveTxtPath +="\\";
  284. strActiveTxtPath +="active.txt";
  285. if (ExistsFileA(strActiveTxtPath)) {
  286. CFileStatus rStatus;
  287. CFile::GetStatus(strActiveTxtPath, rStatus);//获得文件的属性设置
  288. //如果文件为只读的,将只读属性去掉
  289. if (rStatus.m_attribute & CFile::readOnly) {
  290. rStatus.m_attribute = rStatus.m_attribute & 0x3E;
  291. CFile::SetStatus(strActiveTxtPath, rStatus);//更改文件的属性设置
  292. }
  293. }
  294. return 0;
  295. }
  296. void RegistHotkey()
  297. {
  298. RegisterHotKey(
  299. NULL, // this thread will process the hotkey
  300. HOTKEY_ID,
  301. MOD_WIN, // win
  302. VK_OEM_3 // ~
  303. );
  304. }
  305. void UnRegistHotkey()
  306. {
  307. UnregisterHotKey(NULL, HOTKEY_ID);
  308. }
  309. void FormatPosition()
  310. {
  311. if (position.left < 0) {
  312. // taskbar is on the left, move right
  313. position.right -= position.left;
  314. position.left = 0;
  315. }
  316. if (position.bottom < 0) {
  317. // taskbar is on the bottom, move up
  318. position.top -= position.bottom;
  319. position.bottom = 0;
  320. }
  321. }
  322. void HideTaskbarEx()
  323. {
  324. auto empty = CreateRectRgn(0, 0, 0, 0);
  325. SetWindowRgn(taskbar, empty, true);
  326. DeleteObject(empty);
  327. }
  328. void ShowTaskbarEx()
  329. {
  330. auto region = CreateRectRgn(position.left, position.top, position.right * horizontalScale, position.bottom * verticalScale);
  331. SetWindowRgn(taskbar, region, true);
  332. DeleteObject(region);
  333. }
  334. VOID HideTaskBar(BOOL bHide)
  335. {
  336. int nCmdShow;
  337. HWND hWnd;
  338. LPARAM lParam;
  339. hWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
  340. if (bHide == TRUE) {
  341. nCmdShow = SW_HIDE;
  342. lParam = ABS_AUTOHIDE | ABS_ALWAYSONTOP;
  343. }
  344. else {
  345. nCmdShow = SW_SHOW;
  346. lParam = ABS_ALWAYSONTOP;
  347. }
  348. if (hWnd != NULL) {
  349. taskbar = hWnd;
  350. //ShowWindow(hWnd, nCmdShow);
  351. //GetWindowRect(hWnd, &position);
  352. //FormatPosition();
  353. //HideTaskbarEx();
  354. #if defined(DEVOPS_ON_PRD) || defined(DEVOPS_ON_UAT)
  355. APPBARDATA apBar;
  356. memset(&apBar, 0, sizeof(apBar));
  357. apBar.cbSize = sizeof(apBar);
  358. apBar.hWnd = hWnd;
  359. if (apBar.hWnd != NULL)
  360. {
  361. apBar.lParam = lParam;
  362. SHAppBarMessage(ABM_SETSTATE, &apBar);
  363. }
  364. #endif // DEVOPS_ON_PRD
  365. }
  366. }
  367. // Ref: https://stackoverflow.com/questions/54912038/querying-windows-display-scaling
  368. void GetDisplayScale()
  369. {
  370. auto activeWindow = GetActiveWindow();
  371. HMONITOR monitor = MonitorFromWindow(activeWindow, MONITOR_DEFAULTTONEAREST);
  372. // Get the logical width and height of the monitor
  373. MONITORINFOEX monitorInfoEx;
  374. monitorInfoEx.cbSize = sizeof(monitorInfoEx);
  375. GetMonitorInfo(monitor, &monitorInfoEx);
  376. auto cxLogical = monitorInfoEx.rcMonitor.right - monitorInfoEx.rcMonitor.left;
  377. auto cyLogical = monitorInfoEx.rcMonitor.bottom - monitorInfoEx.rcMonitor.top;
  378. // Get the physical width and height of the monitor
  379. DEVMODE devMode;
  380. devMode.dmSize = sizeof(devMode);
  381. devMode.dmDriverExtra = 0;
  382. EnumDisplaySettings(monitorInfoEx.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
  383. auto cxPhysical = devMode.dmPelsWidth;
  384. auto cyPhysical = devMode.dmPelsHeight;
  385. // Calculate the scaling factor
  386. horizontalScale = ((double)cxPhysical / (double)cxLogical);
  387. verticalScale = ((double)cyPhysical / (double)cyLogical);
  388. #ifdef _DEBUG
  389. printf("horizontalScale: %f\n", horizontalScale);
  390. printf("verticalScale: %f\n", verticalScale);
  391. #endif
  392. }
  393. //
  394. // 函数: InitInstance(HINSTANCE, int)
  395. //
  396. // 目的: 保存实例句柄并创建主窗口
  397. //
  398. // 注释:
  399. //
  400. // 在此函数中,我们在全局变量中保存实例句柄并
  401. // 创建和显示主程序窗口。
  402. //
  403. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  404. {
  405. // 校正active.txt的属性
  406. if (0 == checkActiveTxtAttr() && RunSpshell()) {
  407. HideTaskBar(TRUE);
  408. }
  409. hInst = hInstance; // 将实例句柄存储在全局变量中
  410. //HWND hWnd = CreateWindowA(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  411. //CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  412. //if (!hWnd)
  413. //{
  414. //return FALSE;
  415. //}
  416. //ShowWindow(hWnd, nCmdShow);
  417. //UpdateWindow(hWnd);
  418. return FALSE;
  419. }
  420. //
  421. // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
  422. //
  423. // 目的: 处理主窗口的消息。
  424. //
  425. // WM_COMMAND - 处理应用程序菜单
  426. // WM_PAINT - 绘制主窗口
  427. // WM_DESTROY - 发送退出消息并返回
  428. //
  429. //
  430. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  431. {
  432. int wmId, wmEvent;
  433. PAINTSTRUCT ps;
  434. HDC hdc;
  435. switch (message)
  436. {
  437. case WM_COMMAND:
  438. wmId = LOWORD(wParam);
  439. wmEvent = HIWORD(wParam);
  440. // 分析菜单选择:
  441. switch (wmId)
  442. {
  443. case IDM_ABOUT:
  444. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  445. break;
  446. case IDM_EXIT:
  447. DestroyWindow(hWnd);
  448. break;
  449. default:
  450. return DefWindowProc(hWnd, message, wParam, lParam);
  451. }
  452. break;
  453. case WM_PAINT:
  454. hdc = BeginPaint(hWnd, &ps);
  455. // TODO: 在此添加任意绘图代码...
  456. EndPaint(hWnd, &ps);
  457. break;
  458. case WM_DESTROY:
  459. PostQuitMessage(0);
  460. break;
  461. default:
  462. return DefWindowProc(hWnd, message, wParam, lParam);
  463. }
  464. return 0;
  465. }
  466. // “关于”框的消息处理程序。
  467. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  468. {
  469. UNREFERENCED_PARAMETER(lParam);
  470. switch (message)
  471. {
  472. case WM_INITDIALOG:
  473. return (INT_PTR)TRUE;
  474. case WM_COMMAND:
  475. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  476. {
  477. EndDialog(hDlg, LOWORD(wParam));
  478. return (INT_PTR)TRUE;
  479. }
  480. break;
  481. }
  482. return (INT_PTR)FALSE;
  483. }