MFC通过系统API将指定窗口导出为图片,方法之一:
BOOL COutputManager::OutputFromWND(IN HWND hWnd, IN RECT rect)
{
HDC hDC = ::GetDC(hWnd); //获取屏幕DC
HDC hDCMem = ::CreateCompatibleDC(hDC); //创建兼容DC
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom); //创建兼容位图
HBITMAP hOldMap = ( HBITMAP )::SelectObject(hDCMem, hBitMap); //将位图选入DC,并保存返回值
::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY); //将屏幕DC的图象复制到内存DC中
CImage image;
image.Attach(hBitMap);
image.Save(_T( "c://YMDownloads//output.jpg" ));//如果文件后缀为.bmp,则保存为为bmp格式
image.Detach();
::SelectObject(hDCMem, hOldMap); //选入上次的返回值
//释放
::DeleteObject(hBitMap);
::DeleteDC(hDCMem);
::DeleteDC(hDC);
return TRUE;
}