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

C++ 使用PrintWindow實(shí)現(xiàn)窗口截圖功能

 更新時(shí)間:2020年08月04日 14:26:48   作者:xhubobo  
這篇文章主要介紹了C++ 如何使用PrintWindow實(shí)現(xiàn)窗口截圖功能,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

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

需要注意的是,PrintWindow方法能夠抓取使用D3D渲染的窗口(例如Excel、Win10自帶視頻播放器),如果抓取普通窗口則會(huì)附帶窗口陰影,可見窗口陰影是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_;
};

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;
 
  //獲取窗口大小
  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;
 
  //位圖信息
  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;
  }
 
  //刪除用過的對(duì)象
  ::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;
}

以上就是C++ 使用PrintWindow實(shí)現(xiàn)窗口截圖功能的詳細(xì)內(nèi)容,更多關(guān)于c++ 窗口截圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • c語言實(shí)現(xiàn)足球比賽積分統(tǒng)計(jì)系統(tǒng)

    c語言實(shí)現(xiàn)足球比賽積分統(tǒng)計(jì)系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了c語言實(shí)現(xiàn)足球比賽積分統(tǒng)計(jì)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C語言復(fù)數(shù)的加減及輸出結(jié)構(gòu)體

    C語言復(fù)數(shù)的加減及輸出結(jié)構(gòu)體

    大家好,本篇文章主要講的是C語言復(fù)數(shù)的加減及輸出結(jié)構(gòu)體,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • 用C語言判斷字符是否為空白字符或特殊字符的方法

    用C語言判斷字符是否為空白字符或特殊字符的方法

    這篇文章主要介紹了用C語言判斷字符是否為空白字符或特殊字符的方法,分別為isspace()函數(shù)的使用和ispunct()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08
  • C語言中的隱式函數(shù)聲明

    C語言中的隱式函數(shù)聲明

    在c語言里面開來還是要學(xué)習(xí)c++的編程習(xí)慣,使用函數(shù)之前一定要聲明。不然,即使編譯能通過,運(yùn)行時(shí)也可能會(huì)出一些莫名其妙的問題。
    2016-01-01
  • C語言實(shí)現(xiàn)查看進(jìn)程是否存在的方法示例

    C語言實(shí)現(xiàn)查看進(jìn)程是否存在的方法示例

    這篇文章主要介紹了C語言實(shí)現(xiàn)查看進(jìn)程是否存在的方法,涉及C語言針對(duì)進(jìn)程操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • C語言線性表中順序表超詳細(xì)理解

    C語言線性表中順序表超詳細(xì)理解

    程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示
    2022-03-03
  • C++的原生數(shù)組你了解多少

    C++的原生數(shù)組你了解多少

    這篇文章主要為大家詳細(xì)介紹了C++的原生數(shù)組,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C++ vector容器 find erase的使用操作:查找并刪除指定元素

    C++ vector容器 find erase的使用操作:查找并刪除指定元素

    這篇文章主要介紹了C++ vector容器 find erase的使用操作:查找并刪除指定元素,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • QT實(shí)現(xiàn)視頻傳輸功能

    QT實(shí)現(xiàn)視頻傳輸功能

    這篇文章主要為大家詳細(xì)介紹了QT實(shí)現(xiàn)視頻傳輸功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C語言qsort函數(shù)用冒泡排序?qū)崿F(xiàn)過程詳解

    C語言qsort函數(shù)用冒泡排序?qū)崿F(xiàn)過程詳解

    qsort函數(shù)是由C語言提供的標(biāo)準(zhǔn)庫函數(shù), 它的實(shí)現(xiàn)思想是快速排序。這篇文章主要介紹了C語言中qsort函數(shù)用法及用冒泡排序?qū)崿F(xiàn)qsort函數(shù)功能,需要的可以參考一下
    2023-02-02

最新評(píng)論

修文县| 商都县| 柳林县| 红原县| 长顺县| 汤阴县| 涟水县| 灵寿县| 漳州市| 昌江| 邛崃市| 体育| 贵溪市| 柳州市| 延川县| 同德县| 高碑店市| 垣曲县| 本溪市| 福清市| 鹿邑县| 聊城市| 贵定县| 绥宁县| 桐柏县| 闽侯县| 杭锦旗| 泾阳县| 莱阳市| 奉化市| 芮城县| 大理市| 曲阜市| 禹城市| 宁德市| 汝城县| 清流县| 明光市| 张北县| 仁布县| 青冈县|