首页 > 代码库 > 窗口截图(可指定HWND窗口句柄)(三篇文章)
窗口截图(可指定HWND窗口句柄)(三篇文章)
[cpp] view plain copy
- BOOL SaveHwndToBmpFile(HWND hWnd, LPCTSTR lpszPath)
- {
- HWND hDesktop = ::GetDesktopWindow();
- ASSERT(hDesktop);
- if(NULL == hWnd)
- {
- hWnd = hDesktop;
- }
- RECT rect;
- ::GetWindowRect(hWnd, &rect);
- int nWidht = rect.right - rect.left;
- int nHeight = rect.bottom - rect.top;
- HDC hSrcDC = ::GetWindowDC(hWnd);
- ASSERT(hSrcDC);
- HDC hMemDC = ::CreateCompatibleDC(hSrcDC);
- ASSERT(hMemDC);
- HBITMAP hBitmap = ::CreateCompatibleBitmap(hSrcDC, nWidht, nHeight);
- ASSERT(hBitmap);
- HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC, hBitmap);
- ::BitBlt(hMemDC, 0, 0, nWidht, nHeight, hSrcDC, 0, 0, SRCCOPY);
- BITMAP bitmap = {0};
- ::GetObject(hBitmap, sizeof(BITMAP), &bitmap);
- BITMAPINFOHEADER bi = {0};
- BITMAPFILEHEADER bf = {0};
- CONST int nBitCount = 24;
- bi.biSize = sizeof(BITMAPINFOHEADER);
- bi.biWidth = bitmap.bmWidth;
- bi.biHeight = bitmap.bmHeight;
- bi.biPlanes = 1;
- bi.biBitCount = nBitCount;
- bi.biCompression = BI_RGB;
- DWORD dwSize = ((bitmap.bmWidth * nBitCount + 31) / 32) * 4 * bitmap.bmHeight;
- HANDLE hDib = GlobalAlloc(GHND, dwSize + sizeof(BITMAPINFOHEADER));
- LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
- *lpbi = bi;
- ::GetDIBits(hMemDC, hBitmap, 0, bitmap.bmHeight, (BYTE*)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
- try
- {
- CFile file;
- file.Open(lpszPath, CFile::modeCreate | CFile::modeWrite);
- bf.bfType = 0x4d42;
- dwSize += sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- bf.bfSize = dwSize;
- bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- file.Write((BYTE*)&bf, sizeof(BITMAPFILEHEADER));
- file.Write((BYTE*)lpbi, dwSize);
- file.Close();
- }
- catch(CFileException* e)
- {
- e->ReportError();
- e->Delete();
- }
- GlobalUnlock(hDib);
- GlobalFree(hDib);
- ::SelectObject(hMemDC, hOldBitmap);
- ::DeleteObject(hBitmap);
- ::DeleteDC(hMemDC);
- ::ReleaseDC(hWnd, hSrcDC);
- return TRUE;
- }
- // 调用方法
- HWND hWnd = ::FindWindow(NULL, _T("XXX"));
- if(hWnd)
- {
- SaveHwndToBmpFile(hWnd, _T("F://12.bmp"));
- }
http://blog.csdn.net/visualeleven/article/details/6202796
http://blog.csdn.net/visualeleven/article/details/6206715
http://blog.csdn.net/visualeleven/article/details/6093484
窗口截图(可指定HWND窗口句柄)(三篇文章)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。