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