Windows还是Linux?

人生是无法阻止的前进! 酒醒诗残梦断,南国正清秋。把剑凄然望,无处归舟。
随笔 - 9, 文章 - 10, 评论 - 1, 引用 - 0
数据加载中……

使用SDK截图

转载自:http://blog.csdn.net/Lsaint/archive/2006/11/05/1367832.aspx



#include 
<iostream>
#include 
<windows.h>
using namespace std;

bool SaveBitmapToFile(HDC hDC , HBITMAP hBitmap , LPCTSTR name)
{
 
//BITMAP是个结构,记录了已调入内存的BMP图象的宽高,颜色等信息以及图象数据
 
//与系统当前得显示模式,色深有关。
 BITMAP bm; 
 DWORD size;
 GetObject(hBitmap,
sizeof(bm),(LPBYTE)&bm); // SDK中从HBITMAP获得BITMAP的方法 MFC中用GetBitmap
 size = bm.bmWidthBytes*bm.bmHeight;

 HGLOBAL hMen 
= GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,size);//从堆中分配内存
 if(hMen==0)
  
return false;

 LPSTR lpData 
= (LPSTR)GlobalLock(hMen); // 锁定分配的内存并获得指针

 BITMAPINFOHEADER bih; 
// 位图信息头
 bih.biSize = sizeof(BITMAPINFOHEADER);
 bih.biWidth 
= bm.bmWidth;
 bih.biHeight 
= bm.bmHeight;
 bih.biPlanes 
= 1;
 bih.biBitCount 
= bm.bmBitsPixel;
 bih.biCompression 
= 0;
 bih.biSizeImage 
= 0;
 bih.biXPelsPerMeter 
= 0;
 bih.biYPelsPerMeter 
= 0;
 bih.biClrUsed 
= 0;
 bih.biClrImportant 
= 0;

 
if(GetDIBits(hDC,hBitmap,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS)==0)
 {
// 将位图中的数据以bits的形式放入lpData指向的内存单元
  GlobalFree(hMen);
  
return false;
 }

 BITMAPFILEHEADER bfh; 
// 位图文件头
 bfh.bfType = ((WORD)('M'<<8)|'B');
 bfh.bfReserved1 
= 0;
 bfh.bfReserved2 
= 0;
 bfh.bfSize 
= sizeof(bfh)+size;
 bfh.bfOffBits 
= sizeof(bfh);

 HANDLE hFile;  
// MFC中用CFile
 DWORD dWrittenNum;
 hFile 
= CreateFile(name,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
 
if( hFile == INVALID_HANDLE_VALUE )
 {
  cout
<<"CREATE FILE FALSE"<<endl;
  
return false;
 }
 
//SetFilePointer(hFile,0,0,FILE_BEGIN);
 
//MFC 用WriteHuge()
 
// BMP文件由文件头、位图信息头、颜色信息和图形数据四部分组成。  
 WriteFile(hFile,&bfh,sizeof(bfh),&dWrittenNum,0); // 文件头
 
//SetFilePointer(hFile,sizeof(bfh),0,FILE_BEGIN);
 WriteFile(hFile,&bih,sizeof(bih),&dWrittenNum,0); // 位图信息头
 WriteFile(hFile,lpData,size,&dWrittenNum,0); // 图形数据

 CloseHandle(hFile);
 GlobalFree(hMen); 
// 打扫

 
return true;
 
}

 

int main()
{

 HDC hDC , hmDC;
 
int nWidth , nHeight;
 HBITMAP hBitmap ;
 
 hDC 
= CreateDC("DISPLAY",0,0,0);  //  用DISPLAY参数获得整个屏幕DC
 nWidth = GetDeviceCaps(hDC,HORZRES);
 nHeight 
= GetDeviceCaps(hDC,VERTRES);
 hmDC 
= CreateCompatibleDC(hDC); // 内存DC
 hBitmap = CreateCompatibleBitmap(hDC,nWidth,nHeight); // 内存位图
 SelectObject(hmDC , hBitmap);
 BitBlt(hmDC,
0,0,nWidth,nHeight,hDC,0,0,SRCCOPY); // 把屏幕DC的内容BitBlt到内存DC 


#ifdef STORE_IN_CLIPBOARD
 
if((OpenClipboard(NULL)!=0)&&(EmptyClipboard()!=0)) // 打开&清空剪贴板
 {
  SetClipboardData(CF_BITMAP,hBitmap); 
// 写入剪贴板
  CloseClipboard();
 }
#else
 LPCTSTR name;
 name 
= "C:\\lsaint.bmp"// 文件保存路径
 
 
if(!SaveBitmapToFile(hDC,hBitmap,name))
  cout
<<"false"<<endl;
#endif

 
return true;


 

参考:

《编写自己的屏幕捕获木马》                  作者:RITATV
《GetBitmapBits和GetDIBits的区别》 作者:笨笨

http://dev.csdn.net/develop/article/12/12484.shtm
http://dev.csdn.net/develop/article/7/7293.shtm

posted on 2010-11-02 16:56 向前看勿回头 阅读(500) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理