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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
static const LPCWSTR THMVWR_WINDOW_CLASS_DISPLAY = L"ThmViewerDisplay";
struct DISPLAY_THREAD_CONTEXT
{
HWND hWnd;
HINSTANCE hInstance;
HANDLE hInit;
};
static DWORD WINAPI DisplayThreadProc(
__in LPVOID pvContext
);
static LRESULT CALLBACK DisplayWndProc(
__in HWND hWnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
);
static BOOL DisplayOnThmLoadedControl(
__in THEME* pTheme,
__in const THEME_LOADEDCONTROL_ARGS* args,
__in THEME_LOADEDCONTROL_RESULTS* results
);
extern "C" HRESULT DisplayStart(
__in HINSTANCE hInstance,
__in HWND hWnd,
__out HANDLE *phThread,
__out DWORD* pdwThreadId
)
{
HRESULT hr = S_OK;
HANDLE rgHandles[2] = { };
DISPLAY_THREAD_CONTEXT context = { };
rgHandles[0] = ::CreateEventW(NULL, TRUE, FALSE, NULL);
ExitOnNullWithLastError(rgHandles[0], hr, "Failed to create load init event.");
context.hWnd = hWnd;
context.hInstance = hInstance;
context.hInit = rgHandles[0];
rgHandles[1] = ::CreateThread(NULL, 0, DisplayThreadProc, reinterpret_cast<LPVOID>(&context), 0, pdwThreadId);
ExitOnNullWithLastError(rgHandles[1], hr, "Failed to create display thread.");
::WaitForMultipleObjects(countof(rgHandles), rgHandles, FALSE, INFINITE);
*phThread = rgHandles[1];
rgHandles[1] = NULL;
LExit:
ReleaseHandle(rgHandles[1]);
ReleaseHandle(rgHandles[0]);
return hr;
}
static DWORD WINAPI DisplayThreadProc(
__in LPVOID pvContext
)
{
HRESULT hr = S_OK;
DISPLAY_THREAD_CONTEXT* pContext = static_cast<DISPLAY_THREAD_CONTEXT*>(pvContext);
HINSTANCE hInstance = pContext->hInstance;
HWND hwndParent = pContext->hWnd;
// We can signal the initialization event as soon as we have copied the context
// values into local variables.
::SetEvent(pContext->hInit);
BOOL fComInitialized = FALSE;
HANDLE_THEME* pCurrentHandle = NULL;
ATOM atomWc = 0;
WNDCLASSW wc = { };
HWND hWnd = NULL;
RECT rc = { };
int x = CW_USEDEFAULT;
int y = CW_USEDEFAULT;
BOOL fRedoMsg = FALSE;
BOOL fRet = FALSE;
MSG msg = { };
BOOL fCreateIfNecessary = FALSE;
hr = ::CoInitialize(NULL);
ExitOnFailure(hr, "Failed to initialize COM on display thread.");
fComInitialized = TRUE;
// As long as the parent window is alive and kicking, keep this thread going (with or without a theme to display ).
while (::IsWindow(hwndParent))
{
if (pCurrentHandle && fCreateIfNecessary)
{
THEME* pTheme = pCurrentHandle->pTheme;
if (CW_USEDEFAULT == x && CW_USEDEFAULT == y && ::GetWindowRect(hwndParent, &rc))
{
x = rc.left;
y = rc.bottom + 20;
}
hr = ThemeCreateParentWindow(pTheme, 0, wc.lpszClassName, pTheme->sczCaption, pTheme->dwStyle, x, y, hwndParent, hInstance, pCurrentHandle, THEME_WINDOW_INITIAL_POSITION_DEFAULT, &hWnd);
ExitOnFailure(hr, "Failed to create display window.");
fCreateIfNecessary = FALSE;
}
// message pump
while (fRedoMsg || 0 != (fRet = ::GetMessageW(&msg, NULL, 0, 0)))
{
if (fRedoMsg)
{
fRedoMsg = FALSE;
}
if (-1 == fRet)
{
hr = E_UNEXPECTED;
ExitOnFailure(hr, "Unexpected return value from display message pump.");
}
else if (NULL == msg.hwnd) // Thread message.
{
if (WM_THMVWR_NEW_THEME == msg.message)
{
// If there is already a handle, release it.
if (pCurrentHandle)
{
DecrementHandleTheme(pCurrentHandle);
pCurrentHandle = NULL;
}
// If the window was created, remember its window location before we destroy
// it so so we can open the new window in the same place.
if (::IsWindow(hWnd))
{
::GetWindowRect(hWnd, &rc);
x = rc.left;
y = rc.top;
::DestroyWindow(hWnd);
}
// If the display window class was registered, unregister it so we can
// reuse the same window class name for the new theme.
if (atomWc)
{
if (!::UnregisterClassW(reinterpret_cast<LPCWSTR>(atomWc), hInstance))
{
DWORD er = ::GetLastError();
er = er;
}
atomWc = 0;
}
// If we were provided a new theme handle, create a new window class to
// support it.
pCurrentHandle = reinterpret_cast<HANDLE_THEME*>(msg.lParam);
if (pCurrentHandle)
{
ThemeInitializeWindowClass(pCurrentHandle->pTheme, &wc, DisplayWndProc, hInstance, THMVWR_WINDOW_CLASS_DISPLAY);
atomWc = ::RegisterClassW(&wc);
if (!atomWc)
{
ExitWithLastError(hr, "Failed to register display window class.");
}
}
}
else if (WM_THMVWR_SHOWPAGE == msg.message)
{
if (pCurrentHandle && ::IsWindow(hWnd) && pCurrentHandle->pTheme->hwndParent == hWnd)
{
DWORD dwPageId = static_cast<DWORD>(msg.lParam);
int nCmdShow = static_cast<int>(msg.wParam);
// First show/hide the controls not associated with a page.
for (DWORD i = 0; i < pCurrentHandle->pTheme->cControls; ++i)
{
THEME_CONTROL* pControl = pCurrentHandle->pTheme->rgControls + i;
if (!pControl->wPageId)
{
ThemeShowControl(pControl, nCmdShow);
}
}
// If a page id was provided also, show/hide those controls
if (dwPageId)
{
// Ignore error since we aren't using variables and it can only fail when using variables.
ThemeShowPage(pCurrentHandle->pTheme, dwPageId, nCmdShow);
}
}
else // display window isn't visible or it doesn't match the current handle.
{
// Keep the current message around to try again after we break out of this loop
// and create the window.
fRedoMsg = TRUE;
fCreateIfNecessary = TRUE;
break;
}
}
}
else if (!ThemeHandleKeyboardMessage(pCurrentHandle->pTheme, hwndParent, &msg)) // Window message.
{
::TranslateMessage(&msg);
::DispatchMessageW(&msg);
}
}
}
LExit:
if (::IsWindow(hWnd))
{
::DestroyWindow(hWnd);
}
if (atomWc)
{
if (!::UnregisterClassW(THMVWR_WINDOW_CLASS_DISPLAY, hInstance))
{
DWORD er = ::GetLastError();
er = er;
}
}
DecrementHandleTheme(pCurrentHandle);
if (fComInitialized)
{
::CoUninitialize();
}
return hr;
}
static LRESULT CALLBACK DisplayWndProc(
__in HWND hWnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
)
{
static DWORD dwProgress = 0;
HANDLE_THEME* pHandleTheme = reinterpret_cast<HANDLE_THEME*>(::GetWindowLongPtrW(hWnd, GWLP_USERDATA));
switch (uMsg)
{
case WM_NCCREATE:
{
LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
pHandleTheme = reinterpret_cast<HANDLE_THEME*>(lpcs->lpCreateParams);
IncrementHandleTheme(pHandleTheme);
::SetWindowLongPtrW(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pHandleTheme));
}
break;
case WM_TIMER:
if (!lParam && SUCCEEDED(ThemeSetProgressControl(reinterpret_cast<THEME_CONTROL*>(wParam), dwProgress)))
{
dwProgress += rand() % 10 + 1;
if (dwProgress > 100)
{
dwProgress = 0;
}
return 0;
}
break;
case WM_COMMAND:
{
WCHAR wzText[1024];
::StringCchPrintfW(wzText, countof(wzText), L"Command %u\r\n", LOWORD(wParam));
OutputDebugStringW(wzText);
//::MessageBoxW(hWnd, wzText, L"Command fired", MB_OK);
}
break;
case WM_SYSCOMMAND:
{
WCHAR wzText[1024];
::StringCchPrintfW(wzText, countof(wzText), L"SysCommand %u\r\n", LOWORD(wParam));
OutputDebugStringW(wzText);
//::MessageBoxW(hWnd, wzText, L"Command fired", MB_OK);
}
break;
case WM_NCDESTROY:
DecrementHandleTheme(pHandleTheme);
::SetWindowLongPtrW(hWnd, GWLP_USERDATA, 0);
::PostQuitMessage(0);
break;
case WM_THMUTIL_LOADED_CONTROL:
if (pHandleTheme)
{
return DisplayOnThmLoadedControl(pHandleTheme->pTheme, reinterpret_cast<THEME_LOADEDCONTROL_ARGS*>(wParam), reinterpret_cast<THEME_LOADEDCONTROL_RESULTS*>(lParam));
}
}
return ThemeDefWindowProc(pHandleTheme ? pHandleTheme->pTheme : NULL, hWnd, uMsg, wParam, lParam);
}
static BOOL DisplayOnThmLoadedControl(
__in THEME* pTheme,
__in const THEME_LOADEDCONTROL_ARGS* args,
__in THEME_LOADEDCONTROL_RESULTS* results
)
{
HRESULT hr = S_OK;
const THEME_CONTROL* pControl = args->pThemeControl;
// Pre-populate some control types with data.
if (THEME_CONTROL_TYPE_RICHEDIT == pControl->type)
{
hr = WnduLoadRichEditFromResource(pControl->hWnd, MAKEINTRESOURCEA(THMVWR_RES_RICHEDIT_FILE), ::GetModuleHandleW(NULL));
ExitOnFailure(hr, "Failed to load richedit text.");
}
else if (THEME_CONTROL_TYPE_PROGRESSBAR == pControl->type)
{
UINT_PTR timerId = reinterpret_cast<UINT_PTR>(pControl);
UINT_PTR id = ::SetTimer(pTheme->hwndParent, timerId, 500, NULL);
id = id; // prevents warning in "ship" build.
Assert(id == timerId);
}
LExit:
results->hr = hr;
return TRUE;
}
|