/// <summary> /// Callback for the SetHookEx function /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> public int KeyHook(int nCode, IntPtr wParam, IntPtr lParam) { keysChanged = false; if (nCode < 0) { return(Win32Functions.CallNextHookEx(hKeyHook, nCode, wParam, lParam)); } else { int vkCode = Marshal.ReadInt32(lParam); //handle key up/key down if (wParam == (IntPtr)Win32Defines.WM_KEYDOWN || wParam == (IntPtr)Win32Defines.WM_SYSKEYDOWN) { if (vkCode == Key.VK_CONTROL) { ctrlDown = true; } else if (vkCode == Key.VK_SNAPSHOT) { if (!psDown) { psDown = true; keysChanged = true; } } else if (vkCode == Key.VK_ALT) { altDown = true; } } else if (wParam == (IntPtr)Win32Defines.WM_KEYUP || wParam == (IntPtr)Win32Defines.WM_SYSKEYUP) { if (vkCode == Key.VK_CONTROL) { ctrlDown = false; } if (vkCode == Key.VK_SNAPSHOT) { psDown = false; } if (vkCode == Key.VK_ALT) { altDown = false; } } //make sure a key was down to continue... if ((wParam == (IntPtr)Win32Defines.WM_KEYDOWN || wParam == (IntPtr)Win32Defines.WM_SYSKEYDOWN) && canContinue() && vkCode == Key.VK_SNAPSHOT) { Image img = null; if (captureWindow()) { Win32Functions.RECT windowRect = new Win32Functions.RECT(); //get the top level window!! IntPtr hWnd = new IntPtr(Win32Functions.GetForegroundWindow()); IntPtr hdcSrc = Win32Functions.GetWindowDC((int)hWnd); Win32Functions.GetWindowRect((int)hWnd, ref windowRect); // create a bitmap from the visible clipping bounds of //the graphics object from the window int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = Win32Functions.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = Win32Functions.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = Win32Functions.SelectObject(hdcDest, hBitmap); // bitblt over Win32Functions.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Win32Defines.SRCCOPY); // restore selection Win32Functions.SelectObject(hdcDest, hOld); // clean up Win32Functions.DeleteDC(hdcDest); Win32Functions.ReleaseDC(hWnd, hdcSrc); // get a .NET image object for it img = Image.FromHbitmap(hBitmap); // free up the Bitmap object Win32Functions.DeleteObject(hBitmap); } else { Win32Functions.RECT windowRect = new Win32Functions.RECT(); //get the top level window!! IntPtr hWnd = new IntPtr(Win32Functions.GetDesktopWindow()); IntPtr hdcSrc = Win32Functions.GetWindowDC((int)hWnd); Win32Functions.GetWindowRect((int)hWnd, ref windowRect); // create a bitmap from the visible clipping bounds of //the graphics object from the window int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = Win32Functions.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = Win32Functions.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = Win32Functions.SelectObject(hdcDest, hBitmap); // bitblt over Win32Functions.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Win32Defines.SRCCOPY); // restore selection Win32Functions.SelectObject(hdcDest, hOld); // clean up Win32Functions.DeleteDC(hdcDest); Win32Functions.ReleaseDC(hWnd, hdcSrc); // get a .NET image object for it img = Image.FromHbitmap(hBitmap); // free up the Bitmap object Win32Functions.DeleteObject(hBitmap); } callBack(img); } return(Win32Functions.CallNextHookEx(hKeyHook, nCode, wParam, lParam)); } }