Sign in Sign up
kretrod/lodestone-cpp Public
Branches
main
35 lines (29 loc) · 1.5 KB Raw
// "Hide from screen capture".
//
// The menu is drawn into the game's own back buffer, so to a recorder its
// pixels *are* the game's pixels — there is no way to exclude just the menu
// from a capture of that window. What Windows offers instead is
// SetWindowDisplayAffinity, which takes the whole window out of capture:
// recorders, screen shares and PrintScreen get black where it used to be.
//
// So this is honest about what it is: an all-or-nothing switch on the game
// window, not a way to record clean footage with the menu open.
#pragma once

#include <windows.h>

namespace lodestone::capture {

inline HWND g_window = nullptr;

inline void set_window(HWND hwnd) { g_window = hwnd; }

/// Returns false when Windows refused — the flag needs Windows 10 2004 or
/// newer, and the call is resolved at runtime so older builds simply say no.
inline bool apply(bool hide) {
    if (!g_window) return false;
    using PFNSetWindowDisplayAffinity = BOOL(WINAPI*)(HWND, DWORD);
    static auto fn = reinterpret_cast<PFNSetWindowDisplayAffinity>(
        reinterpret_cast<void*>(GetProcAddress(GetModuleHandleA("user32.dll"),
                                               "SetWindowDisplayAffinity")));
    if (!fn) return false;
    constexpr DWORD kNone = 0x00000000;              // WDA_NONE
    constexpr DWORD kExcludeFromCapture = 0x00000011;  // WDA_EXCLUDEFROMCAPTURE
    return fn(g_window, hide ? kExcludeFromCapture : kNone) != 0;
}

}  // namespace lodestone::capture