static void UnsubscribeFromWindowActivationEvents() { if (windowActivationHookHandle == 0) { return; } int result = Win.UnhookWinEvent(windowActivationHookHandle); windowActivationHookHandle = 0; windowActivationDelegate = null; HandleAnyErrors(result); }
static void SubscribeToWindowActivationEvents() { if (windowActivationHookHandle != 0) { return; } windowActivationDelegate = WinEventProc; windowActivationHookHandle = Win.SetWinEventHook(Win.EVENT_SYSTEM_FOREGROUND, Win.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, windowActivationDelegate, 0, 0, Win.WINEVENT_OUTOFCONTEXT); HandleAnyErrors(windowActivationHookHandle); }
public static string GetActiveWindowTitle() { const int nChars = 256; StringBuilder stringBuilder = new StringBuilder(nChars); IntPtr foregroundWindowHandle = Win.GetForegroundWindow(); if (Win.GetWindowText(foregroundWindowHandle, stringBuilder, Win.GetWindowTextLength(foregroundWindowHandle) + 1) > 0) { return(stringBuilder.ToString()); } return(null); }
public static void Start() { if (StepDiagram != null) { throw new Exception("Unable to start twice. Already listening. Call HookEngine.Stop after calling HookEngine.Start"); } activeWindowHandle = Win.GetActiveWindow(); StepDiagram = new StepDiagram(); cpuUsageCalculator = new CpuUsageCalculator(); currentProcessTimer = new Timer(CheckCpuUsage, null, 100, 100); HookEvents(); StepDiagram.AddTaskActive(WindowsEvents.GetActiveWindowTitle(), Win.GetForegroundWindow()); }
static ScreenGrab Grab(int left, int top, int width, int height) { using (System.Drawing.Bitmap screenBmp = BitBltGrab(left, top, width, height)) { IntPtr hBitmap = screenBmp.GetHbitmap(); BitmapSource image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); Win.DeleteObject(hBitmap); return(new ScreenGrab() { Image = image, Time = DateTime.Now, TopLeft = new Point(left, top) }); } }
/// <summary> /// Grabs the specified area of the screen including alpha blended windows on top such as tool tips and menus. /// IMPORTANT: You must call dispose on the bitmap returned by this function as soon as you are done with it. /// </summary> private static System.Drawing.Bitmap BitBltGrab(int left, int top, int width, int height) { IntPtr hDesktop = Win.GetDesktopWindow(); IntPtr dcDesktop = Win.GetWindowDC(hDesktop); IntPtr dcTarget = Win.CreateCompatibleDC(dcDesktop); IntPtr hTargetBitmap = Win.CreateCompatibleBitmap(dcDesktop, width, height); IntPtr hOldBitmap = Win.SelectObject(dcTarget, hTargetBitmap); bool success = Win.BitBlt(dcTarget, 0, 0, width, height, dcDesktop, left, top, System.Drawing.CopyPixelOperation.SourceCopy | System.Drawing.CopyPixelOperation.CaptureBlt); System.Drawing.Bitmap bitmap = System.Drawing.Bitmap.FromHbitmap(hTargetBitmap); Win.SelectObject(dcTarget, hOldBitmap); Win.DeleteObject(hTargetBitmap); Win.DeleteDC(dcTarget); Win.ReleaseDC(hDesktop, dcDesktop); return(bitmap); }
public System.Drawing.Point GetCaretPosition() { Win.GUITHREADINFO guiInfo = new Win.GUITHREADINFO(); guiInfo.cbSize = (uint)Marshal.SizeOf(guiInfo); if (!Win.GetGUIThreadInfo(0, out guiInfo)) { return(System.Drawing.Point.Empty); } System.Drawing.Point caretPosition = new System.Drawing.Point(); caretPosition.X = (int)guiInfo.rcCaret.Left + 10; // +10 to better center the caret. caretPosition.Y = (int)guiInfo.rcCaret.Bottom - 10; // -10 to better center the caret. Win.ClientToScreen(guiInfo.hwndCaret, ref caretPosition); return(caretPosition); }
private static void UnsubscribeFromKeyboardEvents() { if (keyboardHookHandle == 0) // Already unsubscribed. { return; } if (keyDownHandler != null || keyUpHandler != null) // We have listeners. { return; } int result = Win.UnhookWindowsHookEx(keyboardHookHandle); keyboardHookHandle = 0; keyboardDelegate = null; HandleAnyErrors(result); }
private static void TryUnsubscribeFromMouseEvents() { if (mouseHookHandle == 0) // Already unsubscribed. { return; } if (WeHaveMouseEventListeners) { return; } int result = Win.UnhookWindowsHookEx(mouseHookHandle); mouseHookHandle = 0; mouseDelegate = null; HandleAnyErrors(result); }
private static int MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct)); MouseButtons button = MouseButtons.None; short mouseDelta = 0; int clickCount = 0; bool mouseDown = false; bool mouseUp = false; switch (wParam) { case Win.WM_LBUTTONDOWN: mouseDown = true; button = MouseButtons.Left; clickCount = 1; break; case Win.WM_LBUTTONUP: mouseUp = true; button = MouseButtons.Left; clickCount = 1; break; case Win.WM_RBUTTONDOWN: mouseDown = true; button = MouseButtons.Right; clickCount = 1; break; case Win.WM_MBUTTONDOWN: mouseDown = true; button = MouseButtons.Middle; clickCount = 1; break; case Win.WM_MBUTTONUP: mouseUp = true; button = MouseButtons.Middle; clickCount = 1; break; case Win.WM_RBUTTONUP: mouseUp = true; button = MouseButtons.Right; clickCount = 1; break; case Win.WM_MOUSEWHEEL: // High-order word of MouseData is the wheel delta. mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); break; // TODO: Handle WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, & WM_NCXBUTTONDBLCLK. } Point mousePos = mouseHookStruct.Point; MouseEventArgs e = new MouseEventArgs(button, clickCount, mousePos.X, mousePos.Y, mouseDelta); if (mouseUpHandler != null && mouseUp) { mouseUpHandler.Invoke(null, e); } if (mouseDownHandler != null && mouseDown) { mouseDownHandler.Invoke(null, e); } if (mouseWheelHandler != null && mouseDelta != 0) { mouseWheelHandler.Invoke(null, e); } if (mouseMoveHandler != null && !previousMousePos.Equals(mousePos)) { previousMousePos = mousePos; if (mouseMoveHandler != null) { mouseMoveHandler.Invoke(null, e); } } } return(Win.CallNextHookEx(mouseHookHandle, nCode, wParam, lParam)); }