最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間: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語言中pthread_exit和pehread_join的使用

    C語言中pthread_exit和pehread_join的使用

    pthread_exit用于強(qiáng)制退出一個(gè)線程,pthread_join用于阻塞等待線程退出,獲取線程退出狀態(tài),本文主要介紹了C語言中pthread_exit和pehread_join函數(shù)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法

    C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法

    這篇文章主要介紹了C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Qt實(shí)現(xiàn)小功能之圓形進(jìn)度條的方法詳解

    Qt實(shí)現(xiàn)小功能之圓形進(jìn)度條的方法詳解

    在Qt自帶的控件中,只有垂直進(jìn)度條、水平進(jìn)度條兩種。在平時(shí)做頁面開發(fā)時(shí),有些時(shí)候會用到圓形進(jìn)度條,比如說:下載某個(gè)文件的下載進(jìn)度。本文就來實(shí)現(xiàn)一個(gè)圓形進(jìn)度條,需要的可以參考一下
    2022-10-10
  • 基于Matlab實(shí)現(xiàn)數(shù)字音頻分析處理系統(tǒng)

    基于Matlab實(shí)現(xiàn)數(shù)字音頻分析處理系統(tǒng)

    這篇文章主要為大家介紹了如何利用Matlab制作一個(gè)帶GUI的數(shù)字音頻分析與處理系統(tǒng)。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下
    2022-02-02
  • c語言中scanf的基本用法

    c語言中scanf的基本用法

    這篇文章主要給大家介紹了關(guān)于c語言中scanf的基本用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • c++中new的三種用法詳細(xì)解析

    c++中new的三種用法詳細(xì)解析

    以下的是對c++中new的三種使用方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-09-09
  • C語言內(nèi)存操作函數(shù)詳解

    C語言內(nèi)存操作函數(shù)詳解

    這篇文章主要介紹了C語言全部內(nèi)存操作函數(shù)的實(shí)現(xiàn)詳細(xì)講解,作者用圖文代碼實(shí)例講解的很清晰,有感興趣的同學(xué)可以研究下
    2021-10-10
  • sdl顯示一張bmp圖片示例

    sdl顯示一張bmp圖片示例

    這篇文章主要介紹了sdl顯示一張bmp圖片示例,需要的朋友可以參考下
    2014-04-04
  • C語言分別實(shí)現(xiàn)棧和隊(duì)列詳解流程

    C語言分別實(shí)現(xiàn)棧和隊(duì)列詳解流程

    棧和隊(duì)列,嚴(yán)格意義上來說,也屬于線性表,因?yàn)樗鼈円捕加糜诖鎯壿嬯P(guān)系為 "一對一" 的數(shù)據(jù),但由于它們比較特殊,因此將其單獨(dú)作為一章,做重點(diǎn)講解
    2022-04-04
  • 解析C++編程中的選擇結(jié)構(gòu)和switch語句的用法

    解析C++編程中的選擇結(jié)構(gòu)和switch語句的用法

    這篇文章主要介紹了解析C++編程中的選擇結(jié)構(gòu)和switch語句的用法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09

最新評論

庐江县| 冀州市| 大城县| 壶关县| 汝城县| 松滋市| 宁武县| 禄丰县| 菏泽市| 梨树县| 勐海县| 长乐市| 泽普县| 来安县| 陇西县| 武穴市| 错那县| 延川县| 河西区| 哈巴河县| 彭水| 平安县| 安陆市| 赤水市| 安康市| 武川县| 抚松县| 定兴县| 射洪县| 哈尔滨市| 巴林左旗| 武汉市| 城口县| 文化| 三明市| 海宁市| 七台河市| 宽城| 桂平市| 黑山县| 越西县|