u012226625的专栏

生于忧患 死于安乐!

窗口截图

窗口截图,可以获取遮挡窗口完整的图像,并保存到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"));

}

阅读更多
文章标签: vs2010 vs2008 mfc
个人分类: MFC
上一篇vector 对象的定义和初始化
下一篇vector 对象操作
想对作者说点什么? 我来说一句

最小化窗口截图(含源码)

2015年04月05日 45KB 下载

没有更多推荐了,返回首页

关闭
关闭