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

C++使用BitBlt進(jìn)行窗口抓圖的方法

 更新時間:2021年01月07日 09:20:06   作者:xhubobo  
這篇文章主要介紹了C++使用BitBlt進(jìn)行窗口抓圖的方法,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下

本文使用C++雙緩存進(jìn)行指定窗口截圖。CreateDIBSection創(chuàng)建應(yīng)用程序可以直接寫入的、與設(shè)備無關(guān)的位圖(DIB),它提供內(nèi)存中位圖的指針,外部程序可以直接使用。

需要注意的是,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_); //緩沖內(nèi)存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進(jìn)行窗口抓圖的方法的詳細(xì)內(nèi)容,更多關(guān)于c++ 窗口抓圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C++ deque/queue/stack的底層原理解析

    C++ deque/queue/stack的底層原理解析

    這篇文章主要介紹了C++ deque/queue/stack的底層原理解析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • C++基于EasyX框架實現(xiàn)飛機(jī)大戰(zhàn)小游戲

    C++基于EasyX框架實現(xiàn)飛機(jī)大戰(zhàn)小游戲

    EasyX是針對C/C++的圖形庫,可以幫助使用C/C++語言的程序員快速上手圖形和游戲編程。本文將利用EasyX框架實現(xiàn)飛機(jī)大戰(zhàn)小游戲,需要的可以參考一下
    2023-01-01
  • C語言實現(xiàn)簡單學(xué)生成績管理系統(tǒng)項目

    C語言實現(xiàn)簡單學(xué)生成績管理系統(tǒng)項目

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)簡單學(xué)生成績管理系統(tǒng)項目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C語言MFC基礎(chǔ)之計算器詳解

    C語言MFC基礎(chǔ)之計算器詳解

    這篇文章主要為大家介紹了MFC實現(xiàn)簡單的計算器,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-08-08
  • C語言中實現(xiàn)協(xié)程案例

    C語言中實現(xiàn)協(xié)程案例

    這篇文章主要介紹了C語言中實現(xiàn)協(xié)程案例,本文通過將協(xié)程與線程和異步回調(diào)進(jìn)行對比,以及具體實現(xiàn)案例,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • c++文件監(jiān)控之FileSystemWatcher

    c++文件監(jiān)控之FileSystemWatcher

    為了監(jiān)控web程序的靜態(tài)文件是否被惡意改動,所以學(xué)習(xí)了一下FileSystemWatcher 類對文件的監(jiān)控,由于還在初級階段,這里只貼一下關(guān)于FileSystemWatcher學(xué)習(xí)的一些代碼
    2019-04-04
  • 一起聊聊C++中的智能指針

    一起聊聊C++中的智能指針

    C++?是手工管理內(nèi)存的分配和釋放,這給了程序員極大的自由度也給了我們極高的門檻,弄不好就得內(nèi)存泄露。使用智能指針能更好的管理堆內(nèi)存,本文主要給大家介紹一下c++的智能指針,需要的朋友可以參考下
    2022-07-07
  • 剖析C++中的常量表達(dá)式與省略號的相關(guān)作用

    剖析C++中的常量表達(dá)式與省略號的相關(guān)作用

    這篇文章主要介紹了C++中的常量表達(dá)式與省略號的相關(guān)作用,以及表達(dá)式中的可變參數(shù)模板示例,需要的朋友可以參考下
    2016-01-01
  • C/C++中宏/Macro的深入講解

    C/C++中宏/Macro的深入講解

    這篇文章主要給大家介紹了關(guān)于C/C++中宏/Macro的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C/C++具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 關(guān)于C語言 文件讀寫 feof 函數(shù)

    關(guān)于C語言 文件讀寫 feof 函數(shù)

    這篇文章主要給大家分享的是關(guān)于C語言文件讀寫 feof 函數(shù) ,feof 是 C 語言標(biāo)準(zhǔn)庫函數(shù),其功能是檢測文件結(jié)束符,如果文件結(jié)束,則返回非 0 值,否則返回 0,感興趣的小伙伴請跟小編一起來看看下面文章的內(nèi)容吧
    2021-10-10

最新評論