这是微软自带的一个利用cimage进行截图的例子,值得看看

来源:互联网 发布:国际经济数据 编辑:程序博客网 时间:2024/05/22 05:25

struct CaptureData{BOOL    bCaptureFullScreen;char szCapturePath[MAX_PATH]; // no unicode path supportchar 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// slotif (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);}


0 0
原创粉丝点击