// nCmdShow的含义 //0 关闭窗口 //1 正常大小显示窗口 //2 最小化窗口 //3 最大化窗口 //使用实例: ShowWindow(myPtr, 0); #endregion /// <summary> /// 根据窗口HWND截取窗口 /// </summary> /// <param name="handle"></param> /// <returns></returns> public static Image GetWindowCapture(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = GetWindowDC(handle); ShowWindow(hdcSrc, 1); // get the size SC_RECT windowRect = new SC_RECT(); GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = SelectObject(hdcDest, hBitmap); // bitblt over BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY | CAPTUREBLT); // restore selection SelectObject(hdcDest, hOld); // clean up DeleteDC(hdcDest); ReleaseDC(handle, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object DeleteObject(hBitmap); return(img); }
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref SC_RECT rect);