From: Patrick Schönberger Date: Mon, 10 Jan 2022 13:58:11 +0000 (+0100) Subject: Icons X-Git-Url: https://gitweb.ps.run/autorec/commitdiff_plain/f0e7eee62c3b7c71a7accfb7a2cb6e1ed1f566bc Icons --- diff --git a/res/msys2.ico b/res/msys2.ico deleted file mode 100644 index ee73a7d..0000000 Binary files a/res/msys2.ico and /dev/null differ diff --git a/res/res.rc b/res/res.rc index 9369509..f510d18 100644 --- a/res/res.rc +++ b/res/res.rc @@ -1,3 +1,5 @@ #include "resource.h" -IDI_MY_ICON ICON "msys2.ico" \ No newline at end of file +IDI_ICON_WHITE ICON "icon_white.ico" +IDI_ICON_GREEN ICON "icon_green.ico" +IDI_ICON_RED ICON "icon_red.ico" \ No newline at end of file diff --git a/res/res.res b/res/res.res index 77bdd8c..0891986 100644 Binary files a/res/res.res and b/res/res.res differ diff --git a/res/resource.h b/res/resource.h index dd1af7b..a046be6 100644 --- a/res/resource.h +++ b/res/resource.h @@ -1,4 +1,6 @@ -#define IDI_MY_ICON 101 +#define IDI_ICON_WHITE 101 +#define IDI_ICON_GREEN 102 +#define IDI_ICON_RED 103 diff --git a/src/main.cpp b/src/main.cpp index 7366104..d89c199 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,16 +15,52 @@ #include #include +bool recording = false; +HANDLE process = NULL; +HWND hwnd; +HINSTANCE hInstance; + + +NOTIFYICONDATAA niData = { 0 }; +const UINT ICON_MSG = WM_APP+1; +void +ShowNotificationIcon() +{ + Shell_NotifyIconA(NIM_ADD, &niData); + Shell_NotifyIconA(NIM_SETVERSION, &niData); +} + +void +HideNotificationIcon() +{ + Shell_NotifyIconA(NIM_DELETE, &niData); +} + +void changeIcon(HWND hwnd, HINSTANCE hInstance, WORD id) +{ + HICON icon = LoadIcon(hInstance, MAKEINTRESOURCE(id)); + HideNotificationIcon(); + niData.hIcon = icon; + if (! IsWindowVisible(hwnd)) + ShowNotificationIcon(); + SendMessage(hwnd, WM_SETICON, 0, (LPARAM)icon); + SendMessage(hwnd, WM_SETICON, 1, (LPARAM)icon); +} + + + void startRecording() { ws::sendRequest("StartRecord"); + changeIcon(hwnd, hInstance, IDI_ICON_GREEN); } void stopRecording() { ws::sendRequest("StopRecord"); + changeIcon(hwnd, hInstance, IDI_ICON_RED); } bool @@ -84,19 +120,49 @@ checkForegroundProcess(std::string exeName) return strcmp(filename, exeName.c_str()) == 0; } -bool recording = false; -HANDLE process = NULL; - -// int WINAPI -// WinMain(HINSTANCE hInstance, -// HINSTANCE hPrevInstance, -// LPSTR lpCmdLine, -// int nCmdShow) -int main(int argc, char **argv) +int WINAPI +WinMain(HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nCmdShow) +//int main(int argc, char **argv) { + hInstance = GetModuleHandle(0); + + win::Window window("Title", "MyWindowClass", hInstance); + hwnd = window.hwnd; + + niData.cbSize = sizeof(niData); + niData.uID = 12345; + niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; + niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_WHITE)); + niData.hWnd = window.hwnd; + niData.uCallbackMessage = ICON_MSG; + niData.uVersion = NOTIFYICON_VERSION_4; + + window.handlers[WM_SIZE].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { + if (wParam == SIZE_MINIMIZED) { + ShowNotificationIcon(); + ShowWindow(hwnd, false); + } + }); + window.handlers[WM_DESTROY].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { + HideNotificationIcon(); + }); + window.handlers[ICON_MSG].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { + if (LOWORD(lParam) == NIN_SELECT) { + HideNotificationIcon(); + ShowWindow(hwnd, true); + SetForegroundWindow(hwnd); + SetActiveWindow(hwnd); + } + }); - //win::Window window("Title", "MyWindowClass", hInstance); - win::Window window("Title", "MyWindowClass", GetModuleHandle(0)); + window.handlers[WM_GETMINMAXINFO].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { + MINMAXINFO *mmInfo = (MINMAXINFO*)lParam; + mmInfo->ptMinTrackSize.x = 400; + mmInfo->ptMinTrackSize.y = 200; + }); lay_context *ctx = &window.ctx; lay_id root = window.lId; @@ -113,14 +179,19 @@ int main(int argc, char **argv) btnConnect.onClick([&]() { ws::connect("ws://127.0.0.1:4444"); }); + win::Button btnTest(&window, "Test", row1, 100, 25, 0, 0); + btnTest.onClick([&]() { + changeIcon(window.hwnd, hInstance, IDI_ICON_GREEN); + }); win::ListBox lstActiveProcesses(&window, row2, 0, 0, 0, LAY_FILL); lay_id col1 = win::createLayId(&window.ctx, row2, 80, 0, LAY_COLUMN, LAY_VCENTER); + lstActiveProcesses.addStyle(WS_VSCROLL); + lay_set_margins_ltrb(ctx, col1, 5, 0, 5, 0); win::ListBox lstMonitoredProcesses(&window, row2, 0, 0, 0, LAY_FILL); - lstActiveProcesses.addStyle(WS_VSCROLL); lstMonitoredProcesses.addStyle(WS_VSCROLL); win::Button btnUpdateWindows(&window, "Update", col1, 85, 25, 0, 0); @@ -173,12 +244,16 @@ int main(int argc, char **argv) }); window.show(); + window.setDefaultFont(); + ws::onConnect = [&]() { + changeIcon(window.hwnd, hInstance, IDI_ICON_RED); + }; ws::init(); SetTimer(window.hwnd, 10123, 100, [](HWND, UINT, UINT_PTR, DWORD) { if (!recording) { - if (checkForegroundProcess("League of Legends.exe")) { + if (checkForegroundProcess("notepad.exe")) { recording = true; process = getHwndProcess(GetForegroundWindow()); startRecording(); diff --git a/src/win.h b/src/win.h index 1e761b6..b8763ea 100644 --- a/src/win.h +++ b/src/win.h @@ -46,7 +46,6 @@ namespace win struct Window : Hwnd { private: - NOTIFYICONDATAA niData = { 0 }; static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) @@ -62,17 +61,11 @@ namespace win DestroyWindow(hwnd); break; case WM_DESTROY: - Shell_NotifyIconA(NIM_DELETE, &window->niData); lay_destroy_context(&window->ctx); PostQuitMessage(0); break; case WM_SIZE: - if (wParam == SIZE_MINIMIZED) { - //TODO: auslagen - //ShowNotificationIcon(); - ShowWindow(hwnd, false); - } - else { + if (wParam != SIZE_MINIMIZED) { lay_set_size_xy(&window->ctx, window->lId, LOWORD(lParam), HIWORD(lParam)); lay_run_context(&window->ctx); @@ -81,23 +74,8 @@ namespace win break; case WM_NOTIFY: break; - case WM_APP + 1: - if (LOWORD(lParam) == NIN_SELECT) { - //TODO: auslagern - //HideNotificationIcon(); - ShowWindow(hwnd, true); - SetForegroundWindow(hwnd); - SetActiveWindow(hwnd); - } - break; case WM_CTLCOLORSTATIC: - return (LONG)GetStockObject(WHITE_BRUSH); - case WM_GETMINMAXINFO: { - MINMAXINFO *mmInfo = (MINMAXINFO*)lParam; - mmInfo->ptMinTrackSize.x = 400; - mmInfo->ptMinTrackSize.y = 200; - break; - } + return (LONG_PTR)GetStockObject(WHITE_BRUSH); default: defaultHandler = true; break; @@ -126,12 +104,12 @@ namespace win wc.cbClsExtra = 0; wc.cbWndExtra = sizeof(Window*); wc.hInstance = hInstance; - wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); - wc.hCursor = LoadCursor(nullptr, IDC_ARROW); + wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE)); + wc.hCursor = LoadCursor(hInstance, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = nullptr; wc.lpszClassName = className.c_str(); - wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION); + wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE)); RegisterClassExA(&wc); lay_init_context(&ctx); @@ -151,15 +129,6 @@ namespace win nullptr); SetWindowLongPtrA(hwnd, 0, (LONG_PTR)this); - - - niData.cbSize = sizeof(niData); - niData.uID = 12345; - niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_MY_ICON)); - niData.hWnd = hwnd; - niData.uCallbackMessage = WM_APP+1; - niData.uVersion = NOTIFYICON_VERSION_4; } bool update() { @@ -174,7 +143,9 @@ namespace win void show() { ShowWindow(hwnd, true); - + } + void setDefaultFont() + { EnumChildWindows( hwnd, [](HWND hwnd, LPARAM lParam) -> BOOL { diff --git a/src/ws.h b/src/ws.h index ecc88b3..058f69b 100644 --- a/src/ws.h +++ b/src/ws.h @@ -4,6 +4,7 @@ using json = nlohmann::json; #include +#include namespace ws { @@ -12,10 +13,14 @@ namespace ws bool done = false; + std::function onConnect; + static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_WS_OPEN) { + puts("Open"); } else if (ev == MG_EV_WS_MSG) { struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; + printf("Msg: %.*s\n", (int)wm->data.len, wm->data.ptr); std::string jsonStr(wm->data.ptr, wm->data.len); auto msg = json::parse(jsonStr); int op = msg["op"].get(); @@ -32,16 +37,18 @@ namespace ws } else if (op == 2) { - MessageBoxA(NULL, "hura", "connected", MB_OK); + puts("Connected"); + if (onConnect) + onConnect(); } } if (ev == MG_EV_ERROR) { - MessageBoxA(NULL, "", "Error", MB_OK); + puts("Error"); done = true; } if (ev == MG_EV_CLOSE) { - MessageBoxA(NULL, "", "Close", MB_OK); + puts("Close"); done = true; } }