public void setWindowsHook() { void WinEventHookAll(User32.HWINEVENTHOOK hWinEventHook, uint winEvent, HWND hwnd, int idObject, int idChild, uint idEventThread, uint dwmsEventTime) { DesktopWindow desktopWindow = new DesktopWindow(hwnd); if (hwnd != HWND.NULL && idObject == User32.ObjectIdentifiers.OBJID_WINDOW && idChild == 0 && desktopWindow.IsRuntimeValuable()) { switch (winEvent) { case User32.EventConstants.EVENT_OBJECT_SHOW: case User32.EventConstants.EVENT_OBJECT_UNCLOAKED: case User32.EventConstants.EVENT_OBJECT_IME_SHOW: case User32.EventConstants.EVENT_SYSTEM_FOREGROUND: ManageShown(hwnd); break; case User32.EventConstants.EVENT_SYSTEM_MINIMIZEEND: SystrayContext.Logger.Information($"window maximized"); desktopWindow.GetInfo(); DesktopWindowsManager.AddWindow(desktopWindow); break; case User32.EventConstants.EVENT_SYSTEM_MINIMIZESTART: case User32.EventConstants.EVENT_OBJECT_HIDE: case User32.EventConstants.EVENT_OBJECT_IME_HIDE: SystrayContext.Logger.Information($"window minimized/hide"); DesktopWindow removed = DesktopWindowsManager.FindWindow(hwnd); if (removed != null) { DesktopWindowsManager.RemoveWindow(removed); } break; case User32.EventConstants.EVENT_SYSTEM_MOVESIZEEND: SystrayContext.Logger.Information($"window move/size"); DesktopWindow moved = DesktopWindowsManager.FindWindow(hwnd); if (moved != null) { DesktopWindow newMoved = new DesktopWindow(hwnd); newMoved.GetInfo(); if (!moved.Equals(newMoved)) { DesktopWindowsManager.RepositionWindow(moved, newMoved); } } break; case User32.EventConstants.EVENT_OBJECT_DRAGCOMPLETE: SystrayContext.Logger.Information($"window dragged"); break; default: break; } } } User32.WinEventProc winEventHookAll = new User32.WinEventProc(WinEventHookAll); GCHandle gchCreate = GCHandle.Alloc(winEventHookAll); User32.HWINEVENTHOOK hookAll = User32.SetWinEventHook(User32.EventConstants.EVENT_MIN, User32.EventConstants.EVENT_MAX, HINSTANCE.NULL, winEventHookAll, 0, 0, User32.WINEVENT.WINEVENT_OUTOFCONTEXT | User32.WINEVENT.WINEVENT_SKIPOWNPROCESS); }