sskodje / ScreenRecorderLib

A .NET library for screen recording in Windows, using native Microsoft Media Foundation for realtime encoding to h264 video or PNG images.
MIT License
414 stars 94 forks source link

Exclude one window from recording #259

Closed k1mmm closed 1 year ago

k1mmm commented 1 year ago

Hello! Does the source options support adding multiple windows?

My goal is trying to exclude one window from the recording (in my case the recording controls itself) but I'm getting the error "wrongfully params" when starting the recording.

My idea was to add the screen and all windows but exclude my recording controls window to the RecordingSources list.

ranwer-dev commented 1 year ago

You can use SetWindowDisplayAffinity to exclude the window which you don't want to record. Here's how you can use that

[DllImport("user32.dll")]
static extern uint SetWindowDisplayAffinity(IntPtr hWnd, uint dwAffinity);
const uint WDA_NONE = 0x00000000;
const uint WDA_MONITOR = 0x00000001;
const uint WDA_EXCLUDEFROMCAPTURE = 0x00000011;

private void includeButton_Click(object sender, EventArgs e)
{
    SetWindowDisplayAffinity(this.Handle, WDA_NONE);
}

private void excludeButton_Click(object sender, EventArgs e)
{
    SetWindowDisplayAffinity(this.Handle, WDA_MONITOR);
}
k1mmm commented 1 year ago

@ranwer-dev It worked, thank you!

sskodje commented 1 year ago

You can also call it like this to exclude the current window:

IntPtr hwnd = new WindowInteropHelper(this).Handle;
Recorder.SetExcludeFromCapture(hwnd, true);

To include it again, call the same with "false" as param.