欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++ 封裝一個(gè)截圖服務(wù)

 更新時(shí)間:2021年01月07日 17:33:03   作者:xhubobo  
這篇文章主要介紹了c++ 封裝一個(gè)截圖服務(wù)的方法,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下

首先是抓圖服務(wù):

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); //注冊抓圖服務(wù)
  bool RegisterCapture(string name, HWND hwnd, CaptureType type = CreateDibSection); //注冊抓圖服務(wù)
  void UnRegisterCapture(string name); //注銷抓圖服務(wù)
  bool IsRegister(string name); //獲取是否已注冊抓圖服務(wù)

  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); //獲取窗口客戶區(qū)尺寸
  bool GetBitmapDataSize(string name, int& bmpDataSize); //獲取抓圖數(shù)據(jù)大小
  bool GetBitmap(string name, HBITMAP& bitmap); //獲取窗口位圖
  bool GetBitmapAddress(string name, void** bitsPtr); //獲取窗口位圖地址

  void Cleanup(); //清理所有抓圖服務(wù)

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;
  }

  //刪除用過的對(duì)象
  ::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++ 封裝一個(gè)截圖服務(wù)的詳細(xì)內(nèi)容,更多關(guān)于c++ 截圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語言實(shí)現(xiàn)黎曼和求定積分

    C語言實(shí)現(xiàn)黎曼和求定積分

    這篇文章主要為大家詳細(xì)介紹了用C語言程序?qū)崿F(xiàn)黎曼和求定積分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • c++中cin/cout與scanf/printf的區(qū)別比較

    c++中cin/cout與scanf/printf的區(qū)別比較

    這篇文章主要介紹了c++中cin/cout與scanf/printf的區(qū)別比較,需要的朋友可以參考下
    2017-06-06
  • QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)

    QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)

    下面小編就為大家?guī)硪黄猀T網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • C++ 基類指針和子類指針相互賦值的實(shí)現(xiàn)方法

    C++ 基類指針和子類指針相互賦值的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄狢++ 基類指針和子類指針相互賦值的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • C++實(shí)現(xiàn)屏幕截圖(全屏截圖)

    C++實(shí)現(xiàn)屏幕截圖(全屏截圖)

    屏幕截圖已經(jīng)成為了所有IM即時(shí)通訊軟件的必備模塊,也是日常辦公中使用最頻繁的功能之一。今天我們從C++開發(fā)的角度,來看看屏幕截圖的主要功能點(diǎn)是如何實(shí)現(xiàn)的,感興趣的可以了解一下
    2021-11-11
  • C++/C 回文字符串的實(shí)例詳解

    C++/C 回文字符串的實(shí)例詳解

    這篇文章主要介紹了C++ 回文字符串的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • C++實(shí)現(xiàn)數(shù)組的排序/插入重新排序/以及逆置操作詳解

    C++實(shí)現(xiàn)數(shù)組的排序/插入重新排序/以及逆置操作詳解

    將新的數(shù)字與已經(jīng)排序好的數(shù)組中的數(shù)字一一比較,直到找到插入點(diǎn),然后將插入點(diǎn)以后的數(shù)字都向后移動(dòng)一個(gè)單位(a[i+1]=a[i]),然后將數(shù)據(jù)插入即可
    2013-10-10
  • C++使用waveIn實(shí)現(xiàn)聲音采集

    C++使用waveIn實(shí)現(xiàn)聲音采集

    在Windows上實(shí)現(xiàn)錄音比較簡單的方法是使用winmm,其中的waveIn模塊就可以打開錄音設(shè)備,這篇文章主要為大家介紹了C++如何使用waveIn實(shí)現(xiàn)聲音采集,需要的可以了解下
    2023-10-10
  • c語言 字符串的拼接和分割實(shí)例

    c語言 字符串的拼接和分割實(shí)例

    今天小編就為大家分享一篇c語言 字符串的拼接和分割實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • VS2019項(xiàng)目打包生成.exe文件與Setup的步驟實(shí)現(xiàn)

    VS2019項(xiàng)目打包生成.exe文件與Setup的步驟實(shí)現(xiàn)

    這篇文章主要介紹了VS2019項(xiàng)目打包生成.exe文件與Setup的步驟實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評(píng)論