您的位置:首页 > 其它

定时自动打开网页并截图

2017-01-09 10:53 267 查看
由于工作原因,需要定时获取某个网站的信息,该程序可以提供无人监管的自动截图

1、主程序

#include <windows.h>
#include<iostream>
#include "stdafx.h"
#include "sc.h"
#include <string>
using namespace std;
int main()
{

while(1)
{

SYSTEMTIME ct;
GetLocalTime(&ct);//local time
cout<<ct.wYear<<"年"<<ct.wMonth<<"月"<<
ct.wDay<<"日"<<ct.wHour<<"时"<<
ct.wMinute<<"分"<<ct.wSecond<<"秒"<<
" (整点第10分钟截图)"<<endl;
char ch_time[64];
sprintf(ch_time,"%d.%d.%d_%d-%d-%d",ct.wYear,ct.wMonth,ct.wDay,ct.wHour,ct.wMinute,ct.wSecond);
Sleep(1000);

if(ct.wMinute==10 && ct.wSecond==0)
{
ShellExecute(NULL, L"open", L"IEXPLORE", L"https://www.aqistudy.cn/", NULL,SW_SHOWMAXIMIZED);
Sleep(10000);
string str_time_now;
str_time_now.append(ch_time).append(".jpg");
char pic_name[64] = {0};
strcat(pic_name,str_time_now.c_str());
Screen(pic_name);
}
}
return 0;
}


2、头文件sc.h与stdafx.h
sc.h

void Screen(char filename[]);

stdafx.h

#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <tchar.h>


3、sc.cpp

#include "stdafx.h"
#include <afxwin.h>

void Screen(char filename[])
{
CDC *pDC;//屏幕DC
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);

printf("当前截屏时间: ", BitPerPixel);

CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);

CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);

oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC

//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息

FILE *fp = fopen(filename, "w+b");

BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount = bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度

BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;

fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//写入位图文件头

fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//写入位图信息头

byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据

GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据

fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//写入位图数据

delete [] p;

fclose(fp);

memDC.SelectObject(oldmemBitmap);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: