VTM_IL.cpp 13 KB

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