MFC截屏之一:C++截取整个屏幕,并生成图片(基于MFC)
(2011-12-13 17:00:07)
第一步:建立Windows 32控制台控制程序 第二部:设置工程的属性如下:MFC的使用 --> 在共享 DLL 中使用 MFC stdafx.h #pragma once #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 #include <tchar.h>
ScreenCapture.cpp:此文件主要是调用截屏方法 // ScreenCapture.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include "SC.h" #include <windows.h> int _tmain(int argc, _TCHAR* argv[]) { Sleep(10000); Screen("aa.jpg"); return 0; } SC.h void Screen(charfilename[]); 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("当前屏幕色彩模式为%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中的位图到文件 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); }
CDC *pDC=GetDesktopWindow()->GetDC();//屏幕DC int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式 int Width = pDC->GetDeviceCaps(HORZRES); int Height = pDC->GetDeviceCaps(VERTRES); 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("c:\\1.jpg", "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); memDC.DeleteDC(); ReleaseDC(pDC);
转载地址 http://www.gooogleman.com/forum.php?mod=viewthread&tid=19255
box1.Refresh();
MFC实现自动截屏
(2011-07-14 14:35:55)
(一)功能 实现了一个定时截取当前屏幕图像的小程序。 (二)准备工作 1)建立VC CONSOLE APPLICATION,选择MFC SUPPORT 2)在STDAFX.H文件中加入头文件:conio.h (三)主程序 主程序代码如下: charFilename[100]; intcount = 0; while(!_kbhit())//用户按键则退出 { Sleep(5000);//挂起5秒 count++; sprintf(Filename,"%d.bmp", count); Screen(Filename);//调用Screen函数 } 以上代码每隔5秒钟调用一次函数Screen,将当前屏幕保存到文件中。 (四)工作函数Screen Screen实现了当前屏幕内容到bmp文件的拷贝。 源代码如下: void Screen(char filename[]) { CDC*pDC;//屏幕DC pDC= CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC intBitPerPixel =pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式 intWidth = pDC->GetDeviceCaps(HORZRES); intHeight = pDC->GetDeviceCaps(VERTRES); cout<< "当前屏幕色彩模式为"<< BitPerPixel<< "位色彩"<< endl <<"屏幕宽度:" << Width<< endl <<"屏幕高度:" << Height<< endl<< endl; CDCmemDC;//内存DC memDC.CreateCompatibleDC(pDC); CBitmapmemBitmap, *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中的位图到文件 BITMAPbmp; memBitmap.GetBitmap(&bmp);//获得位图信息 FILE*fp = fopen(filename, "w+b"); BITMAPINFOHEADERbih = {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;//宽度 BITMAPFILEHEADERbfh = {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); }