怎么样使用ScreenBase实现截取屏幕任意区域?
怎么样使用ScreenBase实现截取屏幕任意区域?
//========================================================================
//TITLE:
// ScreenBase实现屏幕任意区域的截取
//AUTHOR:
// norains
//DATE:
// Tuesday 16-January -2007
//Environment:
//EVC4.0 + Standard SDK
//========================================================================
1.ScreenBase 源码
ScreenBase为自己写代码方便而封装的一个API类,该类的的主要功能是截取屏幕任意区域的图像,并将之保存为BMP文件.
源代码如下:
//////////////////////////////////////////////////////////////////////
//ScreenBase.h:interfacefortheCScreenBaseclass.
//////////////////////////////////////////////////////////////////////
#ifndefSCREENBASE_H
#defineSCREENBASE_H
//-------------------------------------------------------------------
classCScreenBase
{
public:
voidCopyScreen();
voidSaveFile(TCHAR*pszPath);
CScreenBase();
virtual~CScreenBase();
voidInitialize(constRECT*prcScr=NULL);
protected:
voidDestroy();
typedefstruct
{
HDChDC;//TheDCsavethescreedDC
HGDIOBJhOldSel;//TheoldobjectofsaveDC
HBITMAPhBmpSel;//Theselectedbitmap
}SAVEDCINFO,*LPSAVEDCINFO;
SAVEDCINFOm_SaveDCInfo;
typedefstruct
{
BYTE*lpBitmapBits;//Pointertoavariablethatreceivesapointertothelocationofthedevice-independentbitmap'sbitvalues
BITMAPINFObitMapInfo;//Thesavebitmapinfo
}SAVEBITMAPINFO,*LPSAVEBITMAPINFO;
SAVEBITMAPINFOm_SaveBitmapInfo;
voidSaveFile(LPSAVEBITMAPINFOlpSaveBitmapInfo,TCHAR*pszPath);
voidInitializeSaveDC(LPSAVEDCINFOlpSaveDCInfo,LPSAVEBITMAPINFOlpSaveBitmapInfo,constRECT*prcSave);
voidDestroySaveDC(LPSAVEDCINFOlpSaveDCInfo,LPSAVEBITMAPINFOlpSaveBitmapInfo);
voidCopyScreenDC(HDC*pHdcDest,constLPSAVEBITMAPINFOlpSaveBitmapInfo,constRECT*prcSrc);
RECTm_rcSave;//Theareaforsaving
HDCm_hScrDC;//ThehandleDCofscreen
};
//--------------------------------------------------------------------------
#endif//!definedSCREENBASE_H
//////////////////////////////////////////////////////////////////////
//ScreenBase.cpp:implementationoftheCScreenBaseclass.
//////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"ScreenBase.h"
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
CScreenBase::CScreenBase()
{
memset(&m_rcSave,0,sizeof(m_rcSave));
m_hScrDC=NULL;
}
CScreenBase::~CScreenBase()
{
Destroy();
}
//----------------------------------------------------------------------
//Description:
//DestroythesaveDC
//--------------------------------------------------------------------
voidCScreenBase::DestroySaveDC(LPSAVEDCINFOlpSaveDCInfo,LPSAVEBITMAPINFOlpSaveBitmapInfo)
{
if(lpSaveDCInfo->hDC!=NULL&&lpSaveDCInfo->hOldSel!=NULL)
{
SelectObject(lpSaveDCInfo->hDC,lpSaveDCInfo->hOldSel);
}
if(lpSaveDCInfo->hDC!=NULL)
{
DeleteDC(lpSaveDCInfo->hDC);
}
if(lpSaveDCInfo->hBmpSel!=NULL)
{
DeleteObject(lpSaveDCInfo->hBmpSel);
}
memset(lpSaveDCInfo,0,sizeof(SAVEDCINFO));
memset(lpSaveBitmapInfo,0,sizeof(SAVEBITMAPINFO));
}
//----------------------------------------------------------------------
//Description:
//InitializethesaveDC
//--------------------------------------------------------------------
voidCScreenBase::InitializeSaveDC(LPSAVEDCINFOlpSaveDCInfo,LPSAVEBITMAPINFOlpSaveBitmapInfo,constRECT*prcSave)
{
//thepointerwillsaveallpixelpoint'scolorvalue
lpSaveBitmapInfo->lpBitmapBits=NULL;
//createsamemorydevicecontext(DC)compatiblewiththescreendevice(hScrDC)
lpSaveDCInfo->hDC=CreateCompatibleDC(m_hScrDC);
//InitialisethestructBITMAPINFOforthebimapinfomation,
//inordertousethefunctionCreateDIBSectiononwinceos,
//eachpixelstoredby24bits(biBitCount=24)andnocompressing(biCompression=0)
ZeroMemory(&m_SaveBitmapInfo.bitMapInfo,sizeof(BITMAPINFO));
lpSaveBitmapInfo->bitMapInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
lpSaveBitmapInfo->bitMapInfo.bmiHeader.biWidth=prcSave->right-prcSave->left;
lpSaveBitmapInfo->bitMapInfo.bmiHeader.biHeight=prcSave->bottom-prcSave->top;
lpSaveBitmapInfo->bitMapInfo.bmiHeader.biPlanes=1;
lpSaveBitmapInfo->bitMapInfo.bmiHeader.biBitCount=24;
//usethefunctionCreateDIBSectionandSelectObjectinordertogetthebimappointer:lpBitmapBits
lpSaveDCInfo->hBmpSel=CreateDIBSection(lpSaveDCInfo->hDC,
&lpSaveBitmapInfo->bitMapInfo,
DIB_RGB_COLORS,
(void**)&lpSaveBitmapInfo->lpBitmapBits,
NULL,
0);
lpSaveDCInfo->hOldSel=SelectObject(lpSaveDCInfo->hDC,lpSaveDCInfo->hBmpSel);
}
//----------------------------------------------------------------------
//Description:
//Savethebitmapofsavedctothefile
//--------------------------------------------------------------------
voidCScreenBase::SaveFile(LPSAVEBITMAPINFOlpSaveBitmapInfo,TCHAR*pszPath)
{
intiWidth=lpSaveBitmapInfo->bitMapInfo.bmiHeader.biWidth;
intiHeight=lpSaveBitmapInfo->bitMapInfo.bmiHeader.biHeight;
//Ifyouonlywanttogettheeverypixelcolorvalue,
//youcanbeginhereandthefollowingpartofthisfunctionwillbeunuseful;
//thefollowingpartisinordertowritefile;
//Bimapfileheaderinordertowritebmpfile
BITMAPFILEHEADERbmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER,sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType=0x4d42;//bmp
bmBITMAPFILEHEADER.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize=bmBITMAPFILEHEADER.bfOffBits+((iWidth*iHeight)*3);///norains:3=(24/8)
//writeintofile
FILE*mStream=NULL;
if((mStream=_tfopen(pszPath,TEXT("wb"))))
{
//writebitmapfileheader
fwrite(&bmBITMAPFILEHEADER,sizeof(BITMAPFILEHEADER),1,mStream);
//writebitmapinfo
fwrite(&(lpSaveBitmapInfo->bitMapInfo.bmiHeader),sizeof(BITMAPINFOHEADER),1,mStream);
//writebitmappixelsdata
fwrite(lpSaveBitmapInfo->lpBitmapBits,3*iWidth*iHeight,1,mStream);
//closefile
fclose(mStream);
}
}
//----------------------------------------------------------------------
//Description:
//CopythescreenDCtothedestDC
//--------------------------------------------------------------------
voidCScreenBase::CopyScreenDC(HDC*pHdcDest,constLPSAVEBITMAPINFOlpSaveBitmapInfo,constRECT*prcSrc)
{
//copythescreendctothememorydc
BitBlt(*pHdcDest,0,0,lpSaveBitmapInfo->bitMapInfo.bmiHeader.biWidth,lpSaveBitmapInfo->bitMapInfo.bmiHeader.biHeight,m_hScrDC,prcSrc->left,prcSrc->top,SRCCOPY);
}
//----------------------------------------------------------------------
//Description:
//Initializetheinstance
//--------------------------------------------------------------------
voidCScreenBase::Initialize(const