-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
58 lines (46 loc) · 1.14 KB
/
window.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
#include "window.h"
#include <future>
Window::Window(HINSTANCE hinst)
{
std::atomic_bool winCreated = { false };
std::packaged_task<void()> task([&]()
{
const auto h = Create(hinst);
hwnd.store(h, std::memory_order_relaxed);
winCreated.store(true, std::memory_order_release);
if (h)
MainLoop();
});
auto result = task.get_future();
wndThread = std::move(std::thread(std::move(task)));
while (!winCreated.load(std::memory_order_acquire))
std::this_thread::yield();
if (!hwnd.load(std::memory_order_relaxed))
{
wndThread.join();
throw std::runtime_error("can't create window");
}
}
Window::~Window()
{
const auto threadId = GetThreadId(wndThread.native_handle());
PostThreadMessageW(threadId, WM_QUIT, 0, 0);
wndThread.join();
}
HWND Window::Create(HINSTANCE hinst)
{
return CreateWindowExW(0, L"STATIC", L"C++ Header Protector Window", WS_CAPTION, 0, 0, 0, 0, HWND_MESSAGE, nullptr, hinst, nullptr);
}
void Window::MainLoop()
{
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HWND Window::GetHwnd() const
{
return hwnd.load(std::memory_order_relaxed);
}