struct CaptureData
{
BOOL bCaptureFullScreen;
char szCapturePath[MAX_PATH]; // no unicode path support
char szCaptureFilename[MAX_PATH];
};
void WINAPI CaptureScreen(CaptureData* lpData)
{
ASSERT(lpData);
if (!lpData)
return;
AFX_MANAGE_STATE(AfxGetStaticModuleState());
BOOL bStat;
CImage image;
CWnd *pWnd;
CRect rect;
if (lpData->bCaptureFullScreen) {
pWnd = CWnd::GetDesktopWindow();
}
else {
pWnd = CWnd::GetActiveWindow();
}
ASSERT(pWnd);
if (pWnd == NULL)
return;
CWindowDC winDC(pWnd);
pWnd->GetWindowRect(rect);
int nBPP = winDC.GetDeviceCaps(BITSPIXEL) * winDC.GetDeviceCaps(PLANES);
if (nBPP < 24)
nBPP = 24;
bStat = image.Create(rect.Width(), rect.Height(), nBPP); //创建图像
ASSERT(bStat);
if (!bStat)
return;
CImageDC imageDC(image);
//将图像绘制到imageDC 中
::BitBlt(imageDC, 0, 0, rect.Width(), rect.Height(), winDC, 0, 0, SRCCOPY);
CString strTempName = GetTempName(CString(lpData->szCapturePath)); //保存为指定格式和路径中
HRESULT hr = image.Save(strTempName);
if (FAILED(hr)) {
TRACE("Couldn't Save File: %s, %x\n", (LPCTSTR)strTempName, hr);
return;
}
strncpy_s(lpData->szCaptureFilename, MAX_PATH, CT2A(::PathFindFileName(strTempName)), _TRUNCATE);
}
//获取文件名和路径
static CString GetTempName(CString strPath)
{
const int nMin = 0;
const int nMax = 999;
const CString strBase(_T("ScreenCap"));
static int nLastKnown = nMin;
// count up sequentially to make sure we take the next available
// slot
if (strPath.Right(1) != "\\")
strPath += '\\';
bool bFound = false;
CString strPathName;
while (!bFound) {
if (nLastKnown > nMax)
break;
strPathName = strPath + strBase;
strPathName.AppendFormat(_T("%03.3d.png"), nLastKnown++);
HANDLE hFile = ::CreateFile(strPathName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
::CloseHandle(hFile);
bFound = TRUE;
}
}
if (!bFound)
strPathName.Empty();
return(strPathName);
}
这是微软自带的一个利用cimage进行截图的例子,值得看看
最新推荐文章于 2019-04-09 16:50:47 发布