这篇文章给大家分享的是有关C++ 使用PrintWindow实现窗口截图功能的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

本文使用C++双缓存进行指定窗口截图。CreateDIBSection创建应用程序可以直接写入的、与设备无关的位图(DIB),它提供内存中位图的指针,外部程序可以直接使用。
需要注意的是,PrintWindow方法能够抓取使用D3D渲染的窗口(例如Excel、Win10自带视频播放器),如果抓取普通窗口则会附带窗口阴影,可见窗口阴影是Windows使用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_;
};