#pragma once #include #include #include #include // 包含调试宏定义 #include "../../../basic/basic.h" inline auto CreateCaptureItemForWindow(HWND hwnd) { // 首先检查窗口句柄是否有效 if (!hwnd || !::IsWindow(hwnd)) { __DebugPrint("CreateCaptureItemForWindow: Invalid window handle (HWND=0x%p)", hwnd); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } // 检查窗口是否可见 if (!::IsWindowVisible(hwnd)) { __DebugPrint("CreateCaptureItemForWindow: Window is not visible (HWND=0x%p)", hwnd); // 注意:不可见的窗口仍然可以被捕获,所以这里只是警告 } try { auto activation_factory = winrt::get_activation_factory(); auto interop_factory = activation_factory.as(); winrt::Windows::Graphics::Capture::GraphicsCaptureItem item = {nullptr}; HRESULT hr = interop_factory->CreateForWindow(hwnd, winrt::guid_of(), reinterpret_cast(winrt::put_abi(item))); if (FAILED(hr)) { __DebugPrint("CreateCaptureItemForWindow: CreateForWindow failed with HRESULT=0x%08lx (HWND=0x%p)", static_cast(hr), hwnd); // 提供更详细的错误信息 switch (hr) { case E_INVALIDARG: __DebugPrint(" Error: Invalid argument - window handle may be invalid"); break; case E_ACCESSDENIED: __DebugPrint(" Error: Access denied - window may be protected or system window"); break; case DXGI_ERROR_UNSUPPORTED: __DebugPrint(" Error: Unsupported - window capture not supported for this window"); break; case E_NOINTERFACE: __DebugPrint(" Error: No interface - WGC may not be available on this system"); break; default: __DebugPrint(" Error: Unknown error code"); break; } return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } __DebugPrint("CreateCaptureItemForWindow: Successfully created capture item (HWND=0x%p)", hwnd); return item; } catch (const winrt::hresult_error& e) { __DebugPrint("CreateCaptureItemForWindow: WinRT exception - HRESULT=0x%08lx, Message=%s (HWND=0x%p)", static_cast(e.code()), winrt::to_string(e.message()).c_str(), hwnd); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } catch (const std::exception& e) { __DebugPrint("CreateCaptureItemForWindow: Standard exception - %s (HWND=0x%p)", e.what(), hwnd); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } catch (...) { __DebugPrint("CreateCaptureItemForWindow: Unknown exception (HWND=0x%p)", hwnd); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } } inline auto CreateCaptureItemForMonitor(HMONITOR monitor) { // 检查显示器句柄是否有效 if (!monitor) { __DebugPrint("CreateCaptureItemForMonitor: Invalid monitor handle (HMONITOR=0x%p)", monitor); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } try { auto activation_factory = winrt::get_activation_factory(); auto interop_factory = activation_factory.as(); winrt::Windows::Graphics::Capture::GraphicsCaptureItem item = {nullptr}; HRESULT hr = interop_factory->CreateForMonitor(monitor, winrt::guid_of(), reinterpret_cast(winrt::put_abi(item))); if (FAILED(hr)) { __DebugPrint("CreateCaptureItemForMonitor: CreateForMonitor failed with HRESULT=0x%08lx (HMONITOR=0x%p)", static_cast(hr), monitor); // 提供更详细的错误信息 switch (hr) { case E_INVALIDARG: __DebugPrint(" Error: Invalid argument - monitor handle may be invalid"); break; case E_ACCESSDENIED: __DebugPrint(" Error: Access denied - monitor capture may be restricted"); break; case DXGI_ERROR_UNSUPPORTED: __DebugPrint(" Error: Unsupported - monitor capture not supported"); break; case E_NOINTERFACE: __DebugPrint(" Error: No interface - WGC may not be available on this system"); break; default: __DebugPrint(" Error: Unknown error code"); break; } return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } __DebugPrint("CreateCaptureItemForMonitor: Successfully created capture item (HMONITOR=0x%p)", monitor); return item; } catch (const winrt::hresult_error& e) { __DebugPrint("CreateCaptureItemForMonitor: WinRT exception - HRESULT=0x%08lx, Message=%s (HMONITOR=0x%p)", static_cast(e.code()), winrt::to_string(e.message()).c_str(), monitor); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } catch (const std::exception& e) { __DebugPrint("CreateCaptureItemForMonitor: Standard exception - %s (HMONITOR=0x%p)", e.what(), monitor); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } catch (...) { __DebugPrint("CreateCaptureItemForMonitor: Unknown exception (HMONITOR=0x%p)", monitor); return winrt::Windows::Graphics::Capture::GraphicsCaptureItem{nullptr}; } }