-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDGWindows.cpp
More file actions
438 lines (413 loc) · 15.6 KB
/
Copy pathCDGWindows.cpp
File metadata and controls
438 lines (413 loc) · 15.6 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "stdafx.h"
#include <windowsx.h>
#include "CDGGlobals.h"
#include "CDGBitmaps.h"
#include "CDGPrefs.h"
#include "CDGMenu.h"
#include "CDGRender.h"
#include "resource.h"
// Our CDGPro "window" is actually a sandwich of three windows that are kept "in-sync"
// positionally and "sizely". The bottommost window is the background window, which
// is always a plain colour. Then we have the logo window (which is only shown when no
// CDG file is being processed). Then we have the CDG foreground window, which shows the
// graphics from the CDG canvas.
// Window class names.
const WCHAR* g_foregroundWindowClassName = L"CDGProFG";
const WCHAR* g_backgroundWindowClassName = L"CDGProBG";
const WCHAR* g_logoWindowClassName = L"CDGProLogo";
const WCHAR* g_pszWindowCaption = L"CDG Pro";
// Application icon.
HICON g_hIcon = NULL;
// The window (and it's DC) containing the foreground.
HDC g_hForegroundWindowDC = NULL;
HWND g_hForegroundWindow = NULL;
// The window (and it's DC) containing the (optionally transparent) background.
HDC g_hBackgroundWindowDC = NULL;
HWND g_hBackgroundWindow = NULL;
// The window (and it's DC) containing the (optional) logo.
HDC g_hLogoWindowDC = NULL;
HWND g_hLogoWindow = NULL;
// Window size to restore to from fullscreen. If not currently in fullscreen mode, this is zero size.
RECT g_lastSize = { 0,0,0,0 };
// Offset between the logo window and the foreground window.
SIZE g_logoWindowOffset = { 0,0 };
// Blend function params for logo.
BLENDFUNCTION g_blendFn= { AC_SRC_OVER ,0,255,AC_SRC_ALPHA };
void CDGRectToClientRect(RECT* pRect) {
// Convert the given CDG rect coordinates to onscreen window coordinates.
// It's a complex operation!
static RECT clientRect;
::GetClientRect(g_hForegroundWindow, &clientRect);
::OffsetRect(pRect, -CDG_CANVAS_X, -CDG_CANVAS_Y);
double clientWidth = (double)clientRect.right - clientRect.left;
double clientHeight = (double)clientRect.bottom - clientRect.top;
double scaleXMultiplier = clientWidth / (double)(CDG_CANVAS_WIDTH + (double)((long long)g_nMargin << 1));
double scaleYMultiplier = clientHeight / (double)(CDG_CANVAS_HEIGHT + (double)((long long)g_nMargin << 1));
int nScaledXMargin = (int)(g_nMargin * scaleXMultiplier);
int nScaledYMargin = (int)(g_nMargin * scaleYMultiplier);
clientWidth -= nScaledXMargin * 2.0;
clientHeight -= nScaledYMargin * 2.0;
scaleXMultiplier = clientWidth / (double)CDG_CANVAS_WIDTH;
scaleYMultiplier = clientHeight / (double)CDG_CANVAS_HEIGHT;
pRect->left = (int)(pRect->left * scaleXMultiplier);
pRect->right = (int)(pRect->right * scaleXMultiplier);
pRect->top = (int)(pRect->top * scaleYMultiplier);
pRect->bottom = (int)(pRect->bottom * scaleYMultiplier);
::OffsetRect(pRect, nScaledXMargin, nScaledYMargin);
::InflateRect(&clientRect, -nScaledXMargin, -nScaledYMargin);
if (g_bDrawOutline || g_nSmoothingPasses) {
int nScaling = 1 << (g_nSmoothingPasses + (g_bDrawOutline ? 1 : 0));
::InflateRect(pRect, (int)(nScaling * scaleXMultiplier), (int)(nScaling * scaleYMultiplier));
}
::IntersectRect(pRect, pRect, &clientRect);
}
/// <summary>
/// The logo position changes when the window resizes.
/// </summary>
void UpdateLogoPosition() {
::SetWindowPos(g_hForegroundWindow, g_hLogoWindow, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
static RECT foregroundClientRect,foregroundWindowRect;
::GetClientRect(g_hForegroundWindow, &foregroundClientRect);
::GetWindowRect(g_hForegroundWindow, &foregroundWindowRect);
POINT topLeft = { 0,0 };
::ClientToScreen(g_hForegroundWindow, &topLeft);
int clientRectWidth = foregroundClientRect.right - foregroundClientRect.left;
int clientRectHeight = foregroundClientRect.bottom - foregroundClientRect.top;
POINT logoSourcePoint = { 0,0 };
RECT clientScreenRect = { topLeft.x,topLeft.y,topLeft.x + clientRectWidth,topLeft.y + clientRectHeight };
int halfRemainderX = (g_logoSize.cx - clientRectWidth) >> 1;
if (clientRectWidth < g_logoSize.cx)
logoSourcePoint.x = halfRemainderX;
else
topLeft.x -= halfRemainderX;
int halfRemainderY = (g_logoSize.cy - clientRectHeight) >> 1;
if (clientRectHeight < g_logoSize.cy)
logoSourcePoint.y = halfRemainderY;
else
topLeft.y -= halfRemainderY;
RECT logoRect = { topLeft.x,topLeft.y,topLeft.x + g_logoSize.cx,topLeft.y + g_logoSize.cy };
::IntersectRect(&logoRect, &logoRect, &clientScreenRect);
SIZE logoSize = { logoRect.right-logoRect.left,logoRect.bottom- logoRect.top};
g_logoWindowOffset = { foregroundWindowRect.left - topLeft.x,foregroundWindowRect.top - topLeft.y };
::UpdateLayeredWindow(g_hLogoWindow, g_hScreenDC, &topLeft, &logoSize, g_hLogoDC, &logoSourcePoint, 0, &g_blendFn, ULW_ALPHA);
}
void ShowWindows(bool bSongPlaying, bool updateLogo = false) {
// If a song is playing, then show all windows EXCEPT the logo window.
// If a song is NOT playing, then:
// If there is a logo, show all windows.
// If there is NOT a logo, hide all windows.
DWORD exStyle = ::GetWindowLong(g_hForegroundWindow, GWL_EXSTYLE);
if (bSongPlaying) {
// The foreground window should now be the app window. You can't change this while it's visible,
// so temporarily make it invisible.
::ShowWindow(g_hForegroundWindow, SW_HIDE);
exStyle |= WS_EX_APPWINDOW;
exStyle &= ~WS_EX_NOACTIVATE;
::SetWindowLong(g_hForegroundWindow, GWL_EXSTYLE, exStyle);
// Now show the windows we want.
if (g_bUseLayeredWindows) {
::ShowWindow(g_hLogoWindow, SW_HIDE);
::ShowWindow(g_hBackgroundWindow, SW_SHOW);
}
::ShowWindow(g_hForegroundWindow, SW_SHOW);
}
else {
if (g_pLogoImage) {
// If the IS a logo, then we want the logo to be the "app window", so
// that's what appears in the alt-tab menu, etc. For that to happen,
// we have to remove that flag from the foreground window.
::ShowWindow(g_hForegroundWindow, SW_HIDE);
exStyle &= ~WS_EX_APPWINDOW;
exStyle |= WS_EX_NOACTIVATE;
::SetWindowLong(g_hForegroundWindow, GWL_EXSTYLE, exStyle);
if (g_bUseLayeredWindows)
::ShowWindow(g_hBackgroundWindow, SW_SHOW);
::ShowWindow(g_hForegroundWindow, SW_SHOW);
if (g_bUseLayeredWindows)
::ShowWindow(g_hLogoWindow, SW_SHOW);
}
else {
if (g_bUseLayeredWindows)
::ShowWindow(g_hLogoWindow, SW_HIDE);
::ShowWindow(g_hForegroundWindow, SW_HIDE);
if (g_bUseLayeredWindows)
::ShowWindow(g_hBackgroundWindow, SW_HIDE);
}
}
if(updateLogo && g_bUseLayeredWindows)
::UpdateLogoPosition();
}
bool IsFullScreen() {
return g_lastSize.right - g_lastSize.left > 0;
}
void SetFullScreen(bool fullscreen)
{
bool currentlyFullScreen = IsFullScreen();
if (currentlyFullScreen != fullscreen) {
if (fullscreen)
{
::GetWindowRect(g_hForegroundWindow, &g_lastSize);
// Remove frame from window
DWORD currentStyle = ::GetWindowLong(g_hForegroundWindow, GWL_STYLE);
::SetWindowLong(g_hForegroundWindow, GWL_STYLE, currentStyle & ~WS_THICKFRAME);
// Figure out what screen we're on, and what size it is.
MONITORINFO monitorInfo;
monitorInfo.cbSize = sizeof(monitorInfo);
::GetMonitorInfo(MonitorFromWindow(g_hForegroundWindow, MONITOR_DEFAULTTONEAREST), &monitorInfo);
RECT window_rect(monitorInfo.rcMonitor);
::SetWindowPos(g_hForegroundWindow, NULL, window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
else
{
// Reset original window style and size.
DWORD currentStyle = ::GetWindowLong(g_hForegroundWindow, GWL_STYLE);
::SetWindowLong(g_hForegroundWindow, GWL_STYLE, currentStyle | WS_THICKFRAME);
RECT new_rect(g_lastSize);
::SetWindowPos(g_hForegroundWindow, NULL, new_rect.left, new_rect.top, new_rect.right - new_rect.left, new_rect.bottom - new_rect.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
g_lastSize = { 0,0,0,0 };
}
}
}
LRESULT CALLBACK ForegroundWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static PAINTSTRUCT ps;
switch (uMsg)
{
// Used for "grab testing".
case WM_NCHITTEST: {
LRESULT hit = DefWindowProc(hwnd, uMsg, wParam, lParam);
if (hit == HTCLIENT)
hit = HTCAPTION;
return hit;
}
// You can use the escape key to exit fullscreen mode.
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) {
SetMenuItemCheckmark(MENUITEM_FULLSCREEN_ID, false);
SetFullScreen(false);
}
break;
case WM_SHOWWINDOW:
if (!wParam)
break;
// If a window changes moves, activates, deactivates, or changes z-order, the others have to follow suit!
case WM_WINDOWPOSCHANGED:
if (g_bUseLayeredWindows) {
::SetWindowPos(g_hBackgroundWindow, hwnd, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
::SetWindowPos(g_hLogoWindow, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
::SetWindowPos(hwnd, g_hLogoWindow, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
UpdateLogoPosition();
}
break;
// Right-mouse button click ... show the menu!
case WM_NCRBUTTONUP: {
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
ShowMenu(xPos, yPos);
break;
}
// If a window moves, the others have to move too!
case WM_MOVE: {
if (g_bUseLayeredWindows) {
int x = (int)(short)LOWORD(lParam);
int y = (int)(short)HIWORD(lParam);
::SetWindowPos(g_hBackgroundWindow, hwnd, x, y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
UpdateLogoPosition();
}
break;
}
// If a window changes size, the others have to change too!
case WM_SIZE: {
if (g_bUseLayeredWindows) {
int w = (int)(short)LOWORD(lParam);
int h = (int)(short)HIWORD(lParam);
::SetWindowPos(g_hBackgroundWindow, hwnd, 0, 0, w, h, SWP_NOMOVE | SWP_NOACTIVATE);
}
if (g_hForegroundBackBufferDC) {
ResizeForegroundBackBufferBitmap();
PaintForegroundBackBuffer();
}
if (g_bUseLayeredWindows)
UpdateLogoPosition();
break;
}
// Do not allow the user to manually close this window. It will close when Winamp closes,
// or when the song ends.
case WM_CLOSE:
return 1;
// Window needs painted.
case WM_PAINT: {
if (g_bDoubleBuffered)
::SwapBuffers(g_hForegroundWindowDC);
::WaitForSingleObject(g_hPaintMutex, INFINITE);
::BeginPaint(g_hForegroundWindow, &ps);
// Paint from our foreground back-buffer.
PaintForeground(&ps.rcPaint);
::EndPaint(g_hForegroundWindow, &ps);
LRESULT result = DefWindowProc(hwnd, uMsg, wParam, lParam);
::ReleaseMutex(g_hPaintMutex);
return result;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK BackgroundWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
DrawBackground();
break;
case WM_SHOWWINDOW:
if (!wParam)
break;
case WM_WINDOWPOSCHANGED:
::SetWindowPos(hwnd, g_hForegroundWindow, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK LogoWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_NCHITTEST: {
LRESULT hit = DefWindowProc(hwnd, uMsg, wParam, lParam);
if (hit == HTCLIENT)
hit = HTCAPTION;
return hit;
}
case WM_MOVE: {
int x = (int)(short)LOWORD(lParam);
int y = (int)(short)HIWORD(lParam);
int newForegroundWindowX = x + g_logoWindowOffset.cx;
int newForegroundWindowY = y + g_logoWindowOffset.cy;
::SetWindowPos(g_hForegroundWindow, g_hLogoWindow, newForegroundWindowX, newForegroundWindowY, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
break;
}
case WM_NCRBUTTONUP: {
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
ShowMenu(xPos, yPos);
break;
}
case WM_CLOSE:
return 1;
case WM_SHOWWINDOW:
if (!wParam)
break;
case WM_WINDOWPOSCHANGED:
::SetWindowPos(g_hForegroundWindow,hwnd, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
ATOM RegisterWindowClass(const WCHAR* pszClassName, WNDPROC wndProc) {
if (!g_hIcon)
g_hIcon = ::LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_APPICON));
if (g_hIcon) {
WNDCLASSEX wndClass;
::ZeroMemory(&wndClass, sizeof(WNDCLASSEX));
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_NOCLOSE | CS_PARENTDC | CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = (WNDPROC)wndProc;
wndClass.hInstance = g_hInstance;
wndClass.hIcon = wndClass.hIconSm = g_hIcon;
wndClass.lpszClassName = pszClassName;
return ::RegisterClassEx(&wndClass);
}
return 0;
}
ATOM RegisterForegroundWindowClass() {
return RegisterWindowClass(g_foregroundWindowClassName, (WNDPROC)ForegroundWindowProc);
}
ATOM RegisterBackgroundWindowClass() {
return RegisterWindowClass(g_backgroundWindowClassName, (WNDPROC)BackgroundWindowProc);
}
ATOM RegisterLogoWindowClass() {
return RegisterWindowClass(g_logoWindowClassName, (WNDPROC)LogoWindowProc);
}
bool CreateCDGWindow(HWND* phWnd, HDC* phDC, const WCHAR* pszClassName, DWORD styles, DWORD additionalExStyles=0) {
int width = CDG_CANVAS_WIDTH + (g_nMargin << 1);
int height = CDG_CANVAS_HEIGHT + (g_nMargin << 1);
*phWnd = CreateWindowEx(
(g_bUseLayeredWindows ? WS_EX_LAYERED : 0 ) | additionalExStyles,
pszClassName,
g_pszWindowCaption,
styles,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
// We don't want this window to minimize/restore along with WinAmp, so we don't
// tell Windows that this is a child of WinAmp.
NULL,//g_hWinampWindow,
NULL,
g_hInstance,
NULL);
if (*phWnd) {
*phDC = ::GetDC(*phWnd);
if (*phDC) {
::SetWindowLong(*phWnd, GWL_STYLE, styles);
::SetWindowPos(*phWnd, NULL, 50, 50, width, height, SWP_NOZORDER | SWP_FRAMECHANGED);
RECT windowRect, clientRect;
::GetWindowRect(*phWnd, &windowRect);
::GetClientRect(*phWnd, &clientRect);
int windowWidth = windowRect.right - windowRect.left;
int windowHeight = windowRect.bottom - windowRect.top;
int clientWidth = clientRect.right - clientRect.left;
int clientHeight = clientRect.bottom - clientRect.top;
::SetWindowPos(*phWnd, NULL, 50, 50, width + (windowWidth - clientWidth), height + (windowHeight - clientHeight), SWP_NOZORDER);
return true;
}
}
return false;
}
bool CreateForegroundWindow() {
return CreateCDGWindow(&g_hForegroundWindow, &g_hForegroundWindowDC, g_foregroundWindowClassName, WS_THICKFRAME, WS_EX_NOACTIVATE);
}
bool CreateBackgroundWindow() {
return CreateCDGWindow(&g_hBackgroundWindow, &g_hBackgroundWindowDC, g_backgroundWindowClassName, 0, WS_EX_NOACTIVATE);
}
bool CreateLogoWindow() {
return CreateCDGWindow(&g_hLogoWindow, &g_hLogoWindowDC, g_logoWindowClassName, 0, WS_EX_APPWINDOW);
}
void UnregisterWindowClasses() {
::UnregisterClass(g_foregroundWindowClassName, g_hInstance);
if (g_bUseLayeredWindows) {
::UnregisterClass(g_backgroundWindowClassName, g_hInstance);
::UnregisterClass(g_logoWindowClassName, g_hInstance);
}
if (g_hIcon)
::DeleteObject(g_hIcon);
}
bool CreateWindows() {
if ((!g_bUseLayeredWindows || RegisterBackgroundWindowClass()) &&
RegisterForegroundWindowClass() &&
(!g_bUseLayeredWindows || RegisterLogoWindowClass()) &&
(!g_bUseLayeredWindows || CreateLogoWindow()) &&
(!g_bUseLayeredWindows || CreateBackgroundWindow()) &&
CreateForegroundWindow()) {
::SetStretchBltMode(g_hForegroundWindowDC, COLORONCOLOR);
if (g_bUseLayeredWindows) {
::SetLayeredWindowAttributes(g_hForegroundWindow, DEFAULT_TRANSPARENT_COLORREF, 255, LWA_COLORKEY);
::SetLayeredWindowAttributes(g_hBackgroundWindow, 0, g_nBackgroundOpacity, LWA_ALPHA);
}
return true;
}
return false;
}
void CloseWindow(HWND hWnd, HDC hDC) {
if (hDC)
::ReleaseDC(hWnd, hDC);
if (hWnd) {
::CloseWindow(hWnd);
::DestroyWindow(hWnd);
}
}
void DestroyWindows() {
CloseWindow(g_hForegroundWindow, g_hForegroundWindowDC);
if (g_bUseLayeredWindows) {
CloseWindow(g_hBackgroundWindow, g_hBackgroundWindowDC);
CloseWindow(g_hLogoWindow, g_hLogoWindowDC);
}
UnregisterWindowClasses();
}