-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiddenWindow.cpp
84 lines (70 loc) · 1.92 KB
/
HiddenWindow.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
#include "HiddenWindow.h"
#include <Windowsx.h>
#include <memory>
#include <atomic>
#include <sstream>
#include <stdexcept>
#include "Util.h"
#ifndef HIDDEN_WINDOW_WINDOW_CLASS_PREFIX
#define HIDDEN_WINDOW_WINDOW_CLASS_PREFIX "HiddenWindow"
#endif
std::string GenerateWindowClassName()
{
static std::atomic_int id = 1;
std::stringstream stringStream;
stringStream << HIDDEN_WINDOW_WINDOW_CLASS_PREFIX << "_HiddenWindow_" << id.load();
id++;
return stringStream.str();
}
WNDCLASSEX CreateWindowClass(std::string& className)
{
WNDCLASSEX windowClass;
ClearStructure(windowClass);
windowClass.lpszClassName = className.c_str();
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = ::DefWindowProc;
windowClass.hInstance = GetModuleHandle(NULL);
windowClass.hCursor = ::LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = GetStockBrush(BLACK_BRUSH);
return windowClass;
}
HiddenWindow::HiddenWindow()
{
this->className = GenerateWindowClassName();
this->windowClass = CreateWindowClass(this->className);
if (!::RegisterClassEx(&this->windowClass))
{
ClearStructure(windowClass);
throw std::runtime_error("Cannot register window class");
}
this->hWnd = ::CreateWindowEx(
WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
this->windowClass.lpszClassName,
"HiddenWindow",
0,
0,
0,
8,
8,
NULL,
NULL,
NULL,
NULL
);
if (!this->hWnd)
{
throw std::runtime_error("Cannot create window");
}
}
HiddenWindow::~HiddenWindow()
{
if (this->hWnd)
{
DestroyWindow(this->hWnd);
}
if (this->windowClass.lpszClassName)
{
::UnregisterClass(this->windowClass.lpszClassName, NULL);
}
}