C++使用BitBlt进行窗口抓图的方法

本文使用C++双缓存进行指定窗口截图。CreateDIBSection创建应用程序可以直接写入的、与设备无关的位图(DIB),它提供内存中位图的指针,外部程序可以直接使用。

需要注意的是,BitBlt方法只能抓图普通窗口的截图,对于使用D3D渲染的窗口(例如Excel、Win10自带视频播放器)则只能获取黑屏。

1、DibCaptureHelper.h

#pragma once
 
#include <windows.h>
#include <string>
using std::string;
 
class DibCaptureHelper
{
public:
    DibCaptureHelper();
    virtual ~DibCaptureHelper();
 
    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_;
    POINT bitbltStartPoint_;
    int bmpDataSize_;
};

2、DibCaptureHelper.cpp

#include "stdafx.h"
#include "DibCaptureHelper.h"
 
 
DibCaptureHelper::DibCaptureHelper()
    : hwnd_(nullptr)
    , scrDc_(nullptr)
    , memDc_(nullptr)
    , bitmap_(nullptr)
    , oldBitmap_(nullptr)
    , bitsPtr_(nullptr)
    , windowRect_{ 0, 0, 0, 0 }
    , clientRect_{ 0, 0, 0, 0 }
    , bitbltStartPoint_{ 0,0 }
    , bmpDataSize_(0)
{
}
 
 
DibCaptureHelper::~DibCaptureHelper()
{
    Cleanup();
}
 
bool DibCaptureHelper::Init(const string& windowName)
{
    const auto handle = ::FindWindowA(nullptr, windowName.c_str());
    if (handle == nullptr)
    {
        return false;
    }
 
    return Init(handle);
}
 
bool DibCaptureHelper::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;
 
    bitbltStartPoint_.x = 0;
    bitbltStartPoint_.y = 0;
 
    //位图信息
    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_); //获取窗口DC
    memDc_ = ::CreateCompatibleDC(scrDc_); //缓冲内存DC
    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;
}
 
void DibCaptureHelper::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 DibCaptureHelper::RefreshWindow()
{
    const auto hwnd = hwnd_;
    Cleanup();
    return Init(hwnd);
}
 
bool DibCaptureHelper::ChangeWindowHandle(const string& windowName)
{
    Cleanup();
    return Init(windowName);
}
 
bool DibCaptureHelper::ChangeWindowHandle(HWND hwnd)
{
    Cleanup();
    return Init(hwnd);
}
 
bool DibCaptureHelper::Capture() const
{
    if (bitmap_ == nullptr || memDc_ == nullptr || scrDc_ == nullptr)
    {
        return false;
    }
 
    const auto clientRectWidth = clientRect_.right - clientRect_.left;
    const auto clientRectHeight = clientRect_.bottom - clientRect_.top;
 
    const auto ret = ::BitBlt(
        memDc_, 0, 0, clientRectWidth, clientRectHeight,
        scrDc_, bitbltStartPoint_.x, bitbltStartPoint_.y,
        SRCCOPY);
    return ret != 0;
}

以上就是C++使用BitBlt进行窗口抓图的方法的详细内容,更多关于c++ 窗口抓图的资料请关注我们其它相关文章!

(0)

相关推荐

  • C++ 使用PrintWindow实现窗口截图功能

    本文使用C++双缓存进行指定窗口截图.CreateDIBSection创建应用程序可以直接写入的.与设备无关的位图(DIB),它提供内存中位图的指针,外部程序可以直接使用. 需要注意的是,PrintWindow方法能够抓取使用D3D渲染的窗口(例如Excel.Win10自带视频播放器),如果抓取普通窗口则会附带窗口阴影,可见窗口阴影是Windows使用D3D渲染出来的. 1.PrintCaptureHelper.h #pragma once #include <windows.h> #incl

  • C++实现屏幕截图

    上回分享了一个全屏截图的代码,保存为BMP,参考:C++实现屏幕截图(全屏截图) 实际使用的过程中我发现截图文件实在大,无奈又整成了PNG截图,现在分享出来. MakePNG.h //MakePNG.h #pragma once #include <GdiPlus.h> using namespace Gdiplus; #pragma comment(lib,"GdiPlus.lib") class CMakePNG { public: CMakePNG(void); ~C

  • C++实现屏幕截图功能

    本文实例为大家分享了C++实现全屏截图功能的具体代码,供大家参考,具体内容如下 最近维护的项目,在某些情况下,光有日志还不行,于是添加了截图功能,特定情况下,会自动截图,辅助分析,从而改进程序.以下是截图实现代码. void CDemoDlg::ScreenShot(void) { CWnd *pDesktop = GetDesktopWindow(); CDC *pdeskdc = pDesktop->GetDC(); CRect re; //获取窗口的大小 pDesktop->GetCli

  • VC++基于Dx实现的截图程序示例代码

    本文所述的程序示例为VC++图象特效的截图示例,需要DirectX 3.0以上版,代码中的GetScreen函数是本截图程序的关键.运行这个程序可用Esc键结束.代码中需要ddutil.h与ddutil.cpp文件,请自行下载添加.关于InitDDraw()函数,功能是初始化DirectDraw环境,创建换页链(主页面,一个后台缓冲区),以及创建一个定时器. 具体的功能代码如下: #include <windows.h> #include <windowsx.h> #include

  • C++中BitBlt的使用方法详解

    BitBlt 该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境. 原型: BOOL BitBlt( HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop): 参数: hdcDest:指向目标设备环境的句柄. nXDest.nYDest:指定目标矩形区域左上角的X轴和Y轴逻辑坐标 nWidth.

  • C++使用BitBlt进行窗口抓图的方法

    本文使用C++双缓存进行指定窗口截图.CreateDIBSection创建应用程序可以直接写入的.与设备无关的位图(DIB),它提供内存中位图的指针,外部程序可以直接使用. 需要注意的是,BitBlt方法只能抓图普通窗口的截图,对于使用D3D渲染的窗口(例如Excel.Win10自带视频播放器)则只能获取黑屏. 1.DibCaptureHelper.h #pragma once   #include <windows.h> #include <string> using std::

  • C# 使用BitBlt进行窗口抓图的示例

    本文和C++使用BitBlt进行窗口抓图对应,使用C#实现. 这种方式对1920*1080大小的窗口,一次抓图的时间参考(VS2015+i5 9400F):低至2~3ms(平均4.3ms). 参见:C#抓图服务. 1.Win32封装 Win32Consts using System.ComponentModel; namespace CaptureSharp { public sealed class Win32Consts { public enum DibColorMode : uint {

  • fckeditor粘贴Word时弹出窗口取消的方法

    本文实例讲述了fckeditor粘贴Word时弹出窗口取消的方法.分享给大家供大家参考.具体方法如下: 用fckeditor作为用户发布的编辑框,允许用户发布Word.默认的情况下,粘取进word时,提示是否清除word样式,选择"是"则弹出一个框,需要再粘贴一次来清除word样式.这个操作很麻烦,因此要取消. 开始找到的方法是把ckeditor/" target="_blank">fckeditoreditorjs下的fckeditorcode_i

  • JS获取鼠标位置距浏览器窗口距离的方法示例

    本文实例讲述了JS获取鼠标位置距浏览器窗口距离的方法.分享给大家供大家参考,具体如下: 先来看看运行效果图: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> #test_div { width:400px; height: 400px;

  • JavaScript实现提交模式窗口后刷新父窗口数据的方法

    本文实例讲述了JavaScript实现提交模式窗口后刷新父窗口数据的方法.分享给大家供大家参考,具体如下: 有些时间,按需求设计,一个窗口中,点击按扭,弹出模式窗口,在模式窗口中,添加完数据后(提交),关闭模式窗口,并且刷新父窗口,而恰恰模式窗口并不支持window.opener,所以无法获得父窗口,也就是说无法在关闭模式窗口后,来得到父窗口,但是可以借助showModealDialog的返回值来判断是否刷新,当调用showModealDialog时,父窗口代码会停在这一行,当弹出的模式窗口关闭

  • python在windows下创建隐藏窗口子进程的方法

    本文实例讲述了python在windows下创建隐藏窗口子进程的方法.分享给大家供大家参考.具体实现方法如下: import subprocess IS_WIN32 = 'win32' in str(sys.platform).lower() def subprocess_call(*args, **kwargs): #also works for Popen. #It creates a new *hidden* window, #so it will work in frozen apps

  • C# Winform中实现主窗口打开登录窗口关闭的方法

    在使用C#进行Winform编程时,我们经常需要使用一个登录框来进行登录,一旦输入的用户名密码登录成功,这时登录窗口应该关闭,而且同时打开主程序窗口.该如何来实现呢? 乍一想,很简单啊,打开主窗口就用主窗口的Show()方法,而关闭登录窗口就用登录窗口的Close()方法即可.即代码如下: Program.cs中代码: 复制代码 代码如下: Application.Run(new FormLogin()); 登录窗口(FormLogin)代码: 复制代码 代码如下: private void b

  • jquery简单实现外部链接用新窗口打开的方法

    本文实例讲述了jquery实现外部链接用新窗口打开的方法.分享给大家供大家参考.具体实现方法如下: $("a[href^='http://']").click(function(){ this.target = "_blank"; }); 或者使用下面这个 $(function(){ $("a[href^='http://']").attr({'target':'_blank'}) }) 希望本文所述对大家的jQuery程序设计有所帮助.

  • android自由改变Dialog窗口位置的方法

    本文实例讲述了android自由改变Dialog窗口位置的方法.分享给大家供大家参考.具体如下: Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.dialog_layout); dialog.setTitle("Custom Dialog"); //获得当前窗体 Window window = dialog.getWindow(); //重新设置 WindowManager.LayoutParams lp

  • C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法

    本文实例讲述了C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法.分享给大家供大家参考,具体如下: Windows API Windows 这个多作业系统除了协调应用程序的执行.分配内存.管理资源-之外, 它同时也是一个很大的服务中心,调用这个服务中心的各种服务(每一种服务就是一个函数),可以帮应用程式达到开启视窗.描绘图形.使用周边设备等目的,由于这些函数服务的对象是应用程序(Application), 所以便称之为 Application Programmin

随机推荐