当前位置:   article > 正文

windows抓屏排除指定窗口_magsetwindowfilterlist

magsetwindowfilterlist

方式一:

  1. BOOL SetWindowDisplayAffinity(
  2. HWND hWnd,
  3. DWORD dwAffinity
  4. );

hWnd:窗口句柄

dwAffinity:

  1. #define WDA_NONE 0x00000000
  2. #define WDA_MONITOR 0x00000001
  3. #define WDA_EXCLUDEFROMCAPTURE 0x00000011

在beta版本下,才有WDA_EXCLUDEFROMCAPTURE

包含在:C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\WinUser.h

然鹅,经过测试,并没有用,被排除的窗口,只是显示成了黑框。失败!

 

方式二(从webrtc M81抠的):

Windows放大镜Magnification API

  1. MagSetWindowFilterList
  2. Sets the list of windows to be magnified or the list of windows to be excluded from magnification.
  3. BOOL MagSetWindowFilterList(
  4. HWND hwnd,
  5. DWORD dwFilterMode,
  6. int count,
  7. HWND *pHWND
  8. );

接口MagSetWindowFilterList具有排除指定窗口的功能。

 

测试可行,只是稍显复杂:

  1. //excludewindow.h
  2. #pragma once
  3. #include <magnification.h>
  4. #include <wincodec.h>
  5. #include <windows.h>
  6. // kMagnifierWindowClass has to be "Magnifier" according to the Magnification
  7. // API. The other strings can be anything.
  8. static wchar_t kMagnifierHostClass[] = L"ScreenCapturerWinMagnifierHost";
  9. static wchar_t kHostWindowName[] = L"MagnifierHost";
  10. static wchar_t kMagnifierWindowClass[] = L"Magnifier";
  11. static wchar_t kMagnifierWindowName[] = L"MagnifierWindow";
  12. class CExcludeWindow
  13. {
  14. public:
  15. CExcludeWindow();
  16. virtual ~CExcludeWindow();
  17. int SetExcludeWnd(HWND hWnd);
  18. bool Init();
  19. bool CaptureFrame();
  20. private:
  21. typedef BOOL(WINAPI* MagImageScalingCallback)(HWND hwnd,
  22. void* srcdata,
  23. MAGIMAGEHEADER srcheader,
  24. void* destdata,
  25. MAGIMAGEHEADER destheader,
  26. RECT unclipped,
  27. RECT clipped,
  28. HRGN dirty);
  29. typedef BOOL(WINAPI* MagInitializeFunc)(void);
  30. typedef BOOL(WINAPI* MagUninitializeFunc)(void);
  31. typedef BOOL(WINAPI* MagSetWindowSourceFunc)(HWND hwnd, RECT rect);
  32. typedef BOOL(WINAPI* MagSetWindowFilterListFunc)(HWND hwnd,
  33. DWORD dwFilterMode,
  34. int count,
  35. HWND* pHWND);
  36. typedef BOOL(WINAPI* MagSetImageScalingCallbackFunc)(
  37. HWND hwnd,
  38. MagImageScalingCallback callback);
  39. static BOOL WINAPI OnMagImageScalingCallback(HWND hwnd,
  40. void* srcdata,
  41. MAGIMAGEHEADER srcheader,
  42. void* destdata,
  43. MAGIMAGEHEADER destheader,
  44. RECT unclipped,
  45. RECT clipped,
  46. HRGN dirty);
  47. void OnCaptured(void* data, const MAGIMAGEHEADER& header);
  48. HMODULE mag_lib_handle_ = NULL;
  49. MagInitializeFunc mag_initialize_func_ = nullptr;
  50. MagUninitializeFunc mag_uninitialize_func_ = nullptr;
  51. MagSetWindowSourceFunc set_window_source_func_ = nullptr;
  52. MagSetWindowFilterListFunc set_window_filter_list_func_ = nullptr;
  53. MagSetImageScalingCallbackFunc set_image_scaling_callback_func_ = nullptr;
  54. // The hidden window hosting the magnifier control.
  55. HWND host_window_ = NULL;
  56. // The magnifier control that captures the screen.
  57. HWND magnifier_window_ = NULL;
  58. // True if the magnifier control has been successfully initialized.
  59. bool magnifier_initialized_ = false;
  60. // True if the last OnMagImageScalingCallback was called and handled
  61. // successfully. Reset at the beginning of each CaptureImage call.
  62. bool magnifier_capture_succeeded_ = true;
  63. };

 

  1. //excludewindow.cpp
  2. #include "excludewindow.h"
  3. #include <iostream>
  4. DWORD GetTlsIndex() {
  5. static const DWORD tls_index = TlsAlloc();
  6. return tls_index;
  7. }
  8. CExcludeWindow::CExcludeWindow()
  9. {
  10. }
  11. bool CExcludeWindow::Init()
  12. {
  13. mag_lib_handle_ = LoadLibraryW(L"Magnification.dll");
  14. if (!mag_lib_handle_)
  15. return false;
  16. // Initialize Magnification API function pointers.
  17. mag_initialize_func_ = reinterpret_cast<MagInitializeFunc>(
  18. GetProcAddress(mag_lib_handle_, "MagInitialize"));
  19. mag_uninitialize_func_ = reinterpret_cast<MagUninitializeFunc>(
  20. GetProcAddress(mag_lib_handle_, "MagUninitialize"));
  21. set_window_source_func_ = reinterpret_cast<MagSetWindowSourceFunc>(
  22. GetProcAddress(mag_lib_handle_, "MagSetWindowSource"));
  23. set_window_filter_list_func_ = reinterpret_cast<MagSetWindowFilterListFunc>(
  24. GetProcAddress(mag_lib_handle_, "MagSetWindowFilterList"));
  25. set_image_scaling_callback_func_ =
  26. reinterpret_cast<MagSetImageScalingCallbackFunc>(
  27. GetProcAddress(mag_lib_handle_, "MagSetImageScalingCallback"));
  28. if (!mag_initialize_func_ || !mag_uninitialize_func_ ||
  29. !set_window_source_func_ || !set_window_filter_list_func_ ||
  30. !set_image_scaling_callback_func_) {
  31. std::cout << "Failed to initialize ScreenCapturerWinMagnifier: "
  32. << "library functions missing.";
  33. return false;
  34. }
  35. BOOL result = mag_initialize_func_();
  36. if (!result) {
  37. std::cout << "Failed to initialize ScreenCapturerWinMagnifier: "
  38. << "error from MagInitialize " << GetLastError();
  39. return false;
  40. }
  41. HMODULE hInstance = nullptr;
  42. result =
  43. GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
  44. GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  45. reinterpret_cast<char*>(&DefWindowProc), &hInstance);
  46. if (!result) {
  47. mag_uninitialize_func_();
  48. std::cout << "Failed to initialize ScreenCapturerWinMagnifier: "
  49. << "error from GetModulehandleExA " << GetLastError();
  50. return false;
  51. }
  52. // Register the host window class. See the MSDN documentation of the
  53. // Magnification API for more infomation.
  54. WNDCLASSEXW wcex = {};
  55. wcex.cbSize = sizeof(WNDCLASSEX);
  56. wcex.lpfnWndProc = &DefWindowProc;
  57. wcex.hInstance = hInstance;
  58. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  59. wcex.lpszClassName = kMagnifierHostClass;
  60. // Ignore the error which may happen when the class is already registered.
  61. RegisterClassExW(&wcex);
  62. // Create the host window.
  63. host_window_ =
  64. CreateWindowExW(WS_EX_LAYERED, kMagnifierHostClass, kHostWindowName, 0, 0,
  65. 0, 0, 0, nullptr, nullptr, hInstance, nullptr);
  66. if (!host_window_) {
  67. mag_uninitialize_func_();
  68. std::cout << "Failed to initialize ScreenCapturerWinMagnifier: "
  69. << "error from creating host window "
  70. << GetLastError();
  71. return false;
  72. }
  73. // Create the magnifier control.
  74. magnifier_window_ = CreateWindowW(kMagnifierWindowClass, kMagnifierWindowName,
  75. WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
  76. host_window_, nullptr, hInstance, nullptr);
  77. if (!magnifier_window_) {
  78. mag_uninitialize_func_();
  79. std::cout << "Failed to initialize ScreenCapturerWinMagnifier: "
  80. << "error from creating magnifier window "
  81. << GetLastError();
  82. return false;
  83. }
  84. // Hide the host window.
  85. ShowWindow(host_window_, SW_HIDE);
  86. // Set the scaling callback to receive captured image.
  87. result = set_image_scaling_callback_func_(
  88. magnifier_window_,
  89. &CExcludeWindow::OnMagImageScalingCallback);
  90. if (!result) {
  91. mag_uninitialize_func_();
  92. std::cout << "Failed to initialize ScreenCapturerWinMagnifier: "
  93. << "error from MagSetImageScalingCallback "
  94. << GetLastError();
  95. return false;
  96. }
  97. return true;
  98. }
  99. CExcludeWindow::~CExcludeWindow()
  100. {
  101. }
  102. BOOL CExcludeWindow::OnMagImageScalingCallback(
  103. HWND hwnd,
  104. void* srcdata,
  105. MAGIMAGEHEADER srcheader,
  106. void* destdata,
  107. MAGIMAGEHEADER destheader,
  108. RECT unclipped,
  109. RECT clipped,
  110. HRGN dirty) {
  111. CExcludeWindow* owner =
  112. reinterpret_cast<CExcludeWindow*>(TlsGetValue(GetTlsIndex()));
  113. TlsSetValue(GetTlsIndex(), nullptr);
  114. owner->OnCaptured(srcdata, srcheader);
  115. return TRUE;
  116. }
  117. void CExcludeWindow::OnCaptured(void* data, const MAGIMAGEHEADER& header) {
  118. // Verify the format.
  119. // TODO(jiayl): support capturing sources with pixel formats other than RGBA.
  120. int captured_bytes_per_pixel = header.cbSize / header.width / header.height;
  121. if (header.format != GUID_WICPixelFormat32bppRGBA ) {
  122. std::cout
  123. << "Output format does not match the captured format: "
  124. << "width = " << header.width << ", "
  125. << "height = " << header.height << ", "
  126. << "stride = " << header.stride << ", "
  127. << "bpp = " << captured_bytes_per_pixel << ", "
  128. << "pixel format RGBA ? "
  129. << (header.format == GUID_WICPixelFormat32bppRGBA) << ".";
  130. return;
  131. }
  132. static FILE* fp = fopen(".\\rgba.rgba","wb");
  133. if (fp)
  134. {
  135. //fwrite(data, header.width * header.height * 4, 1, fp);
  136. fwrite(data, header.cbSize, 1, fp);
  137. fflush(fp);
  138. fclose(fp);
  139. fp = NULL;
  140. }
  141. // Copy the data into the frame.
  142. /* current_frame->CopyPixelsFrom(
  143. reinterpret_cast<uint8_t*>(data), header.stride,
  144. DesktopRect::MakeXYWH(0, 0, header.width, header.height));*/
  145. magnifier_capture_succeeded_ = true;
  146. }
  147. int CExcludeWindow::SetExcludeWnd(HWND hWnd)
  148. {
  149. if (hWnd) {
  150. BOOL result = set_window_filter_list_func_(
  151. magnifier_window_, MW_FILTERMODE_EXCLUDE, 1, &hWnd);
  152. if (!result) {
  153. mag_uninitialize_func_();
  154. std::cout
  155. << "Failed to initialize ScreenCapturerWinMagnifier: "
  156. << "error from MagSetWindowFilterList " << GetLastError();
  157. return -1;
  158. }
  159. }
  160. return 0;
  161. }
  162. bool CExcludeWindow::CaptureFrame()
  163. {
  164. int nX = GetSystemMetrics(SM_XVIRTUALSCREEN);
  165. int nY = GetSystemMetrics(SM_YVIRTUALSCREEN);
  166. int nScreenW = GetSystemMetrics(SM_CXVIRTUALSCREEN);
  167. int nScreenH = GetSystemMetrics(SM_CYVIRTUALSCREEN);
  168. BOOL result = SetWindowPos(magnifier_window_, NULL, nX, nY,
  169. nScreenW, nScreenH, 0);
  170. if (!result) {
  171. std::cout << "Failed to call SetWindowPos: " << GetLastError()
  172. << ". Rect = {" << nX
  173. << ", " << nY << ", " << nX + nScreenW << ", "
  174. << nY + nScreenH << "}";
  175. return false;
  176. }
  177. magnifier_capture_succeeded_ = false;
  178. RECT native_rect = { nX, nY, nX+nScreenW, nY+ nScreenH };
  179. TlsSetValue(GetTlsIndex(), this);
  180. // OnCaptured will be called via OnMagImageScalingCallback and fill in the
  181. // frame before set_window_source_func_ returns.
  182. result = set_window_source_func_(magnifier_window_, native_rect);
  183. if (!result) {
  184. std::cout << "Failed to call MagSetWindowSource: "
  185. << GetLastError() << ". Rect = {" << nX
  186. << ", " << nY << ", " << nX + nScreenW << ", "
  187. << nY + nScreenH << "}";
  188. return false;
  189. }
  190. return magnifier_capture_succeeded_;
  191. }

测试:

  1. CExcludeWindow ex;
  2. ex.Init();
  3. ex.SetExcludeWnd(hWnd);
  4. ex.CaptureFrame();
  5. //回调函数会写下一个rgba的文件

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/430044
推荐阅读
相关标签
  

闽ICP备14008679号