-
Notifications
You must be signed in to change notification settings - Fork 29
/
SplashScreen.cpp
122 lines (94 loc) · 3.36 KB
/
SplashScreen.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
#include "SplashScreen.hpp"
void Splash::InitializeSplash()
{
const int splashDisplayTime = 5000;
GdiplusStartupInput gdiplusStartupInput; //Initialize GDI+
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
HINSTANCE hInstance = GetModuleHandle(nullptr);
HWND splashWnd = CreateSplashWindow(hInstance);
ShowWindow(splashWnd, SW_SHOW);
UpdateWindow(splashWnd);
Sleep(splashDisplayTime); // Sleep for 5 seconds
// Hide and destroy splash window
DestroyWindow(splashWnd);
// Shutdown GDI+
GdiplusShutdown(gdiplusToken);
}
void Splash::PositionWindow(HWND hwnd)
{
RECT rc;
GetWindowRect(hwnd, &rc);
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
int windowWidth = rc.right - rc.left;
int windowHeight = rc.bottom - rc.top;
int x = screenWidth - windowWidth;
int y = screenHeight - windowHeight;
SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
HWND Splash::CreateSplashWindow(HINSTANCE hInstance)
{
const wchar_t CLASS_NAME[] = L"SplashWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = SplashWndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(0,CLASS_NAME,nullptr,WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 250, 250, nullptr,nullptr, hInstance, nullptr);
PositionWindow(hwnd);
return hwnd;
}
LRESULT CALLBACK Splash::SplashWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
Graphics graphics(hdc);
Image image(SplashImageName);
UINT imgWidth = image.GetWidth();
UINT imgHeight = image.GetHeight();
if (imgWidth == 0 || imgHeight == 0) //failed to fetch image, check path
{
Logger::logf("UltimateAnticheat.log", Warning, "Failed to load splash screen: please ensure splash.png is in the current folder or project root folder.");
return 0;
}
RECT rect;
GetClientRect(hWnd, &rect);
int winWidth = rect.right - rect.left;
int winHeight = rect.bottom - rect.top;
float imgAspect = (float)imgWidth / imgHeight; //calculate aspect ratios
float winAspect = (float)winWidth / winHeight;
int drawWidth, drawHeight; //fit the image in the window
int offsetX = 0, offsetY = 0;
if (winAspect > imgAspect)
{
drawHeight = winHeight;
drawWidth = (int)(drawHeight * imgAspect);
offsetX = (winWidth - drawWidth) / 2;
}
else
{
drawWidth = winWidth;
drawHeight = (int)(drawWidth / imgAspect);
offsetY = (winHeight - drawHeight) / 2;
}
graphics.DrawImage(&image, offsetX, offsetY, drawWidth, drawHeight);
EndPaint(hWnd, &ps);
} break;
case WM_CLOSE:
{
DestroyWindow(hWnd);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
} break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}