record_desktop_mag.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. // documents
  3. // https://learn.microsoft.com/en-us/previous-versions/windows/desktop/magapi/magapi-intro
  4. // https://learn.microsoft.com/en-us/previous-versions/windows/desktop/magapi/magapi-whats-new
  5. // https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/Magnification/cpp/Windowed/MagnifierSample.cpp
  6. #include <magnification.h>
  7. #include "record_desktop.h"
  8. namespace am {
  9. class record_desktop_mag : public record_desktop {
  10. typedef BOOL(WINAPI *MagImageScalingCallback)(
  11. HWND hwnd, void *srcdata, MAGIMAGEHEADER srcheader, void *destdata,
  12. MAGIMAGEHEADER destheader, RECT unclipped, RECT clipped, HRGN dirty);
  13. typedef BOOL(WINAPI *MagInitializeFunc)(void);
  14. typedef BOOL(WINAPI *MagUninitializeFunc)(void);
  15. typedef BOOL(WINAPI *MagSetWindowSourceFunc)(HWND hwnd, RECT rect);
  16. typedef BOOL(WINAPI *MagSetWindowFilterListFunc)(HWND hwnd,
  17. DWORD dwFilterMode,
  18. int count, HWND *pHWND);
  19. typedef BOOL(WINAPI *MagSetImageScalingCallbackFunc)(
  20. HWND hwnd, MagImageScalingCallback callback);
  21. static BOOL WINAPI on_mag_scaling_callback(
  22. HWND hwnd, void *srcdata, MAGIMAGEHEADER srcheader, void *destdata,
  23. MAGIMAGEHEADER destheader, RECT unclipped, RECT clipped, HRGN dirty);
  24. public:
  25. record_desktop_mag();
  26. ~record_desktop_mag() override;
  27. int init(const RECORD_DESKTOP_RECT &rect, const int fps) override;
  28. int start() override;
  29. int pause() override;
  30. int resume() override;
  31. int stop() override;
  32. void set_exclude(HWND excluded_window);
  33. protected:
  34. void clean_up() override;
  35. private:
  36. void record_func();
  37. void do_sleep(int64_t dur, int64_t pre, int64_t now);
  38. bool do_mag_initialize();
  39. int do_mag_record();
  40. void on_mag_data(void *data, const MAGIMAGEHEADER &header);
  41. bool seh_mag_set_window_source(HWND hwnd, RECT rect, DWORD &exception);
  42. private:
  43. uint32_t _width = 0;
  44. uint32_t _height = 0;
  45. int64_t _current_pts = -1;
  46. // Used to exclude window with specified window id.
  47. HWND _excluded_window = NULL;
  48. // Used for getting the screen dpi.
  49. HDC _desktop_dc = NULL;
  50. // Module handler
  51. HMODULE _mag_lib_handle = NULL;
  52. // Mag functions
  53. MagInitializeFunc _mag_initialize_func = nullptr;
  54. MagUninitializeFunc _mag_uninitialize_func = nullptr;
  55. MagSetWindowSourceFunc _mag_set_window_source_func = nullptr;
  56. MagSetWindowFilterListFunc _mag_set_window_filter_list_func = nullptr;
  57. MagSetImageScalingCallbackFunc _mag_set_image_scaling_callback_func = nullptr;
  58. // The hidden window hosting the magnifier control.
  59. HWND _host_window = NULL;
  60. // The magnifier control that captures the screen.
  61. HWND _magnifier_window = NULL;
  62. // True if the magnifier control has been successfully initialized.
  63. bool _magnifier_initialized = false;
  64. // True if the last OnMagImageScalingCallback was called and handled
  65. // successfully. Reset at the beginning of each CaptureImage call.
  66. bool _magnifier_capture_succeeded = true;
  67. };
  68. } // namespace am