#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <atlimage.h>
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
std::string getCurrentTime() {
time_t now = time(0);
tm *local = localtime(&now);
char buf[80];
strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", local);
return std::string(buf);
}
void ScreenShot()
{
if (!PathIsDirectory(".\\ShotImg"))
{
CreateDirectory(".\\ShotImg", 0);
}
HDC hdcSrc = GetDC(NULL);
int nBitPerPixel = GetDeviceCaps(hdcSrc, BITSPIXEL);
int nWidth = GetDeviceCaps(hdcSrc, HORZRES);
int nHeight = GetDeviceCaps(hdcSrc, VERTRES);
CImage image;
image.Create(nWidth, nHeight, nBitPerPixel);
BitBlt(image.GetDC(), 0, 0, nWidth, nHeight, hdcSrc, 0, 0, SRCCOPY);
ReleaseDC(NULL, hdcSrc);
image.ReleaseDC();
char bufName[128] = { 0 };
static int si = 1;
sprintf_s(bufName, 128, ".\\ShotImg\\%s_%d.png", getCurrentTime().c_str(), si++);
image.Save(bufName, Gdiplus::ImageFormatPNG);
}
int main()
{
int i = 0;
while (i<20)
{
ScreenShot();
i++;
Sleep(500);
}
return 0;
}
结果: