sp_gui.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. #include "precompile.h"
  2. #include "sp_gui.h"
  3. #include "sp_def.h"
  4. #include "memutil.h"
  5. #include "log_define.h"
  6. #ifdef _WIN32
  7. #include <tchar.h>
  8. #include "resource1.h"
  9. #include <atlbase.h>
  10. #include <gdiplus.h>
  11. #pragma comment(lib,"gdiplus")
  12. using namespace Gdiplus;
  13. #include "resource1.h"
  14. #include "sp_checkEntity.h"
  15. #endif //_WIN32
  16. #include "SimpleString.h"
  17. #include <string>
  18. #include <list>
  19. using namespace std;
  20. #include "sp_env.h"
  21. #include "array.h"
  22. #include "sp_cfg.h"
  23. #include "SpBase.h"
  24. #include <winpr/wtypes.h>
  25. #define SHELL_WND_CLS "SpShell_Wnd"
  26. #define BLUE_COLOR RGB(0, 0, 255)
  27. #define WHITE_COLOR RGB(255,255, 255)
  28. #define RED_COLOR RGB(255,0, 0)
  29. #define WM_DISPLAY (WM_APP+1)
  30. #define WM_UNDISPLAY (WM_APP+2)
  31. #define FONT_SMALL_SIZE 20 // show message in a big font
  32. #define FONT_LARGE_SIZE 48 // show message in a big font
  33. int g_guiShowFirst = 1;
  34. struct sp_gui_t
  35. {
  36. #ifdef _WIN32
  37. HINSTANCE hInst;
  38. HWND hWnd;
  39. HWND hWndEditBox;
  40. HFONT hSmallFont;
  41. HFONT hLargeFont;
  42. //HPEN hPen;
  43. HBRUSH hBkBrush;
  44. BOOL bShow;
  45. HANDLE hThreadWorker;
  46. int iNotifyResult;
  47. HANDLE hEventNotify;
  48. BOOL bBlueScreen;
  49. ULONG_PTR pGDIToken;
  50. #endif // _WIN32
  51. };
  52. #ifdef _WIN32
  53. static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  54. static int GetTotalEntityCount()
  55. {
  56. auto env = sp_get_env();
  57. if (env == NULL) {
  58. return getEntityResource()->m_EntityCount;
  59. }
  60. auto cfg = env->cfg;
  61. if (cfg == NULL) {
  62. return getEntityResource()->m_EntityCount;
  63. }
  64. return cfg->shell_ini->arr_entity->nelts;
  65. }
  66. static const wchar_t *GetTerminalInfo()
  67. {
  68. static wchar_t wszInfo[256] = {};
  69. if (wszInfo[0] != 0)
  70. return wszInfo;
  71. auto env = sp_get_env();
  72. if (env == NULL) {
  73. return NULL;
  74. }
  75. auto cfg = env->cfg;
  76. if (cfg == NULL) {
  77. return NULL;
  78. }
  79. swprintf(wszInfo, L"%S %S V%d.%d.%d.%d",
  80. cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no,
  81. cfg->root_ini->site == NULL ? "" : cfg->root_ini->site,
  82. cfg->install_ini->install_version.major,
  83. cfg->install_ini->install_version.minor,
  84. cfg->install_ini->install_version.revision,
  85. cfg->install_ini->install_version.build);
  86. return wszInfo;
  87. }
  88. static int OnCreate(sp_gui_t *gui, HWND hWnd, WPARAM wParam, LPARAM lParam)
  89. {
  90. HDC hdc;
  91. long lfHeight;
  92. LPCREATESTRUCTA lpCreateStruct = (LPCREATESTRUCTA)lParam;
  93. int nLogPixelsY;
  94. //gui->hWndEditBox = CreateWindowA("STATIC", "", WS_VISIBLE | WS_CHILD/*|SS_CENTER*/, 20, 20,
  95. // lpCreateStruct->cx - 20, lpCreateStruct->cy - 20, hWnd, (HMENU)101, gui->hInst, NULL);
  96. //gui->hWndEditBox = CreateWindowA("EDIT", "", WS_VISIBLE | WS_CHILD | ES_READONLY | ES_MULTILINE | ES_AUTOVSCROLL,
  97. // 10, 10, lpCreateStruct->cx - 20, lpCreateStruct->cy - 20, hWnd, (HMENU)101, gui->hInst, NULL);
  98. hdc = GetDC(NULL);
  99. nLogPixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
  100. ReleaseDC(NULL, hdc);
  101. lfHeight = -MulDiv(FONT_SMALL_SIZE, nLogPixelsY, 72);
  102. //gui->hSmallFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
  103. gui->hSmallFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "微软雅黑");
  104. lfHeight = -MulDiv(FONT_LARGE_SIZE, nLogPixelsY, 72);
  105. //gui->hLargeFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
  106. gui->hLargeFont = CreateFontA(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "微软雅黑");
  107. //SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT);
  108. return 0;
  109. }
  110. static int OnDestroy(sp_gui_t *gui, HWND hWnd, WPARAM wParam, LPARAM lParam)
  111. {
  112. DestroyWindow(gui->hWndEditBox);
  113. gui->hWndEditBox = NULL;
  114. DeleteObject(gui->hSmallFont);
  115. DeleteObject(gui->hLargeFont);
  116. PostQuitMessage(0);
  117. return 0;
  118. }
  119. typedef void (WINAPI *PSwitchToThisWindow)(HWND, BOOL);
  120. static int init(sp_gui_t *gui)
  121. {
  122. //GdiPlus初始化
  123. GdiplusStartupInput gdiplusInput;
  124. GdiplusStartup(&gui->pGDIToken, &gdiplusInput, NULL);
  125. // 窗口类注册
  126. WNDCLASSA wc = {0};
  127. gui->hInst = GetModuleHandleA(NULL);
  128. gui->hBkBrush = wc.hbrBackground = CreateSolidBrush(BLUE_COLOR);
  129. wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
  130. wc.hIcon = LoadIconA(NULL, MAKEINTRESOURCEA(IDI_APPLICATION));
  131. wc.hInstance = gui->hInst;
  132. wc.lpfnWndProc = &WndProc;
  133. wc.lpszClassName = SHELL_WND_CLS;
  134. wc.style = CS_HREDRAW | CS_VREDRAW;
  135. if (RegisterClassA(&wc) == 0)
  136. return -1;
  137. gui->hWnd = CreateWindowExA(WS_EX_TOOLWINDOW, wc.lpszClassName, "", WS_POPUP|WS_BORDER,
  138. 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, gui->hInst, gui);
  139. if (gui->hWnd == NULL)
  140. return Error_Unexpect;
  141. gui->bShow = TRUE;
  142. RegisterTouchWindow(gui->hWnd, 0); // support WM_TOUCH
  143. ShowWindow(gui->hWnd, g_guiShowFirst ? SW_SHOW : SW_HIDE);
  144. UpdateWindow(gui->hWnd);
  145. //BringWindowToTop(gui->hWnd);
  146. //SetWindowPos(gui->hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  147. return 0;
  148. }
  149. static int term(sp_gui_t *gui)
  150. {
  151. //DestroyWindow(gui->hWnd);
  152. //gui->hWnd = NULL;
  153. UnregisterClassA(SHELL_WND_CLS, gui->hInst);
  154. DeleteObject(gui->hBkBrush);
  155. gui->hBkBrush = NULL;
  156. //GdiPlus 取消初始化
  157. GdiplusShutdown(gui->pGDIToken);
  158. return 0;
  159. }
  160. static unsigned int __stdcall work_proc(void *param)
  161. {
  162. sp_gui_t *gui = (sp_gui_t*)param;
  163. int rc;
  164. MSG msg;
  165. rc = init(gui);
  166. gui->iNotifyResult = rc;
  167. SetEvent(gui->hEventNotify);
  168. if (rc != 0)
  169. return rc;
  170. //--> will disable the Display and Sleep Idle Timeouts .
  171. SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
  172. while (GetMessageA(&msg, NULL, 0, 0)) {
  173. TranslateMessage(&msg);
  174. DispatchMessageA(&msg);
  175. }
  176. SetThreadExecutionState(ES_CONTINUOUS);
  177. term(gui);
  178. return 0;
  179. }
  180. // 从资源中读出Logo
  181. static int LoadLogoImage(int nResourceID, Image *&pImage)
  182. {
  183. auto hMod = GetModuleHandle("spbase");
  184. auto hResInfo = FindResource(hMod, MAKEINTRESOURCE(nResourceID), TEXT("PNG"));
  185. if (hResInfo == NULL)
  186. return 1;
  187. auto hResData = LoadResource(hMod, hResInfo);
  188. if (hResData == NULL)
  189. return 2;
  190. auto pResData = LockResource(hResData);
  191. if (pResData == NULL)
  192. return 3;
  193. int iSize = SizeofResource(hMod, hResInfo);
  194. if (iSize <= 0)
  195. return 4;
  196. HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, iSize);
  197. if (!hMem)
  198. {
  199. UnlockResource(hResData);
  200. return 5;
  201. }
  202. LPVOID pTemp = ::GlobalLock(hMem);
  203. if (pTemp == NULL)
  204. {
  205. UnlockResource(hResData);
  206. GlobalFree(hMem);
  207. return 6;
  208. }
  209. // copy data
  210. memcpy(pTemp, pResData, iSize);
  211. ::GlobalUnlock(hMem);
  212. UnlockResource(hResData);
  213. CComPtr<IStream> spStream;
  214. HRESULT hr = ::CreateStreamOnHGlobal(hMem, TRUE, &spStream);
  215. if (!SUCCEEDED(hr))
  216. {
  217. GlobalFree(hMem);
  218. return 7;
  219. }
  220. pImage = Image::FromStream(spStream);
  221. if (pImage == NULL)
  222. return 8;
  223. return 0;
  224. }
  225. static int DrawConsoleTitle(Graphics &graphics, int nLeftWidth, int nRightWidth)
  226. {
  227. FontFamily fontFamily(L"宋体");
  228. Font font(&fontFamily, 18, FontStyleBold, UnitPoint);
  229. RectF rectF(nLeftWidth, 0, nRightWidth, nRightWidth / 3);
  230. StringFormat format;
  231. format.SetAlignment(StringAlignmentCenter);
  232. format.SetLineAlignment(StringAlignmentCenter);
  233. SolidBrush fontBrush(Color(255, 67, 67, 67));
  234. CSimpleStringW strMsg= TEXT("招商银行视频柜台");
  235. graphics.DrawString(strMsg, strMsg.GetLength(), &font, rectF, &format, &fontBrush);
  236. return 0;
  237. }
  238. static int DrawBackground(Graphics &graphics, int nLeftWidth, int nRightWidth, int nHeight)
  239. {
  240. // draw left
  241. SolidBrush leftBrush(Color(255, 101, 105, 108));
  242. Pen leftPen(&leftBrush);
  243. graphics.DrawRectangle(&leftPen, 0, 0, nLeftWidth, nHeight);
  244. graphics.FillRectangle(&leftBrush, 0, 0, nLeftWidth, nHeight);
  245. // draw right
  246. SolidBrush rightBrush(Color(255, 161, 163, 165));
  247. Pen rightPen(&rightBrush);
  248. graphics.DrawRectangle(&rightPen, nLeftWidth, 0, nRightWidth, nHeight);
  249. graphics.FillRectangle(&rightBrush, nLeftWidth, 0, nRightWidth, nHeight);
  250. // draw logo
  251. Image *pLogoImage = NULL;
  252. if (LoadLogoImage(IDB_VTMLOGO, pLogoImage) !=0)
  253. {
  254. // 直接输出可视柜台文字
  255. DrawConsoleTitle(graphics, nLeftWidth, nRightWidth);
  256. }
  257. else
  258. {
  259. graphics.DrawImage(pLogoImage, nLeftWidth + nRightWidth / 8, 20, nRightWidth * 3 / 4, nRightWidth * 3 / 4 / 3);
  260. }
  261. delete pLogoImage;
  262. return 0;
  263. }
  264. static int DrawLineString(Graphics &graphics, wstring &msg, Font *font, RectF &rect, StringFormat *format, Brush *brush, int nLineSpace, int &nNextLinePosY)
  265. {
  266. nNextLinePosY = rect.Y;
  267. // 一次最多32个
  268. CharacterRange crs[32];
  269. for (int i = 0; i < 32; i++)
  270. {
  271. crs[i].First = i;
  272. crs[i].Length = 1;
  273. }
  274. format->SetFormatFlags(StringFormatFlagsNoClip);
  275. // 当前输出区域
  276. RectF layoutRect = rect;
  277. int nDelta = 60; // 每行输出留白空间
  278. int nFrom = 0;
  279. int nLineCount = 0;
  280. int nTotalWidth = 0;
  281. int nLineHeight = 0;
  282. int nMsgLen = msg.length();
  283. while (nFrom + nLineCount < nMsgLen)
  284. {
  285. Region regions[32] = {};
  286. int nCount = 32;
  287. if (nFrom + nLineCount + nCount > nMsgLen)
  288. nCount = nMsgLen - nFrom - nLineCount;
  289. format->SetMeasurableCharacterRanges(nCount, crs);
  290. //tempMsg = msg.substr(nFrom + nLineCount, nCount);
  291. graphics.MeasureCharacterRanges(msg.data() + nFrom + nLineCount, nCount, font, layoutRect, format, nCount, regions);
  292. for (int i = 0; i < nCount; i++)
  293. {
  294. Rect r;
  295. regions[i].GetBounds(&r, &graphics);
  296. nTotalWidth += r.Width;
  297. if (r.Height > nLineHeight)
  298. nLineHeight = r.Height;
  299. if (nTotalWidth <= layoutRect.Width - nDelta && msg[nFrom + nLineCount] != TEXT('\r') && msg[nFrom + nLineCount] != TEXT('\n'))
  300. {
  301. nLineCount++;
  302. }
  303. else
  304. {
  305. if (msg[nFrom + nLineCount] == TEXT('\r') || msg[nFrom + nLineCount] == TEXT('\n'))
  306. {
  307. if (nFrom + nLineCount + 1 < nMsgLen
  308. && ((msg[nFrom + nLineCount] == TEXT('\r') && msg[nFrom + nLineCount + 1] == TEXT('\n'))
  309. || (msg[nFrom + nLineCount] == TEXT('\n') && msg[nFrom + nLineCount + 1] == TEXT('\r'))))
  310. {
  311. nLineCount += 2;
  312. i++;
  313. }
  314. else
  315. nLineCount++;
  316. }
  317. //auto wszLine = msg.substr(nFrom, nLineCount);
  318. graphics.DrawString(msg.data() + nFrom, nLineCount, font, layoutRect, format, brush);
  319. // reset vars
  320. nFrom += nLineCount;
  321. nLineCount = 0;
  322. nTotalWidth = 0;
  323. layoutRect.Y += nLineHeight + nLineSpace;
  324. //nLineHeight = 0;
  325. nNextLinePosY = layoutRect.Y;
  326. // 写满显示高度,直接返回
  327. if (layoutRect.Y >= rect.Y + rect.Height)
  328. return 0;
  329. }
  330. }
  331. }
  332. // output last line
  333. if (nLineCount > 0)
  334. {
  335. graphics.DrawString(msg.data() + nFrom, nLineCount, font, layoutRect, format, brush);
  336. nNextLinePosY += nLineHeight + nLineSpace;
  337. }
  338. return 0;
  339. }
  340. static int DrawRunningInfo(Graphics &graphics, int nLeftWidth, int nRightWidth, int nHeight)
  341. {
  342. // 全部输出
  343. // get lock
  344. auto entityRes = getEntityResource();
  345. while (InterlockedCompareExchange(&entityRes->m_Locking, 1, 0) == 1)
  346. {
  347. Sleep(100);
  348. }
  349. // draw left
  350. wstring strLeft;
  351. FontFamily fontFamily(L"微软雅黑");
  352. SolidBrush blackBrush(Color(255, 0, 0, 0));
  353. SolidBrush whiteBrush(Color(255, 255, 255, 255));
  354. SolidBrush redBrush(Color(255, 174, 0, 0));
  355. SolidBrush redBrush2(Color(255, 130, 0, 0));
  356. if (entityRes->m_InBlueScreenMode)
  357. {
  358. strLeft = entityRes->m_BlueScreenMsg;
  359. Font font(&fontFamily, 32, FontStyleBold, UnitPoint);
  360. StringFormat format;
  361. format.SetFormatFlags(StringFormatFlagsNoClip);
  362. format.SetLineAlignment(StringAlignmentCenter);
  363. RectF rectF(10, 10, nLeftWidth - 20, nHeight - 20);
  364. graphics.DrawString(strLeft.c_str(), strLeft.length(), &font, rectF, &format, &whiteBrush);
  365. }
  366. else
  367. {
  368. Font font(&fontFamily, 10, FontStyleRegular, UnitPoint);
  369. StringFormat format;
  370. int nNextLinePosY = 10;
  371. auto it = entityRes->m_OutputMsgs.rbegin();
  372. if (entityRes->m_InBrowseMode)
  373. {
  374. for (int i = 0; i < entityRes->m_SkipLineNum && i < (int)entityRes->m_OutputMsgs.size(); i++)
  375. it++;
  376. }
  377. for (; it != entityRes->m_OutputMsgs.rend() && nNextLinePosY < nHeight; it++)
  378. {
  379. auto &strLine = *it;
  380. // output
  381. Brush *brush = NULL;
  382. if (wcsstr(strLine.c_str(), L"] W:{") != NULL)
  383. brush = &whiteBrush;
  384. else if (wcsstr(strLine.c_str(), L"] E:{") != NULL)
  385. brush = &redBrush2;
  386. else
  387. brush = &blackBrush;
  388. RectF rectF(10, nNextLinePosY, nLeftWidth - 20, nHeight - 20);
  389. DrawLineString(graphics, strLine, &font, rectF, &format, brush, 5, nNextLinePosY);
  390. }
  391. }
  392. // draw right
  393. wstring strLostEntitys;
  394. wstring strSum;
  395. int nStartedCount = 0;
  396. int nLostCount = 0;
  397. int nToStartCount = 0;
  398. wchar_t strTmp[128] = {};
  399. for (int i = 0; i < entityRes->m_EntityCount; i++)
  400. {
  401. if (entityRes->m_EntitysInfo[i].EntityState >= 2 && entityRes->m_EntitysInfo[i].EntityState <= 4)
  402. {
  403. nStartedCount++;
  404. }
  405. else if (entityRes->m_EntitysInfo[i].EntityState == 6)
  406. {
  407. nLostCount++;
  408. swprintf_s(strTmp, 128, L" 模块[%s]启动失败\r\n", entityRes->m_EntitysInfo[i].EntityName.c_str());
  409. strLostEntitys += strTmp;
  410. }
  411. }
  412. // 输出终端号、场所、版本
  413. StringFormat centerFormat;
  414. centerFormat.SetAlignment(StringAlignmentCenter);
  415. auto wszTerminalInfo = GetTerminalInfo();
  416. if (wszTerminalInfo != NULL)
  417. {
  418. Font rfont(&fontFamily, 10, FontStyleRegular, UnitPoint);
  419. RectF rrectF(nLeftWidth + 10, 110, nRightWidth - 20, nHeight - 180);
  420. int nNextLinePosY;
  421. DrawLineString(graphics, wstring(wszTerminalInfo), &rfont, rrectF, &centerFormat, &blackBrush, 20, nNextLinePosY);
  422. }
  423. // 实体启动成功计数
  424. Font rfont(&fontFamily, 12, FontStyleBold, UnitPoint);
  425. swprintf_s(strTmp, 128, L"%d/%d 个模块启动成功", nStartedCount, GetTotalEntityCount());
  426. strSum = strTmp;
  427. int nNextLinePosY;
  428. RectF rrectF(nLeftWidth + 10, 140, nRightWidth - 20, nHeight - 180);
  429. DrawLineString(graphics, strSum, &rfont, rrectF, &centerFormat, &blackBrush, 20, nNextLinePosY);
  430. // 输出当前启动状态信息
  431. Font rfont2(&fontFamily, 11, FontStyleRegular, UnitPoint);
  432. if (entityRes->m_StartupInfo.length() > 0)
  433. {
  434. RectF rrectF2(nLeftWidth + 6, nNextLinePosY, nRightWidth, nHeight - 180);
  435. DrawLineString(graphics, entityRes->m_StartupInfo, &rfont2, rrectF2, &centerFormat, &whiteBrush, 5, nNextLinePosY);
  436. nNextLinePosY += 10;
  437. }
  438. // 输出实体启动错误
  439. StringFormat rformat2;
  440. {
  441. RectF rrectF2(nLeftWidth + 30, nNextLinePosY, nRightWidth, nHeight - 200);
  442. DrawLineString(graphics, strLostEntitys, &rfont2, rrectF2, &rformat2, &redBrush, 5, nNextLinePosY);
  443. }
  444. // 输出严重错误信息
  445. for (auto it2 = entityRes->m_FatalMsgs.rbegin(); it2!= entityRes->m_FatalMsgs.rend() && nNextLinePosY < nHeight; it2++)
  446. {
  447. RectF rrectF2(nLeftWidth + 30, nNextLinePosY, nRightWidth, nHeight - 200);
  448. wstring strLine = L"\r\n 【" + *it2+ L"】";
  449. DrawLineString(graphics, strLine, &rfont2, rrectF2, &rformat2, &redBrush, 5, nNextLinePosY);
  450. }
  451. // release lock
  452. InterlockedExchange(&entityRes->m_Locking, 0);
  453. return 0;
  454. }
  455. static int RepaintWindow(HWND hWnd, bool bCanUseCache)
  456. {
  457. static RECT lastRect = {};
  458. static HDC hBkgMemDC = NULL;
  459. RECT rect = {};
  460. GetClientRect(hWnd, &rect);
  461. HDC hDC = GetDC(hWnd);
  462. if (lastRect.right == rect.right && lastRect.bottom == rect.bottom && hBkgMemDC != NULL && bCanUseCache)
  463. {
  464. // copy mem dc to hwnd dc
  465. //MessageBox(NULL, TEXT("use cached dc"), NULL, 0);
  466. BitBlt(hDC, 0, 0, rect.right, rect.bottom, hBkgMemDC, 0, 0, SRCCOPY);
  467. ReleaseDC(hWnd, hDC);
  468. return 0;
  469. }
  470. if (hBkgMemDC != NULL)
  471. {
  472. DeleteDC(hBkgMemDC);
  473. hBkgMemDC = NULL;
  474. }
  475. lastRect = rect;
  476. int nWidth = rect.right - rect.left;
  477. int nHeight = rect.bottom - rect.top;
  478. int nRightWidth = nWidth / 3;
  479. if (nRightWidth > 450)
  480. nRightWidth = 450;
  481. int nLeftWidth = nWidth - nRightWidth;
  482. hBkgMemDC = CreateCompatibleDC(hDC);
  483. auto hMemBitmap = CreateCompatibleBitmap(hDC, nWidth, nHeight);
  484. auto hOldObject = SelectObject(hBkgMemDC, hMemBitmap);
  485. Graphics graphics(hBkgMemDC);
  486. // draw background image
  487. DrawBackground(graphics, nLeftWidth, nRightWidth, nHeight);
  488. // draw output string
  489. DrawRunningInfo(graphics, nLeftWidth, nRightWidth, nHeight);
  490. // copy mem dc to hwnd dc
  491. BitBlt(hDC, 0, 0, rect.right, rect.bottom, hBkgMemDC, 0, 0, SRCCOPY);
  492. SelectObject(hBkgMemDC, hOldObject);
  493. DeleteObject(hMemBitmap);
  494. //DeleteDC(hBkgMemDC);
  495. ReleaseDC(hWnd, hDC);
  496. return 0;
  497. }
  498. static void SetBrowseModeShift(int nShiftLines)
  499. {
  500. if ((!getEntityResource()->m_InBrowseMode && nShiftLines <= 0) || getEntityResource()->m_OutputMsgs.size() <=20)
  501. return;
  502. // get lock
  503. auto entityRes = getEntityResource();
  504. while (InterlockedCompareExchange(&entityRes->m_Locking, 1, 0) == 1)
  505. {
  506. Sleep(100);
  507. }
  508. getEntityResource()->m_InBrowseMode = true;
  509. getEntityResource()->m_SkipLineNum += nShiftLines;
  510. if (nShiftLines > 0)
  511. {
  512. if (getEntityResource()->m_SkipLineNum + 20 > (int)getEntityResource()->m_OutputMsgs.size())
  513. {
  514. getEntityResource()->m_SkipLineNum = (int)getEntityResource()->m_OutputMsgs.size() - 20;
  515. }
  516. }
  517. else
  518. {
  519. if (getEntityResource()->m_SkipLineNum <= 0)
  520. {
  521. // 解除浏览模式
  522. getEntityResource()->m_SkipLineNum = 0;
  523. getEntityResource()->m_InBrowseMode = false;
  524. }
  525. }
  526. // release lock
  527. InterlockedExchange(&getEntityResource()->m_Locking, 0);
  528. }
  529. static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  530. {
  531. sp_gui_t *gui = NULL;
  532. static POINTS lastPoint = {};
  533. if (msg == WM_CREATE) {
  534. LPCREATESTRUCTA lpCreateStruct = (LPCREATESTRUCTA)lParam;
  535. gui = (sp_gui_t*)lpCreateStruct->lpCreateParams;
  536. SetWindowLongA(hWnd, GWL_USERDATA, (LONG)gui);
  537. }
  538. else {
  539. gui = (sp_gui_t*)GetWindowLongA(hWnd, GWL_USERDATA);
  540. }
  541. switch (msg) {
  542. case WM_CREATE:
  543. {
  544. if (OnCreate(gui, hWnd, wParam, lParam) != 0)
  545. return -1;
  546. }
  547. break;
  548. case WM_CLOSE:
  549. DestroyWindow(hWnd);
  550. break;
  551. case WM_MOUSEWHEEL:
  552. {
  553. getEntityResource()->m_LastShiftTick = GetTickCount();
  554. short sDelta = (short)HIWORD(wParam);
  555. SetBrowseModeShift(sDelta > 0 ? 10 : -10);
  556. RepaintWindow(hWnd, false);
  557. }
  558. break;
  559. case WM_TOUCH:
  560. {
  561. getEntityResource()->m_LastShiftTick = GetTickCount();
  562. DWORD nInputNum = (DWORD)wParam;
  563. TOUCHINPUT *tis = new TOUCHINPUT[nInputNum];\
  564. if (GetTouchInputInfo((HTOUCHINPUT)lParam, nInputNum, tis, sizeof(TOUCHINPUT)))
  565. {
  566. for (int i = 0; i < nInputNum; i++)
  567. {
  568. POINT pt = {};
  569. pt.x = TOUCH_COORD_TO_PIXEL(tis[i].x);
  570. pt.y = TOUCH_COORD_TO_PIXEL(tis[i].y);
  571. ScreenToClient(gui->hWnd, &pt);
  572. if (tis[i].dwFlags & TOUCHEVENTF_DOWN)
  573. {
  574. getEntityResource()->m_LastTouchID = tis[i].dwID;
  575. getEntityResource()->m_LastTouchYPos = pt.y;
  576. }
  577. else if (tis[i].dwFlags & TOUCHEVENTF_MOVE)
  578. {
  579. }
  580. else if (tis[i].dwFlags & TOUCHEVENTF_UP)
  581. {
  582. if (tis[i].dwID == getEntityResource()->m_LastTouchID)
  583. {
  584. SetBrowseModeShift(pt.y < getEntityResource()->m_LastTouchYPos ? 10 : -10);
  585. RepaintWindow(hWnd, false);
  586. getEntityResource()->m_LastTouchID = 0;
  587. getEntityResource()->m_LastTouchYPos = 0;
  588. }
  589. }
  590. }
  591. CloseTouchInputHandle((HTOUCHINPUT)lParam);
  592. delete[] tis;
  593. }
  594. }
  595. break;
  596. case WM_DESTROY:
  597. OnDestroy(gui, hWnd, wParam, lParam);
  598. break;
  599. case WM_PAINT:
  600. {
  601. PAINTSTRUCT ps;
  602. HDC hdc = BeginPaint(hWnd, &ps);
  603. RepaintWindow(hWnd, true);
  604. EndPaint(hWnd, &ps);
  605. }
  606. break;
  607. case WM_KEYDOWN:
  608. {
  609. if (wParam == VK_F3) { // hide
  610. SendMessageA(hWnd, WM_UNDISPLAY, 0, 0);
  611. }
  612. }
  613. break;
  614. case WM_CTLCOLORSTATIC:
  615. {
  616. HDC hDC = (HDC)wParam;
  617. HWND hWndControl = (HWND)lParam;
  618. SetBkColor(hDC, BLUE_COLOR);
  619. SetTextColor(hDC, WHITE_COLOR);
  620. return (LRESULT)gui->hBkBrush;
  621. }
  622. break;
  623. case WM_DISPLAY:
  624. {
  625. RepaintWindow(hWnd, false);
  626. if (getEntityResource()->m_InBlueScreenMode && !gui->bShow)
  627. {
  628. gui->bShow = TRUE;
  629. ShowWindow(gui->hWnd, SW_SHOW);
  630. }
  631. }
  632. break;
  633. case WM_UNDISPLAY:
  634. {
  635. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("WM_UNDISPLAY, show gui undisplay");
  636. ShowWindow(hWnd, SW_HIDE);
  637. UpdateWindow(hWnd);
  638. gui->bShow = FALSE;
  639. }
  640. break;
  641. default:
  642. return DefWindowProc(hWnd, msg, wParam, lParam);
  643. }
  644. return 0;
  645. }
  646. #endif //_WIN32
  647. void sp_gui_setShow(int isShowFirst)
  648. {
  649. g_guiShowFirst = isShowFirst;
  650. }
  651. int sp_gui_create(sp_gui_t **p_gui)
  652. {
  653. sp_gui_t *gui = ZALLOC_T(sp_gui_t);
  654. #ifdef RVC_OS_WIN
  655. gui->hEventNotify = CreateEventA(NULL, TRUE, FALSE, NULL);
  656. if (!gui->hEventNotify)
  657. goto on_error;
  658. gui->hThreadWorker = (HANDLE)_beginthreadex(NULL, 0, &work_proc, gui, 0, NULL);
  659. if (!gui->hThreadWorker)
  660. goto on_error;
  661. WaitForSingleObject(gui->hEventNotify, INFINITE);
  662. if (gui->iNotifyResult)
  663. goto on_error;
  664. #endif //RVC_OS_WIN
  665. *p_gui = gui;
  666. return 0;
  667. #ifdef RVC_OS_WIN
  668. on_error :
  669. if (gui->hThreadWorker)
  670. CloseHandle(gui->hThreadWorker);
  671. if (gui->hEventNotify)
  672. CloseHandle(gui->hEventNotify);
  673. free(gui);
  674. return Error_Unexpect;
  675. #endif //RVC_OS_WIN
  676. }
  677. void sp_gui_destroy(sp_gui_t *gui)
  678. {
  679. #ifdef _WIN32
  680. PostMessageA(gui->hWnd, WM_CLOSE, 0, 0);
  681. WaitForSingleObject(gui->hThreadWorker, INFINITE);
  682. CloseHandle(gui->hThreadWorker);
  683. CloseHandle(gui->hEventNotify);
  684. #endif //_WIN32
  685. free(gui);
  686. }
  687. #if defined(_MSC_VER)
  688. int sp_gui_show_running_info(sp_gui_t* gui, const char* pMsg, int type)
  689. {
  690. // get lock
  691. auto entityRes = getEntityResource();
  692. while (InterlockedCompareExchange(&entityRes->m_Locking, 1, 0) == 1)
  693. Sleep(100);
  694. SYSTEMTIME st = {};
  695. GetLocalTime(&st);
  696. wchar_t szBuf[1024] = {};
  697. CSimpleStringA curMsg = pMsg;
  698. CSimpleStringW dstMsg = CSimpleStringA2W(curMsg);
  699. dstMsg = dstMsg.SubString(0, 1000);
  700. if (type == 1) {
  701. // bluescreen
  702. entityRes->m_InBlueScreenMode = true;
  703. swprintf_s(szBuf, 1024, L"%s\r\n", dstMsg.GetData());
  704. entityRes->m_BlueScreenMsg = szBuf;
  705. } else if (type == 2) {
  706. // fatal error
  707. swprintf_s(szBuf, 1024, L"%s", dstMsg.GetData());
  708. entityRes->m_FatalMsgs.push_back(szBuf);
  709. if (entityRes->m_FatalMsgs.size() > 100)
  710. entityRes->m_FatalMsgs.pop_front();
  711. } else if (type == 3) {
  712. // important startup info
  713. swprintf_s(szBuf, 1024, L"%s", dstMsg.GetData());
  714. entityRes->m_StartupInfo = szBuf;
  715. } else {
  716. // startup info
  717. swprintf_s(szBuf, 1024, L"[%02d:%02d:%02d] %s\r\n", st.wHour, st.wMinute, st.wSecond, dstMsg.GetData());
  718. entityRes->m_OutputMsgs.push_back(szBuf);
  719. // 只保留最近500条
  720. if (entityRes->m_OutputMsgs.size() > 500)
  721. entityRes->m_OutputMsgs.pop_front();
  722. }
  723. // browse mode continues 30s
  724. if (entityRes->m_InBrowseMode && GetTickCount() - entityRes->m_LastShiftTick >= 30000) {
  725. entityRes->m_InBrowseMode = false;
  726. entityRes->m_SkipLineNum = 0;
  727. }
  728. // release lock
  729. InterlockedExchange(&entityRes->m_Locking, 0);
  730. if (type >= 1 || (!entityRes->m_InBlueScreenMode && !entityRes->m_InBrowseMode)) {
  731. if (!PostMessageA(gui->hWnd, WM_DISPLAY, NULL, NULL)) {
  732. return Error_Unexpect;
  733. }
  734. }
  735. return 0;
  736. }
  737. int sp_gui_show_entity_info(sp_gui_t* gui, const char* entity, int state)
  738. {
  739. auto strEntity = CSimpleStringA2W(CSimpleStringA(entity));
  740. auto entityRes = getEntityResource();
  741. int i = 0;
  742. for (; i < entityRes->m_EntityCount; i++) {
  743. if (wcsicmp(strEntity, entityRes->m_EntitysInfo[i].EntityName.c_str()) == 0) {
  744. entityRes->m_EntitysInfo[i].EntityState = state;
  745. break;
  746. }
  747. }
  748. if (i == entityRes->m_EntityCount && entityRes->m_EntityCount < 80) {
  749. // new entity
  750. entityRes->m_EntitysInfo[i].EntityName = strEntity;
  751. entityRes->m_EntitysInfo[i].EntityState = state;
  752. entityRes->m_EntityCount++;
  753. }
  754. if (!PostMessageA(gui->hWnd, WM_DISPLAY, NULL, NULL)) {
  755. return Error_Unexpect;
  756. }
  757. return 0;
  758. }
  759. int sp_gui_display(sp_gui_t* gui)
  760. {
  761. if (!gui->bShow) {
  762. gui->bShow = TRUE;
  763. HWND hShellWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
  764. if (hShellWnd != NULL)
  765. SendMessage(hShellWnd, WM_COMMAND, 419, 0);
  766. ShowWindow(gui->hWnd, SW_SHOW);
  767. }
  768. return 0;
  769. }
  770. int sp_gui_undisplay(sp_gui_t* gui)
  771. {
  772. getEntityResource()->m_InBlueScreenMode = false;
  773. if (!PostMessageA(gui->hWnd, WM_UNDISPLAY, 0, 0))
  774. return Error_Unexpect;
  775. return 0;
  776. }
  777. #else
  778. int sp_gui_create_format(sp_gui_format_t** p_gui)
  779. {
  780. int result = 0;
  781. sp_gui_format_t* gui = ZALLOC_T(sp_gui_format_t);
  782. sp_gui_t* inst = NULL;
  783. result = sp_gui_create(&inst);
  784. if (result == 0) {
  785. gui->gui_inst = inst;
  786. gui->display = &sp_gui_display;
  787. gui->hide = &sp_gui_undisplay;
  788. gui->show_entity_info = &sp_gui_show_entity_info;
  789. gui->show_running_info = &sp_gui_show_running_info;
  790. gui->post_message = &sp_gui_post_message;
  791. *p_gui = gui;
  792. } else {
  793. FREE(gui);
  794. }
  795. return result;
  796. }
  797. void sp_gui_destroy_format(sp_gui_format_t* p_gui)
  798. {
  799. sp_gui_destroy((sp_gui_t*)p_gui->gui_inst);
  800. FREE(p_gui);
  801. }
  802. int sp_gui_show_running_info(void* data, const char* pMsg, int type)
  803. {
  804. sp_gui_t* gui = (sp_gui_t*)data;
  805. return 0;
  806. }
  807. int sp_gui_show_entity_info(void* data, const char* entity, int state)
  808. {
  809. //void* data
  810. sp_gui_t* gui = (sp_gui_t*)data;
  811. return 0;
  812. }
  813. int sp_gui_display(void* data)
  814. {
  815. //void* data
  816. sp_gui_t* gui = (sp_gui_t*)data;
  817. return 0;
  818. }
  819. int sp_gui_undisplay(void* data)
  820. {
  821. //void* data
  822. sp_gui_t* gui = (sp_gui_t*)data;
  823. return 0;
  824. }
  825. void sp_gui_post_message(void* gui, unsigned long param1, unsigned long param2)
  826. {
  827. return;
  828. }
  829. #endif //_MSC_VER