搜索
您的当前位置:首页正文

c++如何截取桌面

来源:榕意旅游网
#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;
}

结果:

因篇幅问题不能全部显示,请点此查看更多更全内容

Top