c++캡 처 서비스 패키지

우선 캡 처 서비스:
ICaptureHelper.h 

#pragma once

#include <windows.h>
#include <string>
using std::string;

class ICaptureHelper
{
public:
  virtual ~ICaptureHelper() {}
  virtual bool Init(const string& windowName) = 0;
  virtual bool Init(HWND hwnd) = 0;
  virtual void Cleanup() = 0;
  virtual bool RefreshWindow() = 0;
  virtual bool ChangeWindowHandle(const string& windowName) = 0;
  virtual bool ChangeWindowHandle(HWND hwnd) = 0;
  virtual bool Capture() = 0;

  virtual const RECT& GetWindowRect() const = 0;
  virtual const RECT& GetClientRect() const = 0;
  virtual int GetBitmapDataSize() const = 0;
  virtual HBITMAP GetBitmap() const = 0;
  virtual void* GetBitmapAddress() const = 0;
};
CaptureService.h

#pragma once

#include "ICaptureHelper.h"
#include <map>
using std::map;

class CaptureService
{
public:
  CaptureService() = default;
  static CaptureService& GetInstance();

  enum CaptureType
  {
    //  CreateDIBSection  ,   ,      D3D      
    CreateDibSection = 0,

    //  PrintWindow  ,   (16ms  ),      D3D      
    PrintWindow
  };

  bool RegisterCapture(string name, string windowName, CaptureType type = CreateDibSection); //      
  bool RegisterCapture(string name, HWND hwnd, CaptureType type = CreateDibSection); //      
  void UnRegisterCapture(string name); //      
  bool IsRegister(string name); //           

  bool RefreshWindow(string name); //    
  bool ChangeWindowHandle(string name, string windowName); //      
  bool ChangeWindowHandle(string name, HWND hwnd); //      
  bool Capture(string name); //  

  bool GetWindowRect(string name, RECT& winRect); //      
  bool GetClientRect(string name, RECT& clientRect); //         
  bool GetBitmapDataSize(string name, int& bmpDataSize); //        
  bool GetBitmap(string name, HBITMAP& bitmap); //      
  bool GetBitmapAddress(string name, void** bitsPtr); //        

  void Cleanup(); //        

private:
  ~CaptureService();

private:
  map<string, ICaptureHelper*> captureHelpers_;
};
그 다음은 캡 처 코드 패키지 입 니 다.
AbsCaptureHelper.h

#pragma once

#include "ICaptureHelper.h"

class AbsCaptureHelper : public ICaptureHelper
{
public:
  AbsCaptureHelper();
  virtual ~AbsCaptureHelper();

  bool Init(const string& windowName) override;
  bool Init(HWND hwnd) override;
  void Cleanup() override;
  bool RefreshWindow() override;
  bool ChangeWindowHandle(const string& windowName) override;
  bool ChangeWindowHandle(HWND hwnd) override;
  bool Capture() override;

  const RECT& GetWindowRect() const override { return windowRect_; }
  const RECT& GetClientRect() const override { return clientRect_; }
  int GetBitmapDataSize() const override { return bmpDataSize_; }
  HBITMAP GetBitmap() const override { return bitmap_; }
  void* GetBitmapAddress() const override { return bitsPtr_; }

protected:
  virtual bool InitDC(const BITMAPINFO& bitmapInfo) = 0;
  virtual bool DoCapture() = 0;

protected:
  HWND hwnd_;
  HDC scrDc_;
  HDC memDc_;
  HBITMAP bitmap_;
  HBITMAP oldBitmap_;
  void* bitsPtr_;

  RECT windowRect_;
  RECT clientRect_;
  int bmpDataSize_;
};
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();
}

bool AbsCaptureHelper::Init(const string& windowName)
{
  const auto handle = ::FindWindowA(nullptr, windowName.c_str());
  if (handle == nullptr)
  {
    return false;
  }

  return Init(handle);
}

bool AbsCaptureHelper::Init(HWND hwnd)
{
  hwnd_ = hwnd;

  //      
  if (!::GetWindowRect(hwnd_, &windowRect_) || !::GetClientRect(hwnd_, &clientRect_))
  {
    return false;
  }

  const auto clientRectWidth = clientRect_.right - clientRect_.left;
  const auto clientRectHeight = clientRect_.bottom - clientRect_.top;
  bmpDataSize_ = clientRectWidth * clientRectHeight * 4;

  //    
  BITMAPINFO bitmapInfo;
  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;

  return InitDC(bitmapInfo);
}

void AbsCaptureHelper::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;
}

bool AbsCaptureHelper::RefreshWindow()
{
  const auto hwnd = hwnd_;
  Cleanup();
  return Init(hwnd);
}

bool AbsCaptureHelper::ChangeWindowHandle(const string& windowName)
{
  Cleanup();
  return Init(windowName);
}

bool AbsCaptureHelper::ChangeWindowHandle(HWND hwnd)
{
  Cleanup();
  return Init(hwnd);
}

bool AbsCaptureHelper::Capture()
{
  if (bitmap_ == nullptr || memDc_ == nullptr || scrDc_ == nullptr)
  {
    return false;
  }

  return DoCapture();
}
DibCaptureHelper.h

#pragma once

#include "AbsCaptureHelper.h"

class DibCaptureHelper : public AbsCaptureHelper
{
public:
  DibCaptureHelper();
  virtual ~DibCaptureHelper();

protected:
  bool InitDC(const BITMAPINFO& bitmapInfo) override;
  bool DoCapture() override;

private:
  bool saveBitmap_;
  int mockPageNumber;
  int bmpCount_;
};
DibCaptureHelper.cpp

#include "stdafx.h"
#include "DibCaptureHelper.h"
#include <sstream>

static int BmpCount = 0;
static int BmpMaxCount = 50;

DibCaptureHelper::DibCaptureHelper()
  : saveBitmap_(false)
  , mockPageNumber(++BmpCount)
  , bmpCount_(0)
{
}

DibCaptureHelper::~DibCaptureHelper()
{
}

bool DibCaptureHelper::InitDC(const BITMAPINFO& 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_);
    return false;
  }

  oldBitmap_ = static_cast<HBITMAP>(::SelectObject(memDc_, bitmap_));
  return true;
}

bool DibCaptureHelper::DoCapture()
{
  const auto clientRectWidth = clientRect_.right - clientRect_.left;
  const auto clientRectHeight = clientRect_.bottom - clientRect_.top;

  const auto ret = ::BitBlt(
    memDc_, 0, 0, clientRectWidth, clientRectHeight,
    scrDc_, 0, 0, SRCCOPY);

  return ret != 0;
}
PrintCaptureHelper.h

#pragma once

#include "AbsCaptureHelper.h"

class PrintCaptureHelper : public AbsCaptureHelper
{
public:
  PrintCaptureHelper();
  virtual ~PrintCaptureHelper();

protected:
  bool InitDC(const BITMAPINFO& bitmapInfo) override;
  bool DoCapture() override;
};
PrintCaptureHelper.cpp

#include "stdafx.h"
#include "PrintCaptureHelper.h"


PrintCaptureHelper::PrintCaptureHelper()
{
}

PrintCaptureHelper::~PrintCaptureHelper()
{
}

bool PrintCaptureHelper::InitDC(const BITMAPINFO& 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_);
    return false;
  }
  
  oldBitmap_ = static_cast<HBITMAP>(::SelectObject(memDc_, bitmap_));
  return true;
}

bool PrintCaptureHelper::DoCapture()
{
  const auto ret = ::PrintWindow(hwnd_, memDc_, PW_CLIENTONLY | PW_RENDERFULLCONTENT);
  return ret != 0;
}
이상 은 c++캡 처 서 비 스 를 패키지 하 는 상세 한 내용 입 니 다.c+캡 처 에 관 한 자 료 는 저희 의 다른 관련 글 을 주목 하 세 요!

좋은 웹페이지 즐겨찾기