C++实现屏幕截图的方法
来源:互联网 发布:ubuntu自带拼音输入法 编辑:程序博客网 时间:2024/05/22 07:51
第一步:建立Windows 32控制台控制程序第二部:设置工程的属性如下:MFC的使用 --> 在共享 DLL 中使用 MFCstdafx.hScreenCapture.cpp : 定义控制台应用程序的入口点。
#include"stdafx.h"
#include"SC.h"
#include <windows.h>
int_tmain(int argc, _TCHAR* argv[])
{
Sleep(10000);
Screen("aa.jpg");
return 0;
}SC.hvoid Screen(char filename[]);SC.cpp#include"stdafx.h"
#include <afxwin.h>
voidScreen(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("当前屏幕色彩模式为%d位色彩\n", BitPerPixel);
printf("屏幕宽度:%d\n", Width);
printf("屏幕高度:%d\n", Height);
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中的位图到文件
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <tchar.h>
#include
#include
#include <windows.h>
int
{
}SC.hvoid Screen(char filename[]);SC.cpp#include
#include <afxwin.h>
void
{