-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_example.cpp
More file actions
111 lines (93 loc) · 2.85 KB
/
Copy pathbasic_example.cpp
File metadata and controls
111 lines (93 loc) · 2.85 KB
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
#include <windows.h>
#include <commctrl.h>
#include <string>
#include <wndkit/dispatcher.hpp>
#include <wndkit/message_handler.hpp>
#include <wndkit/widgets/main_window.hpp>
#define IDC_START_BUTTON 1001
#define IDC_STOP_BUTTON 1002
class ticker {
public:
HWND create(HWND parent, int x, int y, int width, int height, HINSTANCE instance) {
hwnd_ = wndkit::dispatcher::create_subclass_window(&message_handler_,
{}, WC_STATICW, {},
WS_CHILD | WS_VISIBLE,
x, y, width, height,
parent, {}, instance, {});
message_handler_
.on_message<WM_TIMER>([this](HWND, const auto&) {
tick();
})
;
return hwnd_;
}
void start() {
SetTimer(hwnd_, 1, 1000, nullptr);
}
void stop() {
KillTimer(hwnd_, 1);
}
private:
void tick() {
seconds_++;
SetWindowTextW(hwnd_, std::to_wstring(seconds_).c_str());
}
HWND hwnd_{};
wndkit::message_handler message_handler_;
int seconds_{};
};
class example_window : public wndkit::widgets::main_window {
public:
example_window() {
message_handler_
.on_message<WM_CREATE>([this](HWND hwnd, const wndkit::create_params& params) {
on_create(hwnd, params);
})
.on_command_invoke(IDC_START_BUTTON, [this]() {
ticker_.start();
})
.on_command_invoke(IDC_STOP_BUTTON, [this]() {
ticker_.stop();
})
;
}
private:
void on_create(HWND hwnd, const wndkit::create_params& params) {
CreateWindowW(
WC_BUTTONW, L"Start",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, 10, 50, 20,
hwnd, (HMENU)IDC_START_BUTTON, params.createstruct()->hInstance, {});
CreateWindowW(
WC_BUTTONW, L"Stop",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, 30, 50, 20,
hwnd, (HMENU)IDC_STOP_BUTTON, params.createstruct()->hInstance, {});
ticker_.create(hwnd, 10, 50, 50, 50, params.createstruct()->hInstance);
;
}
ticker ticker_;
};
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int) {
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
INITCOMMONCONTROLSEX init_cc{
.dwSize = sizeof(init_cc),
.dwICC = ICC_WIN95_CLASSES | ICC_STANDARD_CLASSES,
};
InitCommonControlsEx(&init_cc);
WNDCLASSW wc{
.lpfnWndProc = wndkit::dispatcher::window_proc,
.hInstance = hInstance,
.hCursor = LoadCursor(nullptr, IDC_ARROW),
.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1),
.lpszClassName = L"wndkit_example",
};
RegisterClassW(&wc);
example_window window;
window.create(
0, L"wndkit_example", L"Example",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
nullptr, nullptr, hInstance, nullptr);
return wndkit::dispatcher::run();
}