uiUpdatingData.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "./uiUpdatingData.h"
  2. #include "./resource.h"
  3. #include <strsafe.h>
  4. #define TIMER_SHOWDIALOG_ID 1979
  5. #define TIMER_ANIMATION_ID 1978
  6. #define TIMER_ANIMATION_INTERVAL 500
  7. #define ANIMATION_CUBE_SIDE 16
  8. #define ANIMATION_CUBE_SPACING 4
  9. UpdatingDataUI::UpdatingDataUI(void)
  10. {
  11. hwnd = NULL;
  12. hThread = NULL;
  13. evntExit = NULL;
  14. evntStarted = NULL;
  15. text[0] = 0x0000;
  16. }
  17. UpdatingDataUI::~UpdatingDataUI(void)
  18. {
  19. Hide();
  20. }
  21. void UpdatingDataUI::Show(int delay,HWND ownerWnd)
  22. {
  23. wchar_t buffer[128] = {0};
  24. LoadStringW(hResource, IDS_UPDATINGDATA, buffer, 128);
  25. Show(delay, buffer, TRUE, ownerWnd);
  26. }
  27. void UpdatingDataUI::Show(int delay, const wchar_t* text, int animation, HWND ownerWnd)
  28. {
  29. this->delay = delay;
  30. this->ownerWnd = ownerWnd;
  31. StringCchCopyW(this->text, 128, text);
  32. this->animation = animation;
  33. animStep = 0;
  34. evntExit = CreateEvent(NULL, FALSE, FALSE, NULL);
  35. evntStarted = CreateEvent(NULL, FALSE, FALSE, NULL);
  36. DWORD id;
  37. hThread = CreateThread(NULL, 0, MessagePump, this, 0, &id);
  38. WaitForSingleObject(evntStarted, INFINITE);
  39. CloseHandle(evntStarted);
  40. evntStarted = NULL;
  41. }
  42. void UpdatingDataUI::Hide(void)
  43. {
  44. if (hwnd) PostMessage(hwnd, WM_CLOSE, 0, 0);
  45. if (evntExit)
  46. {
  47. WaitForSingleObject(evntExit, INFINITE);
  48. CloseHandle(evntExit);
  49. evntExit = NULL;
  50. }
  51. if (hThread)
  52. {
  53. CloseHandle(hThread);
  54. hThread = NULL;
  55. }
  56. }
  57. void UpdatingDataUI::OnInitDialog(HWND hwndDlg)
  58. {
  59. hwnd = hwndDlg;
  60. SetTimer(hwnd, TIMER_SHOWDIALOG_ID, delay, NULL);
  61. SetEvent(evntStarted);
  62. }
  63. void UpdatingDataUI::OnShowTimer(void)
  64. {
  65. KillTimer(hwnd, TIMER_SHOWDIALOG_ID);
  66. RECT rect;
  67. if (ownerWnd)
  68. {
  69. RECT ownerRect;
  70. GetWindowRect(hwnd, &rect);
  71. GetWindowRect(ownerWnd, &ownerRect);
  72. SetWindowPos(hwnd, HWND_TOPMOST, ownerRect.left + ((ownerRect.right - ownerRect.left) - (rect.right - rect.left))/2,
  73. ownerRect.top + ((ownerRect.bottom - ownerRect.top) - (rect.bottom - rect.top))/2,
  74. 0,0, SWP_NOSIZE);
  75. }
  76. SetDlgItemTextW(hwnd, IDC_LBL_TEXT, text);
  77. if (animation)
  78. {
  79. GetClientRect(hwnd, &rect);
  80. int width = (rect.right - rect.left);
  81. animStep = 0;
  82. SetRect(&animRect, 10, (rect.bottom - rect.top) - ANIMATION_CUBE_SIDE - 8, width - 8, (rect.bottom - rect.top) - 10);
  83. animMaxStep = ((animRect.right - animRect.left) + ANIMATION_CUBE_SPACING) / (ANIMATION_CUBE_SIDE + ANIMATION_CUBE_SPACING);
  84. SetTimer(hwnd, TIMER_ANIMATION_ID, TIMER_ANIMATION_INTERVAL, NULL);
  85. }
  86. else
  87. {
  88. SetRect(&animRect, 0, 0, 0, 0);
  89. animMaxStep = 0;
  90. }
  91. ShowWindow(hwnd, SW_SHOWNORMAL);
  92. UpdateWindow(hwnd);
  93. }
  94. void UpdatingDataUI::OnAnimationTimer(void)
  95. {
  96. animStep++;
  97. if (animStep == animMaxStep) animStep = 0;
  98. InvalidateRect(hwnd, &animRect, FALSE);
  99. }
  100. void UpdatingDataUI::OnDestroy(void)
  101. {
  102. hwnd = NULL;
  103. }
  104. void UpdatingDataUI::OnPaint(PAINTSTRUCT *ps)
  105. {
  106. if (RectVisible(ps->hdc, &animRect))
  107. {
  108. HBRUSH br = GetSysColorBrush(COLOR_3DFACE);
  109. HBRUSH sbr = CreateSolidBrush(RGB(254, 172, 1));
  110. HBRUSH oldBrush;
  111. HPEN oldPen, pen = CreatePen(PS_SOLID, 2, RGB(21, 72, 9));
  112. oldPen = (HPEN)SelectObject(ps->hdc, pen);
  113. oldBrush = (HBRUSH)SelectObject(ps->hdc, br);
  114. RECT cube = {animRect.left, animRect.top, animRect.left + ANIMATION_CUBE_SIDE, animRect.top + ANIMATION_CUBE_SIDE};
  115. for (int i = 0; i < animMaxStep; i++)
  116. {
  117. SelectObject(ps->hdc, (i == animStep) ? sbr : br);
  118. Rectangle(ps->hdc, cube.left, cube.top, cube.right, cube.bottom);
  119. cube.left = cube.right + ANIMATION_CUBE_SPACING;
  120. cube.right = cube.left + ANIMATION_CUBE_SIDE;
  121. }
  122. SelectObject(ps->hdc, oldPen);
  123. SelectObject(ps->hdc,oldBrush);
  124. // DeleteObject(br);
  125. DeleteObject(sbr);
  126. }
  127. }
  128. DWORD UpdatingDataUI::MessagePump(void *param)
  129. {
  130. UpdatingDataUI *object = (UpdatingDataUI*)param;
  131. LPCDLGTEMPLATE templ = NULL;
  132. HRSRC hres = FindResourceExW(hResource, MAKEINTRESOURCEW(5), MAKEINTRESOURCEW(IDD_DLG_UPDATING), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  133. if (hres) templ = (LPCDLGTEMPLATE)LoadResource(hResource, hres);
  134. HWND dlgWnd = CreateDialogIndirectParamW(dllInstance, templ, NULL, (DLGPROC)WndProc, (LPARAM)object);
  135. if (!dlgWnd) return 1;
  136. MSG msg;
  137. BOOL ret;
  138. while( 0 != (ret = GetMessageW(&msg, dlgWnd, 0, 0)))
  139. {
  140. if (ret == -1) break;
  141. if (IsDialogMessage(dlgWnd, &msg)) continue;
  142. TranslateMessage(&msg);
  143. DispatchMessageW(&msg);
  144. }
  145. SetEvent(object->evntExit);
  146. return 0;
  147. }
  148. LRESULT UpdatingDataUI::WndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  149. {
  150. static UpdatingDataUI *object = NULL;
  151. switch(uMsg)
  152. {
  153. case WM_INITDIALOG:
  154. object = (UpdatingDataUI*)lParam;
  155. object->OnInitDialog(hwndDlg);
  156. break;
  157. case WM_PAINT:
  158. {
  159. PAINTSTRUCT ps;
  160. if (BeginPaint(hwndDlg, &ps))
  161. {
  162. object->OnPaint(&ps);
  163. EndPaint(hwndDlg, &ps);
  164. }
  165. }
  166. break;
  167. case WM_CLOSE:
  168. DestroyWindow(hwndDlg);
  169. break;
  170. case WM_DESTROY:
  171. ShowWindow(hwndDlg, SW_HIDE);
  172. PostQuitMessage(1);
  173. object->OnDestroy();
  174. break;
  175. case WM_TIMER:
  176. switch(wParam)
  177. {
  178. case TIMER_SHOWDIALOG_ID:
  179. object->OnShowTimer();
  180. break;
  181. case TIMER_ANIMATION_ID:
  182. object->OnAnimationTimer();
  183. break;
  184. }
  185. break;
  186. }
  187. return 0;
  188. }