-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSSplashScreen.cpp
164 lines (126 loc) · 4.2 KB
/
PSSplashScreen.cpp
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
#include "PrecompiledHeader.h"
#include "PSSplashScreen.h"
PSSplashScreen* PSSplashScreen::SplashWnd = _In_opt_ NULL;
ATOM PSSplashScreen::szWindowClass = 0;
int PSSplashScreen::m_millisecondsToDisplay = 0;
PSSplashScreen::PSSplashScreen() {}
void PSSplashScreen::ShowSplashScreen(int millisecondsToDisplay /*= 0*/) {
m_millisecondsToDisplay = millisecondsToDisplay;
if (SplashWnd == NULL) {
SplashWnd = new PSSplashScreen();
if (!SplashWnd->Create()) {
delete SplashWnd;
SplashWnd = NULL;
}
}
if (m_millisecondsToDisplay) {
SetTimer(SplashWnd->m_hwnd, 1, m_millisecondsToDisplay, NULL);
}
ShowWindow(SplashWnd->m_hwnd, SW_SHOW);
UpdateWindow(SplashWnd->m_hwnd);
InvalidateRect(SplashWnd->m_hwnd, NULL, FALSE);
SplashWnd->ClearMessageQueue();
}
BOOL PSSplashScreen::RegisterClass(LPCTSTR WindowClassName) {
m_Instance = GetModuleHandle(NULL);
WNDCLASSEX wcex{ };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = m_Instance;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = WindowClassName;
wcex.hCursor = LoadCursor(nullptr, IDC_APPSTARTING);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APP_ICON));
wcex.hIconSm = NULL;
szWindowClass = RegisterClassEx(&wcex);
if (szWindowClass == 0)
return FALSE;
return TRUE;
}
BOOL PSSplashScreen::Create() {
m_Instance = GetModuleHandle(NULL);
m_bitmap = LoadBitmap(m_Instance, MAKEINTRESOURCE(IDB_BITMAP1));
HWND hwndDesktop = GetDesktopWindow();
BITMAPINFO bitmapInfo;
memset(&bitmapInfo, 0, sizeof(BITMAPINFOHEADER));
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(GetDC(hwndDesktop), // handle to DC
m_bitmap, // handle to bitmap
0, // first scan line to set
0, // number of scan lines to copy
NULL, // array for bitmap bits
&bitmapInfo, // bitmap data buffer
DIB_RGB_COLORS); // RGB or palette index
LPCTSTR szTitle = L"Loading";
LPCTSTR szWindowClassName = L"SplashScreen";
if (szWindowClass == 0) {
BOOL result = RegisterClass(szWindowClassName);
if (!result)
return FALSE;
}
int xPos = 0;
int yPos = 0;
int width = bitmapInfo.bmiHeader.biWidth;
int height = bitmapInfo.bmiHeader.biHeight;
RECT parentRect;
::GetWindowRect(GetDesktopWindow(), &parentRect);
xPos = parentRect.left + (parentRect.right - parentRect.left) / 2 - (width / 2);
yPos = parentRect.top + (parentRect.bottom - parentRect.top) / 2 - (height / 2);
m_hwnd = CreateWindowEx(0, szWindowClassName, szTitle, WS_POPUP,
xPos, yPos, width, height, NULL, NULL, m_Instance, this);
if (m_hwnd == NULL) {
return FALSE;
}
::SetWindowPos(m_hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
return TRUE;
}
void PSSplashScreen::ClearMessageQueue() {
MSG msg;
while (PeekMessage(&msg, m_hwnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK PSSplashScreen::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message){
case WM_PAINT:
SplashWnd->OnPaint(hwnd);
break;
case WM_NCDESTROY:
delete SplashWnd;
SplashWnd = NULL;
PostQuitMessage(0);
break;
case WM_TIMER:
DestroyWindow(hwnd);
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
void PSSplashScreen::OnPaint(HWND hwnd) {
PAINTSTRUCT ps;
HDC paintDC = BeginPaint(hwnd, &ps);
HDC imageDC = ::CreateCompatibleDC(paintDC);
BITMAPINFO bitmapInfo;
memset(&bitmapInfo, 0, sizeof(BITMAPINFOHEADER));
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(imageDC, // handle to DC
m_bitmap, // handle to bitmap
0, // first scan line to set
0, // number of scan lines to copy
NULL, // array for bitmap bits
&bitmapInfo, // bitmap data buffer
DIB_RGB_COLORS); // RGB or palette index
// Paint the bitmap image.
HBITMAP pOldBitmap = (HBITMAP)SelectObject(imageDC, m_bitmap);
int width = bitmapInfo.bmiHeader.biWidth;
int height = bitmapInfo.bmiHeader.biHeight;
BitBlt(paintDC, 0, 0, width, height, imageDC, 0, 0, SRCCOPY);
SelectObject(imageDC, pOldBitmap);
EndPaint(hwnd, &ps);
}