c++ 封装一个截图服务
首先是抓图服务:
ICaptureHelper.h
#pragmaonce #include#include usingstd::string; classICaptureHelper { public: virtual~ICaptureHelper(){} virtualboolInit(conststring&windowName)=0; virtualboolInit(HWNDhwnd)=0; virtualvoidCleanup()=0; virtualboolRefreshWindow()=0; virtualboolChangeWindowHandle(conststring&windowName)=0; virtualboolChangeWindowHandle(HWNDhwnd)=0; virtualboolCapture()=0; virtualconstRECT&GetWindowRect()const=0; virtualconstRECT&GetClientRect()const=0; virtualintGetBitmapDataSize()const=0; virtualHBITMAPGetBitmap()const=0; virtualvoid*GetBitmapAddress()const=0; };
CaptureService.h
#pragmaonce #include"ICaptureHelper.h" #include
其次是抓图代码封装:
AbsCaptureHelper.h
#pragmaonce
#include"ICaptureHelper.h"
classAbsCaptureHelper:publicICaptureHelper
{
public:
AbsCaptureHelper();
virtual~AbsCaptureHelper();
boolInit(conststring&windowName)override;
boolInit(HWNDhwnd)override;
voidCleanup()override;
boolRefreshWindow()override;
boolChangeWindowHandle(conststring&windowName)override;
boolChangeWindowHandle(HWNDhwnd)override;
boolCapture()override;
constRECT&GetWindowRect()constoverride{returnwindowRect_;}
constRECT&GetClientRect()constoverride{returnclientRect_;}
intGetBitmapDataSize()constoverride{returnbmpDataSize_;}
HBITMAPGetBitmap()constoverride{returnbitmap_;}
void*GetBitmapAddress()constoverride{returnbitsPtr_;}
protected:
virtualboolInitDC(constBITMAPINFO&bitmapInfo)=0;
virtualboolDoCapture()=0;
protected:
HWNDhwnd_;
HDCscrDc_;
HDCmemDc_;
HBITMAPbitmap_;
HBITMAPoldBitmap_;
void*bitsPtr_;
RECTwindowRect_;
RECTclientRect_;
intbmpDataSize_;
};
AbsCaptureHelper.cpp
#include"stdafx.h"
#include"AbsCaptureHelper.h"
AbsCaptureHelper::AbsCaptureHelper()
:hwnd_(nullptr)
,scrDc_(nullptr)
,memDc_(nullptr)
,bitmap_(nullptr)
,oldBitmap_(nullptr)
,bitsPtr_(nullptr)
,windowRect_{0,0,0,0}
,clientRect_{0,0,0,0}
,bmpDataSize_(0)
{
}
AbsCaptureHelper::~AbsCaptureHelper()
{
AbsCaptureHelper::Cleanup();
}
boolAbsCaptureHelper::Init(conststring&windowName)
{
constautohandle=::FindWindowA(nullptr,windowName.c_str());
if(handle==nullptr)
{
returnfalse;
}
returnInit(handle);
}
boolAbsCaptureHelper::Init(HWNDhwnd)
{
hwnd_=hwnd;
//获取窗口大小
if(!::GetWindowRect(hwnd_,&windowRect_)||!::GetClientRect(hwnd_,&clientRect_))
{
returnfalse;
}
constautoclientRectWidth=clientRect_.right-clientRect_.left;
constautoclientRectHeight=clientRect_.bottom-clientRect_.top;
bmpDataSize_=clientRectWidth*clientRectHeight*4;
//位图信息
BITMAPINFObitmapInfo;
bitmapInfo.bmiHeader.biSize=sizeof(bitmapInfo);
bitmapInfo.bmiHeader.biWidth=clientRectWidth;
bitmapInfo.bmiHeader.biHeight=clientRectHeight;
bitmapInfo.bmiHeader.biPlanes=1;
bitmapInfo.bmiHeader.biBitCount=32;
bitmapInfo.bmiHeader.biSizeImage=clientRectWidth*clientRectHeight;
bitmapInfo.bmiHeader.biCompression=BI_RGB;
returnInitDC(bitmapInfo);
}
voidAbsCaptureHelper::Cleanup()
{
if(bitmap_==nullptr)
{
return;
}
//删除用过的对象
::SelectObject(memDc_,oldBitmap_);
::DeleteObject(bitmap_);
::DeleteDC(memDc_);
::ReleaseDC(hwnd_,scrDc_);
hwnd_=nullptr;
scrDc_=nullptr;
memDc_=nullptr;
bitmap_=nullptr;
oldBitmap_=nullptr;
bitsPtr_=nullptr;
}
boolAbsCaptureHelper::RefreshWindow()
{
constautohwnd=hwnd_;
Cleanup();
returnInit(hwnd);
}
boolAbsCaptureHelper::ChangeWindowHandle(conststring&windowName)
{
Cleanup();
returnInit(windowName);
}
boolAbsCaptureHelper::ChangeWindowHandle(HWNDhwnd)
{
Cleanup();
returnInit(hwnd);
}
boolAbsCaptureHelper::Capture()
{
if(bitmap_==nullptr||memDc_==nullptr||scrDc_==nullptr)
{
returnfalse;
}
returnDoCapture();
}
DibCaptureHelper.h
#pragmaonce
#include"AbsCaptureHelper.h"
classDibCaptureHelper:publicAbsCaptureHelper
{
public:
DibCaptureHelper();
virtual~DibCaptureHelper();
protected:
boolInitDC(constBITMAPINFO&bitmapInfo)override;
boolDoCapture()override;
private:
boolsaveBitmap_;
intmockPageNumber;
intbmpCount_;
};
DibCaptureHelper.cpp
#include"stdafx.h" #include"DibCaptureHelper.h" #includestaticintBmpCount=0; staticintBmpMaxCount=50; DibCaptureHelper::DibCaptureHelper() :saveBitmap_(false) ,mockPageNumber(++BmpCount) ,bmpCount_(0) { } DibCaptureHelper::~DibCaptureHelper() { } boolDibCaptureHelper::InitDC(constBITMAPINFO&bitmapInfo) { scrDc_=::GetWindowDC(hwnd_); memDc_=::CreateCompatibleDC(scrDc_); bitmap_=::CreateDIBSection(memDc_,&bitmapInfo,DIB_RGB_COLORS,&bitsPtr_,nullptr,0); if(bitmap_==nullptr) { ::DeleteDC(memDc_); ::ReleaseDC(hwnd_,scrDc_); returnfalse; } oldBitmap_=static_cast (::SelectObject(memDc_,bitmap_)); returntrue; } boolDibCaptureHelper::DoCapture() { constautoclientRectWidth=clientRect_.right-clientRect_.left; constautoclientRectHeight=clientRect_.bottom-clientRect_.top; constautoret=::BitBlt( memDc_,0,0,clientRectWidth,clientRectHeight, scrDc_,0,0,SRCCOPY); returnret!=0; }
PrintCaptureHelper.h
#pragmaonce
#include"AbsCaptureHelper.h"
classPrintCaptureHelper:publicAbsCaptureHelper
{
public:
PrintCaptureHelper();
virtual~PrintCaptureHelper();
protected:
boolInitDC(constBITMAPINFO&bitmapInfo)override;
boolDoCapture()override;
};
PrintCaptureHelper.cpp
#include"stdafx.h"
#include"PrintCaptureHelper.h"
PrintCaptureHelper::PrintCaptureHelper()
{
}
PrintCaptureHelper::~PrintCaptureHelper()
{
}
boolPrintCaptureHelper::InitDC(constBITMAPINFO&bitmapInfo)
{
scrDc_=::GetWindowDC(hwnd_);
memDc_=::CreateCompatibleDC(scrDc_);
bitmap_=::CreateDIBSection(scrDc_,&bitmapInfo,DIB_RGB_COLORS,&bitsPtr_,nullptr,0);
if(bitmap_==nullptr)
{
::DeleteDC(memDc_);
::ReleaseDC(hwnd_,scrDc_);
returnfalse;
}
oldBitmap_=static_cast(::SelectObject(memDc_,bitmap_));
returntrue;
}
boolPrintCaptureHelper::DoCapture()
{
constautoret=::PrintWindow(hwnd_,memDc_,PW_CLIENTONLY|PW_RENDERFULLCONTENT);
returnret!=0;
}
以上就是c++封装一个截图服务的详细内容,更多关于c++截图的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。