1 #pragma comment(linker, "\"/manifestdependency:type='win32' \
\r
2 name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
\r
3 processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
\r
6 #include <CommCtrl.h>
\r
10 #include <functional>
\r
28 GetClientRect(hwnd, &rect);
\r
29 return x.percent * (rect.right - rect.left) + x.pixel;
\r
34 GetClientRect(hwnd, &rect);
\r
35 return y.percent * (rect.bottom - rect.top) + y.pixel;
\r
40 GetClientRect(hwnd, &rect);
\r
41 return w.percent * (rect.right - rect.left) + w.pixel;
\r
43 int getH(HWND hwnd, int _w, int _h)
\r
46 GetWindowRect(hwnd, &rect);
\r
47 return h.percent * (rect.bottom - rect.top) + h.pixel;
\r
52 using CallbackFn = std::function<void()>;
\r
53 std::map<HWND, std::map<WORD, CallbackFn>> handlers;
\r
54 std::map<HWND, lay_id> lIds;
\r
56 NOTIFYICONDATA niData = { 0 };
\r
62 WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
\r
66 DestroyWindow(hwnd);
\r
69 Shell_NotifyIconA(NIM_DELETE, &niData);
\r
70 lay_destroy_context(&ctx);
\r
74 if (wParam == SIZE_MINIMIZED) {
\r
75 ShowWindow(hwnd, false);
\r
76 SetForegroundWindow(hwnd);
\r
77 SetActiveWindow(hwnd);
\r
80 lay_set_size_xy(&_::ctx, _::root, LOWORD(lParam), HIWORD(lParam));
\r
81 lay_run_context(&_::ctx);
\r
83 for (auto &lId : lIds) {
\r
84 lay_vec4 rect = lay_get_rect(&_::ctx, lId.second);
\r
85 SetWindowPos(lId.first, HWND_TOP,
\r
93 RedrawWindow(hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN);
\r
97 if (handlers.find((HWND)lParam) != handlers.end()) {
\r
98 auto handler = handlers[(HWND)lParam];
\r
99 if (handler.find(HIWORD(wParam)) != handler.end()) {
\r
100 auto cb = handler[HIWORD(wParam)];
\r
108 if (LOWORD(lParam) == NIN_SELECT)
\r
109 ShowWindow(hwnd, true);
\r
111 case WM_CTLCOLORSTATIC:
\r
112 return (LONG)GetStockObject(WHITE_BRUSH);
\r
113 case WM_GETMINMAXINFO: {
\r
114 MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;
\r
115 mmInfo->ptMinTrackSize.x = 400;
\r
116 mmInfo->ptMinTrackSize.y = 200;
\r
120 return DefWindowProc(hwnd, msg, wParam, lParam);
\r
127 ShowNotificationIcon(HINSTANCE hInstance, HWND hwnd)
\r
129 _::niData.cbSize = sizeof(_::niData);
\r
130 _::niData.uID = 12345;
\r
131 _::niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
\r
132 _::niData.hIcon = LoadIconA(nullptr, IDI_WINLOGO);
\r
133 _::niData.hWnd = hwnd;
\r
134 _::niData.uCallbackMessage = WM_APP+1;
\r
135 _::niData.uVersion = NOTIFYICON_VERSION_4;
\r
137 Shell_NotifyIconA(NIM_ADD, &_::niData);
\r
138 Shell_NotifyIconA(NIM_SETVERSION, &_::niData);
\r
142 Callback(HWND hwnd, WORD ev, std::function<void()> cb)
\r
144 _::handlers[hwnd][ev] = cb;
\r
148 Window(string title, string className, HINSTANCE hInstance)
\r
151 wc.cbSize = sizeof(WNDCLASSEX);
\r
153 wc.lpfnWndProc = _::WndProc;
\r
156 wc.hInstance = hInstance;
\r
157 wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
\r
158 wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
\r
159 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
\r
160 wc.lpszMenuName = nullptr;
\r
161 wc.lpszClassName = className.c_str();
\r
162 wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
\r
163 RegisterClassEx(&wc);
\r
165 lay_init_context(&_::ctx);
\r
166 _::root = lay_item(&_::ctx);
\r
167 lay_set_contain(&_::ctx, _::root, LAY_COLUMN);
\r
168 lay_set_margins_ltrb(&_::ctx, _::root, 5, 5, 5, 5);
\r
170 return CreateWindowA(className.c_str(),
\r
172 WS_OVERLAPPEDWINDOW,
\r
184 UpdateWindow(HWND hwnd)
\r
187 if (GetMessage(&msg, nullptr, 0, 0) > 0) {
\r
188 TranslateMessage(&msg);
\r
189 DispatchMessage(&msg);
\r
196 ShowWindow(HWND hwnd)
\r
198 ShowWindow(hwnd, true);
\r
202 [](HWND hwnd, LPARAM lParam) -> BOOL {
\r
203 HFONT guiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
\r
204 SendMessage(hwnd, WM_SETFONT, (WPARAM)guiFont, MAKELPARAM(TRUE, 0));
\r
211 Button(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
213 lay_id lId = lay_item(&_::ctx);
\r
214 lay_insert(&_::ctx, parent, lId);
\r
215 lay_set_size_xy(&_::ctx, lId, w, h);
\r
216 lay_set_contain(&_::ctx, lId, contain);
\r
217 lay_set_behave(&_::ctx, lId, behave);
\r
219 HWND result = CreateWindowExA(0,
\r
222 WS_VISIBLE | WS_CHILD,
\r
228 _::lIds[result] = lId;
\r
233 ListBox(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
235 lay_id lId = lay_item(&_::ctx);
\r
236 lay_insert(&_::ctx, parent, lId);
\r
237 lay_set_size_xy(&_::ctx, lId, w, h);
\r
238 lay_set_contain(&_::ctx, lId, contain);
\r
239 lay_set_behave(&_::ctx, lId, behave);
\r
241 HWND result = CreateWindowExA(0,
\r
244 WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,
\r
250 _::lIds[result] = lId;
\r
255 ListAddString(HWND hwnd, string str)
\r
257 SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)str.c_str());
\r
261 ListGetSelectedIndex(HWND hwnd)
\r
263 int sel = SendMessage(hwnd, LB_GETCURSEL, 0, 0);
\r
267 int ListFindString(HWND hwnd, string str)
\r
269 return SendMessageA(hwnd, LB_FINDSTRINGEXACT, -1, (LPARAM)str.c_str());
\r
273 ListGetText(HWND hwnd, int index)
\r
276 SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)buffer);
\r
277 return string(buffer);
\r
281 ListClear(HWND hwnd)
\r
283 SendMessageA(hwnd, LB_RESETCONTENT, 0, 0);
\r
286 void ListRemove(HWND hwnd, int index)
\r
288 SendMessageA(hwnd, LB_DELETESTRING, index, 0);
\r
292 CheckBox(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
294 lay_id lId = lay_item(&_::ctx);
\r
295 lay_insert(&_::ctx, parent, lId);
\r
296 lay_set_size_xy(&_::ctx, lId, w, h);
\r
297 lay_set_contain(&_::ctx, lId, contain);
\r
298 lay_set_behave(&_::ctx, lId, behave);
\r
300 HWND result = CreateWindowExA(0,
\r
303 WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
\r
309 _::lIds[result] = lId;
\r
313 void SetStyle(HWND hwnd, DWORD style)
\r
315 SetWindowLongPtrA(hwnd, GWL_STYLE, style);
\r
317 DWORD GetStyle(HWND hwnd)
\r
319 return GetWindowLongPtrA(hwnd, GWL_STYLE);
\r
321 void AddStyle(HWND hwnd, DWORD style)
\r
323 SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) | style);
\r
325 void RemoveStyle(HWND hwnd, DWORD style)
\r
327 SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) & (~style));
\r