关闭

窗口截图

标签: vs2010vs2008mfc
395人阅读 评论(0) 收藏 举报
分类:

窗口截图,可以获取遮挡窗口完整的图像,并保存到bmp文件中去。主要用到的函数中PrintWindow,VC6不支持该函数,请使用VS2008或vs2010来编译。其它需求如平台要求请参考MSDN文档。

void SaveHwndToBmpFile(HWND hWnd, LPCTSTR lpszPath)
{
HDC hDC = ::GetWindowDC(hWnd);
ASSERT(hDC);

HDC hMemDC = ::CreateCompatibleDC(hDC);
ASSERT(hMemDC);

RECT rc;
::GetWindowRect(hWnd, &rc);


HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, rc.right - rc.left, rc.bottom - rc.top);
ASSERT(hBitmap);

HBITMAP hOldBmp = (HBITMAP)::SelectObject(hMemDC, hBitmap);
::PrintWindow(hWnd, hMemDC, 0);


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, hOldBmp);
::DeleteObject(hBitmap);
::DeleteObject(hMemDC);
::ReleaseDC(hWnd, hDC);
}


// 调用方法
HWND hWnd = ::FindWindow(NULL, _T("XXX"));
if(hWnd)
{
   SaveHwndToBmpFile(hWnd, _T("F://12.bmp"));

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13800次
    • 积分:377
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:8篇
    • 译文:0篇
    • 评论:1条
    最新评论