C++ USES PrintWindow to achieve the window screenshot function

  • 2020-10-31 21:52:58
  • OfStack

This article USES C++ double cache for the specified window screenshots. CreateDIBSection creates device-independent bitmaps (DIB) that applications can write directly, providing Pointers to in-memory bitmaps that external programs can use directly.

It should be noted that the PrintWindow method can capture the window rendered by D3D (for example, Excel and Win10 come with their own video player). If it captures the normal window, it will be accompanied by a window shadow. It can be seen that the window shadow is rendered by Windows using D3D.

1, PrintCaptureHelper. h


#pragma once
 
#include <windows.h>
#include <string>
using std::string;
 
class PrintCaptureHelper
{
public:
  PrintCaptureHelper();
  virtual ~PrintCaptureHelper();
 
  bool Init(const string& windowName);
  bool Init(HWND hwnd);
  void Cleanup();
  bool RefreshWindow();
  bool ChangeWindowHandle(const string& windowName);
  bool ChangeWindowHandle(HWND hwnd);
  bool Capture() const;
 
  const RECT& GetWindowRect() const { return windowRect_; }
  const RECT& GetClientRect() const { return clientRect_; }
  int GetBitmapDataSize() const { return bmpDataSize_; }
  HBITMAP GetBitmap() const { return bitmap_; }
  void* GetBitmapAddress() const { return bitsPtr_; }
 
private:
  HWND hwnd_;
  HDC scrDc_;
  HDC memDc_;
  HBITMAP bitmap_;
  HBITMAP oldBitmap_;
  void* bitsPtr_;
 
  RECT windowRect_;
  RECT clientRect_;
  int bmpDataSize_;
};

2, PrintCaptureHelper cpp


#include "stdafx.h"
#include "PrintCaptureHelper.h"
 
 
PrintCaptureHelper::PrintCaptureHelper()
  : hwnd_(nullptr)
  , scrDc_(nullptr)
  , memDc_(nullptr)
  , bitmap_(nullptr)
  , oldBitmap_(nullptr)
  , bitsPtr_(nullptr)
  , windowRect_{ 0, 0, 0, 0 }
  , clientRect_{ 0, 0, 0, 0 }
  , bmpDataSize_(0)
{
 
}
 
PrintCaptureHelper::~PrintCaptureHelper()
{
  Cleanup();
}
 
bool PrintCaptureHelper::Init(const string& windowName)
{
  const auto handle = ::FindWindowA(nullptr, windowName.c_str());
  if (handle == nullptr)
  {
    return false;
  }
 
  return Init(handle);
}
 
bool PrintCaptureHelper::Init(HWND hwnd)
{
  hwnd_ = hwnd;
 
  // Get window size 
  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;
 
  // The bitmap information 
  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;
 
  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;
}
 
void PrintCaptureHelper::Cleanup()
{
  if (bitmap_ == nullptr)
  {
    return;
  }
 
  // Delete used objects 
  ::SelectObject(memDc_, oldBitmap_);
  ::DeleteObject(bitmap_);
  ::DeleteDC(memDc_);
  ::ReleaseDC(hwnd_, scrDc_);
 
  hwnd_ = nullptr;
  scrDc_ = nullptr;
  memDc_ = nullptr;
  bitmap_ = nullptr;
  oldBitmap_ = nullptr;
}
 
bool PrintCaptureHelper::RefreshWindow()
{
  const auto hwnd = hwnd_;
  Cleanup();
  return Init(hwnd);
}
 
bool PrintCaptureHelper::ChangeWindowHandle(const string& windowName)
{
  Cleanup();
  return Init(windowName);
}
 
bool PrintCaptureHelper::ChangeWindowHandle(HWND hwnd)
{
  Cleanup();
  return Init(hwnd);
}
 
bool PrintCaptureHelper::Capture() const
{
  if (bitmap_ == nullptr || memDc_ == nullptr || scrDc_ == nullptr)
  {
    return false;
  }
 
  const auto ret = ::PrintWindow(hwnd_, memDc_, PW_CLIENTONLY | PW_RENDERFULLCONTENT);
  return ret != 0;
}

Above is C++ using PrintWindow to achieve the window screenshots of the detailed content, more information about c++ window screenshots, please pay attention to other related articles on this site!


Related articles: