App.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "App.h"
  2. // D3D
  3. #include <d2d1_3.h>
  4. #include <d3d11_4.h>
  5. #include <dxgi1_6.h>
  6. #include <wincodec.h>
  7. #include "pch.h"
  8. #include "basic/frame.h"
  9. #include <QDebug>
  10. using namespace winrt;
  11. using namespace Windows::System;
  12. using namespace Windows::Foundation;
  13. using namespace Windows::UI;
  14. using namespace Windows::UI::Composition;
  15. using namespace Windows::Graphics::Capture;
  16. void App::Initialize(ContainerVisual const& root)
  17. {
  18. auto queue = DispatcherQueue::GetForCurrentThread();
  19. m_compositor = root.Compositor();
  20. m_root = m_compositor.CreateContainerVisual();
  21. m_content = m_compositor.CreateSpriteVisual();
  22. m_brush = m_compositor.CreateSurfaceBrush();
  23. m_root.RelativeSizeAdjustment({1, 1});
  24. root.Children().InsertAtTop(m_root);
  25. m_content.AnchorPoint({0.5f, 0.5f});
  26. m_content.RelativeOffsetAdjustment({0.5f, 0.5f, 0});
  27. m_content.RelativeSizeAdjustment({1, 1});
  28. m_content.Size({-80, -80});
  29. m_content.Brush(m_brush);
  30. m_brush.HorizontalAlignmentRatio(0.5f);
  31. m_brush.VerticalAlignmentRatio(0.5f);
  32. m_brush.Stretch(CompositionStretch::Uniform);
  33. auto shadow = m_compositor.CreateDropShadow();
  34. shadow.Mask(m_brush);
  35. m_content.Shadow(shadow);
  36. m_root.Children().InsertAtTop(m_content);
  37. auto d3dDevice = CreateD3DDevice();
  38. auto dxgiDevice = d3dDevice.as<IDXGIDevice>();
  39. m_device = CreateDirect3DDevice(dxgiDevice.get());
  40. }
  41. void App::Close()
  42. {
  43. qDebug() << "App::Close() - 开始关闭App";
  44. if (m_capture) {
  45. qDebug() << "App::Close() - 调用SimpleCapture::Close()";
  46. m_capture->Close();
  47. qDebug() << "App::Close() - 删除SimpleCapture对象";
  48. delete m_capture;
  49. m_capture = nullptr;
  50. qDebug() << "App::Close() - SimpleCapture对象已删除";
  51. }
  52. qDebug() << "App::Close() - App关闭完成";
  53. }
  54. bool App::StartCaptureWindow(HWND hwnd, int width, int height)
  55. {
  56. __DebugPrint("App::StartCaptureWindow: Starting window capture (HWND=0x%p, %dx%d)", hwnd, width, height);
  57. // 验证输入参数
  58. if (!hwnd) {
  59. __DebugPrint("App::StartCaptureWindow: Invalid HWND (null)");
  60. return false;
  61. }
  62. if (width <= 0 || height <= 0) {
  63. __DebugPrint("App::StartCaptureWindow: Invalid dimensions (%dx%d)", width, height);
  64. return false;
  65. }
  66. if (!m_device) {
  67. __DebugPrint("App::StartCaptureWindow: D3D device is null");
  68. return false;
  69. }
  70. if (!m_compositor) {
  71. __DebugPrint("App::StartCaptureWindow: Compositor is null");
  72. return false;
  73. }
  74. // 获取窗口信息用于调试
  75. RECT windowRect;
  76. if (::GetWindowRect(hwnd, &windowRect)) {
  77. __DebugPrint("App::StartCaptureWindow: Window rect (%ld,%ld,%ld,%ld)",
  78. windowRect.left, windowRect.top, windowRect.right, windowRect.bottom);
  79. }
  80. wchar_t windowTitle[256] = {0};
  81. ::GetWindowTextW(hwnd, windowTitle, sizeof(windowTitle)/sizeof(wchar_t));
  82. __DebugPrint("App::StartCaptureWindow: Window title: %ls", windowTitle);
  83. try {
  84. Close();
  85. __DebugPrint("App::StartCaptureWindow: Creating capture item...");
  86. auto item = CreateCaptureItemForWindow(hwnd);
  87. if (!item) {
  88. __DebugPrint("App::StartCaptureWindow: CreateCaptureItemForWindow returned null");
  89. return false;
  90. }
  91. __DebugPrint("App::StartCaptureWindow: Creating SimpleCapture...");
  92. m_capture = new SimpleCapture(m_device, item, width, height);
  93. if (!m_capture) {
  94. __DebugPrint("App::StartCaptureWindow: Failed to create SimpleCapture");
  95. return false;
  96. }
  97. __DebugPrint("App::StartCaptureWindow: Creating surface...");
  98. auto surface = m_capture->CreateSurface(m_compositor);
  99. if (!surface) {
  100. __DebugPrint("App::StartCaptureWindow: Failed to create surface");
  101. delete m_capture;
  102. m_capture = nullptr;
  103. return false;
  104. }
  105. __DebugPrint("App::StartCaptureWindow: Setting surface to brush...");
  106. m_brush.Surface(surface);
  107. __DebugPrint("App::StartCaptureWindow: Starting capture...");
  108. m_capture->StartCapture();
  109. __DebugPrint("App::StartCaptureWindow: Window capture started successfully");
  110. return true;
  111. } catch (const winrt::hresult_error& e) {
  112. __DebugPrint("App::StartCaptureWindow: WinRT exception - HRESULT=0x%08lx, Message=%s",
  113. static_cast<unsigned long>(e.code()),
  114. winrt::to_string(e.message()).c_str());
  115. if (m_capture) {
  116. delete m_capture;
  117. m_capture = nullptr;
  118. }
  119. return false;
  120. } catch (const std::exception& e) {
  121. __DebugPrint("App::StartCaptureWindow: Standard exception - %s", e.what());
  122. if (m_capture) {
  123. delete m_capture;
  124. m_capture = nullptr;
  125. }
  126. return false;
  127. } catch (...) {
  128. __DebugPrint("App::StartCaptureWindow: Unknown exception");
  129. if (m_capture) {
  130. delete m_capture;
  131. m_capture = nullptr;
  132. }
  133. return false;
  134. }
  135. }
  136. void App::SetDrawCursor(bool isDrawCursor)
  137. {
  138. if (m_capture == nullptr) {
  139. return;
  140. }
  141. m_capture->SetDrawCursor(isDrawCursor);
  142. }
  143. bool App::StartCaptureMonitor(HMONITOR monitor, int width, int height)
  144. {
  145. Close();
  146. auto item = CreateCaptureItemForMonitor(monitor);
  147. if (!item) {
  148. __DebugPrint("CreateCaptureItemForMonitor returned null");
  149. return false;
  150. }
  151. m_capture = new SimpleCapture(m_device, item, width, height);
  152. auto surface = m_capture->CreateSurface(m_compositor);
  153. m_brush.Surface(surface);
  154. m_capture->StartCapture();
  155. return true;
  156. }