window_capture_utils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_DESKTOP_CAPTURE_WIN_WINDOW_CAPTURE_UTILS_H_
  11. #define MODULES_DESKTOP_CAPTURE_WIN_WINDOW_CAPTURE_UTILS_H_
  12. #include <shlobj.h>
  13. #include <windows.h>
  14. #include <wrl/client.h>
  15. #include "modules/desktop_capture/desktop_capturer.h"
  16. #include "modules/desktop_capture/desktop_geometry.h"
  17. #include "rtc_base/constructor_magic.h"
  18. namespace webrtc {
  19. // Outputs the window rect. The returned DesktopRect is in system coordinates,
  20. // i.e. the primary monitor on the system always starts from (0, 0). This
  21. // function returns false if native APIs fail.
  22. bool GetWindowRect(HWND window, DesktopRect* result);
  23. // Outputs the window rect, with the left/right/bottom frame border cropped if
  24. // the window is maximized or has a transparent resize border.
  25. // |avoid_cropping_border| may be set to true to avoid cropping the visible
  26. // border when cropping any resize border.
  27. // |cropped_rect| is the cropped rect relative to the
  28. // desktop. |original_rect| is the original rect returned from GetWindowRect.
  29. // Returns true if all API calls succeeded. The returned DesktopRect is in
  30. // system coordinates, i.e. the primary monitor on the system always starts from
  31. // (0, 0). |original_rect| can be nullptr.
  32. //
  33. // TODO(zijiehe): Move this function to CroppingWindowCapturerWin after it has
  34. // been removed from MouseCursorMonitorWin.
  35. // This function should only be used by CroppingWindowCapturerWin. Instead a
  36. // DesktopRect CropWindowRect(const DesktopRect& rect)
  37. // should be added as a utility function to help CroppingWindowCapturerWin and
  38. // WindowCapturerWinGdi to crop out the borders or shadow according to their
  39. // scenarios. But this function is too generic and easy to be misused.
  40. bool GetCroppedWindowRect(HWND window,
  41. bool avoid_cropping_border,
  42. DesktopRect* cropped_rect,
  43. DesktopRect* original_rect);
  44. // Retrieves the rectangle of the content area of |window|. Usually it contains
  45. // title bar and window client area, but borders or shadow are excluded. The
  46. // returned DesktopRect is in system coordinates, i.e. the primary monitor on
  47. // the system always starts from (0, 0). This function returns false if native
  48. // APIs fail.
  49. bool GetWindowContentRect(HWND window, DesktopRect* result);
  50. // Returns the region type of the |window| and fill |rect| with the region of
  51. // |window| if region type is SIMPLEREGION.
  52. int GetWindowRegionTypeWithBoundary(HWND window, DesktopRect* result);
  53. // Retrieves the size of the |hdc|. This function returns false if native APIs
  54. // fail.
  55. bool GetDcSize(HDC hdc, DesktopSize* size);
  56. // Retrieves whether the |window| is maximized and stores in |result|. This
  57. // function returns false if native APIs fail.
  58. bool IsWindowMaximized(HWND window, bool* result);
  59. // Checks that the HWND is for a valid window, that window's visibility state is
  60. // visible, and that it is not minimized.
  61. bool IsWindowValidAndVisible(HWND window);
  62. // This function is passed into the EnumWindows API and filters out windows that
  63. // we don't want to capture, e.g. minimized or unresponsive windows and the
  64. // Start menu.
  65. BOOL CALLBACK FilterUncapturableWindows(HWND hwnd, LPARAM param);
  66. typedef HRESULT(WINAPI* DwmIsCompositionEnabledFunc)(BOOL* enabled);
  67. typedef HRESULT(WINAPI* DwmGetWindowAttributeFunc)(HWND hwnd,
  68. DWORD flag,
  69. PVOID result_ptr,
  70. DWORD result_size);
  71. class WindowCaptureHelperWin {
  72. public:
  73. WindowCaptureHelperWin();
  74. ~WindowCaptureHelperWin();
  75. bool IsAeroEnabled();
  76. bool IsWindowChromeNotification(HWND hwnd);
  77. bool AreWindowsOverlapping(HWND hwnd,
  78. HWND selected_hwnd,
  79. const DesktopRect& selected_window_rect);
  80. bool IsWindowOnCurrentDesktop(HWND hwnd);
  81. bool IsWindowVisibleOnCurrentDesktop(HWND hwnd);
  82. bool IsWindowCloaked(HWND hwnd);
  83. bool EnumerateCapturableWindows(DesktopCapturer::SourceList* results);
  84. private:
  85. HMODULE dwmapi_library_ = nullptr;
  86. DwmIsCompositionEnabledFunc func_ = nullptr;
  87. DwmGetWindowAttributeFunc dwm_get_window_attribute_func_ = nullptr;
  88. // Only used on Win10+.
  89. Microsoft::WRL::ComPtr<IVirtualDesktopManager> virtual_desktop_manager_;
  90. RTC_DISALLOW_COPY_AND_ASSIGN(WindowCaptureHelperWin);
  91. };
  92. } // namespace webrtc
  93. #endif // MODULES_DESKTOP_CAPTURE_WIN_WINDOW_CAPTURE_UTILS_H_