VC++ 下截取屏幕相关操作:
当我们的应用程序运行在一些设备上的时候,我们有的时候需要一些截屏操作,
用来保存获取软件运行中出现的问题,这个时候截屏显得很重要.
具体实现为:
void CScreenshot::SaveScreen()
{
SYSTEMTIME time;
GetSystemTime(&time);
int mm = time.wMonth;
int dd = time.wDay;
int h = time.wHour;
int m = time.wMinute;
int s = time.wSecond;
TCHAR szPath[MAX_PATH];
_stprintf(szPath, _T("%s\\bmp\\"), g_szDataPath);
CreateDirectory(szPath, NULL);
// 创建保存文件的路径和名称
char filename[300];
TCHAR file[100];
wsprintf(file, _T("%s%02d%02d%02d%02d%02d.bmp"), szPath,mm,dd,h,m,s);
sprintf(filename, "%S", file);
OnScreenSave(filename);
MessageBox(g_hwnd, _T("截屏失败!"), _T("提示"), MB_OK);
}
void CScreenshot::OnScreenSave(const char *filename)
{
HDC hScrDC, hMemDC;
int width, height;
BYTE *lpBitmam_pBitsMap = NULL;
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES);
hMemDC = CreateCompatibleDC(hScrDC);
BITMAPINFO RGB24BitsBITMAPINFO;
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO, DIB_RGB_COLORS, (void **)&lpBitmam_pBitsMap, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42;
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3);
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))
{
//写位图文件头
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmam_pBitsMap, 3*width*height, 1, mStream);
fclose(mStream);
}
SelectObject(hMemDC, previousObject);
DeleteObject(directBmp);
DeleteDC(hMemDC);
DeleteDC(hScrDC);
}
以上仅供参看!