-
Notifications
You must be signed in to change notification settings - Fork 35
/
window_main.h
97 lines (83 loc) · 3.2 KB
/
window_main.h
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
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include "internals/base_loop.h"
#include "internals/base_msg_pubm.h"
#include "internals/base_scroll.h"
#include "internals/base_text_pubm.h"
#include "internals/base_thread_pubm.h"
#include "internals/base_window.h"
#include "internals/run.h"
#include "internals/styler.h"
#include "wnd.h"
namespace wl {
namespace _wli { class dialog_modeless; } // friend forward declaration
// Inherit from this class to have an ordinary main window for your application.
class window_main :
public wnd,
public _wli::base_msg_pubm<LRESULT>,
public _wli::base_thread_pubm<LRESULT, 0>,
public _wli::base_text_pubm<window_main>
{
friend _wli::dialog_modeless; // needs to access _baseLoop
protected:
// Variables to be set by user, used only during window creation.
struct setup_vars final : public _wli::base_window::setup_vars {
HACCEL accelTable = nullptr;
};
private:
HWND _hWnd = nullptr;
_wli::base_msg<LRESULT> _baseMsg{_hWnd};
_wli::base_thread<LRESULT, 0> _baseThread{_baseMsg};
_wli::base_window _baseWindow{_hWnd, _baseMsg};
_wli::base_loop _baseLoop;
public:
// Defines window creation parameters.
setup_vars setup;
// Wraps window style changes done by Get/SetWindowLongPtr.
_wli::styler<window_main> style{this};
protected:
window_main() :
wnd(_hWnd), base_msg_pubm(_baseMsg), base_thread_pubm(_baseThread), base_text_pubm(_hWnd)
{
this->_init_setup_styles();
this->base_msg_pubm::on_message(WM_NCDESTROY, [](params) noexcept -> LRESULT {
PostQuitMessage(0);
return 0;
});
}
public:
window_main(window_main&&) = default;
window_main& operator=(window_main&&) = default; // movable only
// Runs the window as the main program window; intended to be called in WinMain.
int winmain_run(HINSTANCE hInst, int cmdShow) {
InitCommonControls();
this->_baseWindow.register_create(this->setup, nullptr, hInst);
ShowWindow(this->hwnd(), cmdShow);
if (!UpdateWindow(this->hwnd())) {
throw std::system_error(GetLastError(), std::system_category(),
"UpdateWindow failed");
}
_wli::base_scroll::apply_behavior(this->hwnd());
return this->_baseLoop.run_loop(this->hwnd(), this->setup.accelTable); // can be used as program return value
}
private:
void _init_setup_styles() noexcept {
this->setup.wndClassEx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
this->setup.wndClassEx.hCursor = LoadCursorW(nullptr, reinterpret_cast<LPCWSTR>(IDC_ARROW));
this->setup.wndClassEx.style = CS_DBLCLKS;
this->setup.position = {CW_USEDEFAULT, CW_USEDEFAULT};
this->setup.size = {CW_USEDEFAULT, CW_USEDEFAULT};
this->setup.style = WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN | WS_BORDER;
// Useful styles to be added by user:
// WS_SIZEBOX resizable window
// WS_MAXIMIZEBOX adds maximize button
// WS_MINIMIZEBOX adds minimize button
// WS_EX_ACCEPTFILES accepts dropped files (extended style, add on exStyle)
}
};
}//namespace wl