- 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