4 #include <WinSock2.h>
\r
8 #define LAY_IMPLEMENTATION
\r
12 #include <stdbool.h>
\r
15 #include <windows.h>
\r
16 #include <shlwapi.h>
\r
18 bool recording = false;
\r
19 HANDLE process = NULL;
\r
21 HINSTANCE hInstance;
\r
24 NOTIFYICONDATAA niData = { 0 };
\r
25 const UINT ICON_MSG = WM_APP+1;
\r
27 ShowNotificationIcon()
\r
29 Shell_NotifyIconA(NIM_ADD, &niData);
\r
30 Shell_NotifyIconA(NIM_SETVERSION, &niData);
\r
34 HideNotificationIcon()
\r
36 Shell_NotifyIconA(NIM_DELETE, &niData);
\r
39 void changeIcon(HWND hwnd, HINSTANCE hInstance, WORD id)
\r
41 HICON icon = LoadIcon(hInstance, MAKEINTRESOURCE(id));
\r
42 HideNotificationIcon();
\r
43 niData.hIcon = icon;
\r
44 if (! IsWindowVisible(hwnd))
\r
45 ShowNotificationIcon();
\r
46 SendMessage(hwnd, WM_SETICON, 0, (LPARAM)icon);
\r
47 SendMessage(hwnd, WM_SETICON, 1, (LPARAM)icon);
\r
55 ws::sendRequest("StartRecord");
\r
56 changeIcon(hwnd, hInstance, IDI_ICON_GREEN);
\r
62 ws::sendRequest("StopRecord");
\r
63 changeIcon(hwnd, hInstance, IDI_ICON_RED);
\r
67 checkProcessRunning(HANDLE handle)
\r
71 if (handle != NULL && GetExitCodeProcess(handle, &exit_code)) {
\r
72 return exit_code == STILL_ACTIVE;
\r
79 getHwndProcess(HWND hwnd)
\r
81 DWORD processId, threadId = GetWindowThreadProcessId(hwnd, &processId);
\r
82 return OpenProcess(PROCESS_QUERY_INFORMATION, false, processId);
\r
85 // HWND getProcessHwnd(HANDLE handle) {}
\r
88 checkFullscreenWindow()
\r
90 HWND desktopHwnd = GetDesktopWindow();
\r
91 HWND fgHwnd = GetForegroundWindow();
\r
93 if (fgHwnd != desktopHwnd && fgHwnd != GetShellWindow()) {
\r
94 RECT windowRect, desktopRect;
\r
95 // Get Window and Desktop size
\r
96 GetWindowRect(fgHwnd, &windowRect);
\r
97 GetWindowRect(desktopHwnd, &desktopRect);
\r
99 bool fullscreen = windowRect.bottom == desktopRect.bottom &&
\r
100 windowRect.top == desktopRect.top &&
\r
101 windowRect.left == desktopRect.left &&
\r
102 windowRect.right == desktopRect.right;
\r
111 checkForegroundProcess(std::string exeName)
\r
113 HWND fgHwnd = GetForegroundWindow();
\r
114 HANDLE fgHandle = getHwndProcess(fgHwnd);
\r
116 char filename[1024];
\r
117 int len = GetModuleFileNameExA(fgHandle, NULL, filename, 1024);
\r
118 PathStripPathA(filename);
\r
120 return strcmp(filename, exeName.c_str()) == 0;
\r
124 WinMain(HINSTANCE hInstance,
\r
125 HINSTANCE hPrevInstance,
\r
128 //int main(int argc, char **argv)
\r
130 hInstance = GetModuleHandle(0);
\r
132 win::Window window("Title", "MyWindowClass", hInstance);
\r
133 hwnd = window.hwnd;
\r
135 niData.cbSize = sizeof(niData);
\r
136 niData.uID = 12345;
\r
137 niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
\r
138 niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_WHITE));
\r
139 niData.hWnd = window.hwnd;
\r
140 niData.uCallbackMessage = ICON_MSG;
\r
141 niData.uVersion = NOTIFYICON_VERSION_4;
\r
143 window.handlers[WM_SIZE].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
144 if (wParam == SIZE_MINIMIZED) {
\r
145 ShowNotificationIcon();
\r
146 ShowWindow(hwnd, false);
\r
149 window.handlers[WM_DESTROY].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
150 HideNotificationIcon();
\r
152 window.handlers[ICON_MSG].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
153 if (LOWORD(lParam) == NIN_SELECT) {
\r
154 HideNotificationIcon();
\r
155 ShowWindow(hwnd, true);
\r
156 SetForegroundWindow(hwnd);
\r
157 SetActiveWindow(hwnd);
\r
161 window.handlers[WM_GETMINMAXINFO].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
162 MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;
\r
163 mmInfo->ptMinTrackSize.x = 400;
\r
164 mmInfo->ptMinTrackSize.y = 200;
\r
167 lay_context *ctx = &window.ctx;
\r
168 lay_id root = window.lId;
\r
170 lay_id row1 = win::createLayId(&window.ctx, window.lId, 0, 25, LAY_ROW, LAY_LEFT);
\r
171 lay_set_margins_ltrb(ctx, row1, 5, 5, 5, 5);
\r
172 lay_id row2 = win::createLayId(&window.ctx, window.lId, 0, 0, LAY_ROW, LAY_FILL);
\r
173 lay_set_margins_ltrb(ctx, row2, 5, 5, 5, 5);
\r
175 win::CheckBox cbWindowTitle(&window, "Window Title", row1, 100, 25, 0, 0);
\r
176 win::CheckBox cbFullscreenWindow(&window, "Any Fullscreen Application", row1, 200, 25, 0, 0);
\r
178 win::Button btnConnect(&window, "Connect", row1, 100, 25, 0, 0);
\r
179 btnConnect.onClick([&]() {
\r
180 ws::connect("ws://127.0.0.1:4444");
\r
182 win::Button btnTest(&window, "Test", row1, 100, 25, 0, 0);
\r
183 btnTest.onClick([&]() {
\r
184 changeIcon(window.hwnd, hInstance, IDI_ICON_GREEN);
\r
187 win::ListBox lstActiveProcesses(&window, row2, 0, 0, 0, LAY_FILL);
\r
189 lay_id col1 = win::createLayId(&window.ctx, row2, 80, 0, LAY_COLUMN, LAY_VCENTER);
\r
190 lstActiveProcesses.addStyle(WS_VSCROLL);
\r
192 lay_set_margins_ltrb(ctx, col1, 5, 0, 5, 0);
\r
194 win::ListBox lstMonitoredProcesses(&window, row2, 0, 0, 0, LAY_FILL);
\r
195 lstMonitoredProcesses.addStyle(WS_VSCROLL);
\r
197 win::Button btnUpdateWindows(&window, "Update", col1, 85, 25, 0, 0);
\r
198 win::Button btnStartMonitoringName(&window, "Exe name >>", col1, 85, 25, 0, 0);
\r
199 win::Button btnStartMonitoringPath(&window, "Full path >>", col1, 85, 25, 0, 0);
\r
200 win::Button btnStopMonitoring(&window, "Remove", col1, 85, 25, 0, 0);
\r
201 btnUpdateWindows.onClick([&]() {
\r
202 lstActiveProcesses.clear();
\r
203 for (HWND hwnd = GetTopWindow(NULL); hwnd != nullptr;
\r
204 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)) {
\r
205 if (!IsWindowVisible(hwnd))
\r
209 GetWindowRect(hwnd, &rect);
\r
212 if (GetModuleFileNameExA(getHwndProcess(hwnd), 0, str, 1024) != 0 &&
\r
213 lstActiveProcesses.findString(str) == LB_ERR) {
\r
214 lstActiveProcesses.addString(str);
\r
218 btnStartMonitoringName.onClick([&]() {
\r
219 int sel = lstActiveProcesses.getSelectedIndex();
\r
220 if (sel < 0) return;
\r
222 std::string selStr = lstActiveProcesses.getText(sel);
\r
224 char *filename = new char[selStr.size()];
\r
225 std::memcpy(filename, selStr.c_str(), selStr.size());
\r
226 PathStripPathA(filename);
\r
228 if (lstMonitoredProcesses.findString(std::string(filename)) == LB_ERR)
\r
229 lstMonitoredProcesses.addString(std::string(filename));
\r
233 btnStartMonitoringPath.onClick([&]() {
\r
234 int sel = lstActiveProcesses.getSelectedIndex();
\r
235 if (sel < 0) return;
\r
236 std::string selStr = lstActiveProcesses.getText(sel);
\r
237 if (lstMonitoredProcesses.findString(selStr) == LB_ERR)
\r
238 lstMonitoredProcesses.addString(selStr);
\r
240 btnStopMonitoring.onClick([&]() {
\r
241 int sel = lstMonitoredProcesses.getSelectedIndex();
\r
242 if (sel < 0) return;
\r
243 lstMonitoredProcesses.remove(sel);
\r
247 window.setDefaultFont();
\r
249 ws::onConnect = [&]() {
\r
250 changeIcon(window.hwnd, hInstance, IDI_ICON_RED);
\r
254 SetTimer(window.hwnd, 10123, 100, [](HWND, UINT, UINT_PTR, DWORD) {
\r
256 if (checkForegroundProcess("notepad.exe")) {
\r
258 process = getHwndProcess(GetForegroundWindow());
\r
262 if (!checkProcessRunning(process)) {
\r
270 while (window.update()) {
\r