public static void HookEvents(IVBE vbe) { _vbe = vbe; if (_eventHandle == IntPtr.Zero) { _eventProc = VbeEventCallback; _threadId = User32.GetWindowThreadProcessId(new IntPtr(_vbe.MainWindow.HWnd), IntPtr.Zero); _eventHandle = User32.SetWinEventHook((uint)WinEvent.Min, (uint)WinEvent.Max, IntPtr.Zero, _eventProc, 0, _threadId, WinEventFlags.OutOfContext); } }
public WindowObserver(IntPtr windowHandle, Action <User32.WindowEventTypes> callback) { WindowObserver.windowHandle = windowHandle; WindowObserver.callback = callback; eventListener = new User32.WinEventProc(WindowEventCallback); int processId; int threadId = User32.GetWindowThreadProcessId(windowHandle, out processId); eventHook = User32.SetWinEventHook(1, 0x7fffffff, IntPtr.Zero, eventListener, processId, threadId, User32.SetWinEventHookFlags.WINEVENT_OUTOFCONTEXT); Debug.WriteLine("hooked to window: " + eventHook); }
public static void HookEvents(IVBE vbe) { _vbe = vbe; if (_eventHandle == IntPtr.Zero) { _eventProc = VbeEventCallback; IntPtr mainWindowHwnd; using (var mainWindow = _vbe.MainWindow) { mainWindowHwnd = new IntPtr(mainWindow.HWnd); } _threadId = User32.GetWindowThreadProcessId(mainWindowHwnd, IntPtr.Zero); _eventHandle = User32.SetWinEventHook((uint)WinEvent.Min, (uint)WinEvent.Max, IntPtr.Zero, _eventProc, 0, _threadId, WinEventFlags.OutOfContext); Subclasses.Subclass(mainWindowHwnd.ChildWindows() .Where(hwnd => SubclassManager.IsSubclassable(hwnd.ToWindowType()))); } }
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"); HMONITOR monitorHandle = User32.MonitorFromWindow(hwnd, User32.MonitorFlags.MONITOR_DEFAULTTONEAREST); DesktopWindow remove = DesktopWindowsManager.GetWindowByHandlers(hwnd, monitorHandle, VirtualDesktop.Current); if (remove != null) { DesktopWindowsManager.RemoveWindow(remove); } break; case User32.EventConstants.EVENT_SYSTEM_MOVESIZEEND: SystrayContext.Logger.Information($"window move/size"); DesktopWindow movedWindow1 = DesktopWindowsManager.FindWindow(hwnd); VirtualDesktop movedDesktop1 = VirtualDesktop.FromHwnd(hwnd); HMONITOR currentMonitor1 = User32.MonitorFromWindow(hwnd, User32.MonitorFlags.MONITOR_DEFAULTTONEAREST); Pair <VirtualDesktop, HMONITOR> movedPair1 = new Pair <VirtualDesktop, HMONITOR>(movedDesktop1, currentMonitor1); DesktopWindow resizedWindow = new DesktopWindow(hwnd); resizedWindow.GetInfo(); if (resizedWindow.Info.rcWindow.Size.Equals(movedWindow1.Info.rcWindow.Size)) { // Not resized. DesktopWindow movedWindow2 = DesktopWindowsManager.FindWindow(Control.MousePosition); VirtualDesktop movedDesktop2 = VirtualDesktop.FromHwnd(movedWindow2.Window); HMONITOR currentMonitor2 = User32.MonitorFromWindow(movedWindow2.Window, User32.MonitorFlags.MONITOR_DEFAULTTONEAREST); Pair <VirtualDesktop, HMONITOR> movedPair2 = new Pair <VirtualDesktop, HMONITOR>(movedDesktop2, currentMonitor2); DesktopWindowsManager.SwapWindows(movedPair1, movedWindow1, movedPair2, movedWindow2); DesktopWindowsManager.Draw(movedPair2); } else { // Resized. } DesktopWindowsManager.Draw(movedPair1); 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); }
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); }