public void Install() { if (Installed) { return; } NativeMethods.WinEventProc callback = new NativeMethods.WinEventProc(EventProcessor); _keyHookDelegate = GCHandle.Alloc(callback); _keyHook = NativeMethods.SetWinEventHook(3, 32780, IntPtr.Zero, callback, 0, 0, 0); if (_keyHook == IntPtr.Zero) { throw new Win32Exception("H"); } IntPtr currentForegroundWindow = NativeMethods.GetForegroundWindow(); new Thread(new ThreadStart(() => { OnWindowChangeEvent?.Invoke(this, new WindowChangeMonitorEventArgs(currentForegroundWindow)); })).Start(); // InvokeFocusChange(currentForegroundWindow, NativeHelpers.GetTitleFromHandle(currentForegroundWindow), NativeHelpers.GetProcessFromWindowHandle(currentForegroundWindow)); //OnWindowFocus.BeginInvoke(currentForegroundWindow, Utilities.GetTitleFromHandle(currentForegroundWindow), // Utilities.GetProcessFromWindowHandle(currentForegroundWindow), null, null); }
public WinEventHook(Process process, int?threadId, WinEvent minEvent, WinEvent maxEvent, HookOptions options = HookOptions.None, SynchronizationContext syncCtx = null) { if (!(syncCtx != null || !options.HasFlag(HookOptions.AsyncCallbacks))) { throw new ArgumentException("Contract assertion not met: syncCtx != null || !options.HasFlag(HookOptions.AsyncCallbacks)", nameof(syncCtx)); } this.thCreated = Thread.CurrentThread; this.Observers = new List <IObserver <WinEventEventArgs> >(); this.Process = process; this.ThreadID = threadId; this.MinEvent = minEvent; this.MaxEvent = maxEvent; this.SyncCtx = syncCtx; this.CallbackContext = options.HasFlag(HookOptions.AsyncCallbacks) ? new Action <Action>(CallbackAsync) : new Action <Action>(CallbackSyncIfNeeded); // EventProcHandle is the callback to CLR code, and must be valid. We take a new GCRoot // and only release once a successfull Unhook occurs this.EventProcHandle = new NativeMethods.WinEventProc(Hook_EventProc); this.EventProcHandleGCRoot = GCHandle.Alloc(EventProcHandle); if (!options.HasFlag(HookOptions.StartDisabled)) { StartInternal(); } }
public CursorModule() { llMouseProc = llMouseHookCallback; winEventProc = EventCallback; StartListeningForWindowChanges(); }
private FullScreenHelper() { if (!useNewBehavior) { resizeEventProc = ResizeEventCallback; tasksSvc.Windows.CollectionChanged += Windows_CollectionChanged; if (resizeEventHook == IntPtr.Zero) { resizeEventHook = NativeMethods.SetWinEventHook( NativeMethods.EVENT_OBJECT_LOCATIONCHANGE, NativeMethods.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, resizeEventProc, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT | NativeMethods.WINEVENT_SKIPOWNPROCESS); } } else { fullscreenCheck = new DispatcherTimer(DispatcherPriority.Background, System.Windows.Application.Current.Dispatcher); fullscreenCheck.Interval = new TimeSpan(0, 0, 0, 0, 100); fullscreenCheck.Tick += FullscreenCheck_Tick; fullscreenCheck.Start(); } }
static void Main() { if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { return; } Proc = new NativeMethods.WinEventProc(WindowEventCallback); NativeMethods.SetWinEventHook(NativeMethods.EVENT_SYSTEM_MOVESIZEEND, NativeMethods.EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, Proc, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT); Application.Run(); }
private FullScreenHelper() { resizeEventProc = ResizeEventCallback; tasksSvc.Windows.CollectionChanged += Windows_CollectionChanged; if (resizeEventHook == IntPtr.Zero) { resizeEventHook = NativeMethods.SetWinEventHook( NativeMethods.EVENT_OBJECT_LOCATIONCHANGE, NativeMethods.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, resizeEventProc, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT | NativeMethods.WINEVENT_SKIPOWNPROCESS); } }
private void btn_set_event_Click(object sender, RoutedEventArgs e) { Dictionary <AccessibleEvents, NativeMethods.WinEventProc> events = InitializeWinEventToHandlerMap(); //initialize the first event to LocationChanged NativeMethods.WinEventProc eventHandler = new NativeMethods.WinEventProc(events[AccessibleEvents.LocationChange].Invoke); //When you use SetWinEventHook to set a callback in managed code, //you should use the GCHandle //(Provides a way to access a managed object from unmanaged memory.) //structure to avoid exceptions. //This tells the garbage collector not to move the callback. GCHandle gch = GCHandle.Alloc(eventHandler); //Set Window Event Hool on Location changed. g_hook = NativeMethods.SetWinEventHook(AccessibleEvents.LocationChange, AccessibleEvents.LocationChange, IntPtr.Zero, eventHandler , 0, 0, NativeMethods.SetWinEventHookParameter.WINEVENT_OUTOFCONTEXT); //Hook window close event - close our HoverContorl on Target window close. eventHandler = new NativeMethods.WinEventProc (events[AccessibleEvents.Destroy].Invoke); gch = GCHandle.Alloc(eventHandler); g_hook = NativeMethods.SetWinEventHook(AccessibleEvents.Destroy, AccessibleEvents.Destroy, IntPtr.Zero, eventHandler , 0, 0, NativeMethods.SetWinEventHookParameter.WINEVENT_OUTOFCONTEXT); //AccessibleEvents -> //http://msdn.microsoft.com/en-us/library/system.windows.forms.accessibleevents.aspx //SetWinEventHookParameter -> //http://msdn.microsoft.com/en-us/library/dd373640(VS.85).aspx }
public CursorModule(PlayerInfo player) : base(player) { llMouseProc = llMouseHookCallback; winEventProc = EventCallback; }
internal void StartListeningForWindowChanges() { _listener = EventCallback; //setting the window hook _winHook = NativeMethods.SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _listener, 0, 0, WINEVENT_OUTOFCONTEXT); }
public static extern IntPtr SetWinEventHook(AccessibleEvents eventMin, AccessibleEvents eventMax, IntPtr eventHookAssemblyHandle, NativeMethods.WinEventProc eventHookHandle, uint processId, uint threadId, NativeMethods.SetWinEventHookParameter parameterFlags);
public GameOverlay() { InitializeComponent(); // // add buttons for each player // for (int i = 1; i <= 4; i++) // { // Button button = MakeButton(i); // orangePlayerList.Children.Add(button); // } // // for (int i = 1; i <= 4; i++) // { // Button button = MakeButton(i + 5); // bluePlayerList.Children.Add(button); // } Process[] processes = Process.GetProcessesByName("echovr"); // Process[] processes = Process.GetProcessesByName("Unity Hub"); if (processes.Length < 1) { Close(); return; } targetWindow = NativeMethods.Find(processes[0].ProcessName, processes[0].MainWindowTitle); // if window not found if (targetWindow.Equals(IntPtr.Zero)) { Close(); return; } NativeMethods.SetWindowPosition(this, targetWindow); Dictionary <AccessibleEvents, NativeMethods.WinEventProc> events = InitializeWinEventToHandlerMap(); //initialize the first event to LocationChanged NativeMethods.WinEventProc eventHandler = new NativeMethods.WinEventProc(events[AccessibleEvents.LocationChange].Invoke); //When you use SetWinEventHook to set a callback in managed code, //you should use the GCHandle //(Provides a way to access a managed object from unmanaged memory.) //structure to avoid exceptions. //This tells the garbage collector not to move the callback. GCHandle gch = GCHandle.Alloc(eventHandler); //Set Window Event Hool on Location changed. g_hook = NativeMethods.SetWinEventHook(AccessibleEvents.LocationChange, AccessibleEvents.LocationChange, IntPtr.Zero, eventHandler , 0, 0, NativeMethods.SetWinEventHookParameter.WINEVENT_OUTOFCONTEXT); //Hook window close event - close our HoverContorl on Target window close. // eventHandler = events[AccessibleEvents.Destroy].Invoke; // // gch = GCHandle.Alloc(eventHandler); // // g_hook = NativeMethods.SetWinEventHook(AccessibleEvents.Destroy, // AccessibleEvents.Destroy, IntPtr.Zero, eventHandler // , 0, 0, NativeMethods.SetWinEventHookParameter.WINEVENT_OUTOFCONTEXT); }
private void AddPositionalEvents() { Dictionary<AccessibleEvents, NativeMethods.WinEventProc> events = this.InitializeWinEventToHandlerMap(); NativeMethods.WinEventProc eventHandler = new NativeMethods.WinEventProc(events[AccessibleEvents.LocationChange].Invoke); //Tell the garbage collector not to move the callback. GCHandle.Alloc(eventHandler); this.hook = NativeMethods.SetWinEventHook(AccessibleEvents.LocationChange, AccessibleEvents.LocationChange, IntPtr.Zero, eventHandler, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT); eventHandler = new NativeMethods.WinEventProc(events[AccessibleEvents.Destroy].Invoke); //Tell the garbage collector not to move the callback. GCHandle.Alloc(eventHandler); this.hook = NativeMethods.SetWinEventHook(AccessibleEvents.Destroy, AccessibleEvents.LocationChange, IntPtr.Zero, eventHandler, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT); }