How can you prevent an individual window being captured by Print-Screen?
Asked Answered
C

4

6

I noticed a few times than when I print-screen while Media Player is running, the content is replaced with a grey rectangle. I've no idea if it's deliberate due to DRM, or some technical issue, but it struck me that being able to deliberately block screen-capture might be a useful feature in some scenarios. I am not looking to fix the problem, but replicate it!

Is there some specific message each HWND gets for print-screen - does Windows do a special repaint or simply dump buffers to the clipboard?

I guess I'd prefer to focus this on Win32 specifically, but as long as it's Windows-related then fine.

Carma answered 11/6, 2010 at 12:21 Comment(2)
I may be wrong but i would guess that print screen is capturing a snap shot of whichever buffer windows uses to composite together its windows, whilst the video is being overlayed at a later stage, maybe on the video card. en.wikipedia.org/wiki/Hardware_overlaySlapbang
Some video games and media players have a built in "screen shot" capability, but most do not. The fact is media players and games tend to use DirectX which allocates a video buffer to use. You could use Fraps or some other screen capture utility to capture these type of images.Delude
M
2

You can register a hotkey, using this method. Register the PrtScr key and your application will receive a message in your WndProc, WM_HOTKEY. Once you recieve that, you can do something to your form to blank out the display, or something else.

That hotkey method should work even if your form is minimised.

Magnoliamagnoliaceous answered 11/6, 2010 at 12:26 Comment(9)
So basically for a short interval I do something like draw a big box on top of my dialog, and then remove it. Interesting. Is this proven or an idea that should work?Carma
Well, good point :) The Hotkey stuff is proven, but blocking out the dialog, that's up for experimentation.Magnoliamagnoliaceous
@John, registering a hotkey for VK_SNAPSHOT will disable print-screening completely. I thank you in advance for not disturbing the functionality of my operating system.Polysyllabic
I've seen another solution that just detects if printscr was pressed during the normal loop, and just clears the clipboard, don't know how viable that is in win32.Magnoliamagnoliaceous
All that won't help if the user uses a different program for the screenshot, for example the Snipping Tool, which is supplied with Windows Vista and later. I'd hate any app that blocks screenshots for no reason, especially since the "protection" can be circumvented easily.Flak
To clarify I wouldn't want to disable screenshots entirely. I might for instance have an app which seeks to stop you taking any digital copy of the text shown... e.g disable copy-paste, blank/blur text in a screenshot. It's an academic exercise so lets leave out the ethical aspect (although pointing out about blocking entire print-screen IS very valid)Carma
"you have to call UnregisterHotKey on closing", no, hotkeys will get unregistered automatically when the process gets terminated. However, the print-screen key will not work correctly while the app is running, which from where I'm standing is unacceptable.Polysyllabic
@Polysyllabic - this is from MSDN: In Windows XP and previous versions of Windows, if a hot key already exists with the same hWnd and id parameters, it is replaced by the new hot key. In subsequent versions of Windows, if a hot key already exists with the same hWnd and id parameters, it is maintained along with the new hot key. In these versions of Windows, the application must explicitly call UnregisterHotKey to unregister the old hot key.Magnoliamagnoliaceous
Note "the application must explicitly call UnregisterHotKey to unregister the old hot key"Magnoliamagnoliaceous
M
2

It wasn't available when the question was asked, but since Windows SDK 10.0.19041.0, we can do it:

SetWindowDisplayAffinity(hwnd, WDA_EXCLUDEFROMCAPTURE);

Before that (I don't know since when), there was also WDA_MONITOR instead of WDA_EXCLUDEFROMCAPTURE, which draws a black box instead.

I found it because OBS does it: https://github.com/obsproject/obs-studio/blob/master/UI/window-basic-main.cpp

Melessa answered 12/4 at 17:24 Comment(5)
This is the correct answer. This has the virtue of working when the user captures the screen using something other than Print Screen key (like Snipping tool, or from programs trying to capture the screen). It also has the virtue of not trying to register a global hotkey: thus being a poorly designed program that breaks all of the users other applications.Mastectomy
@IanBoyd Why then do I not see your votes on those proposed answers that recommend implementing a "poorly designed program"?Isooctane
@Isooctane I don't know why Stackoverflow doesn't let you see my votes. I suppose it was done to stop the thing that runs rampant on reddit. Here's a screenshot if you likeMastectomy
@IanBoyd I was referring to the votes that cost. We can't eliminate accepted answers as long as they have a non-negative score.Isooctane
@Isooctane You don't need to eliminate it; just vote on the better/right/correct/supported/modern answer.Mastectomy
O
1

You have to turn off 'Use overlays' to be able to take a screenshot.

Open Windows Media player. Select "Tools" -> "Options..." from the menu. Click on "Performance" tab. Click on "Advanced..." button. Uncheck "Use overlays" checkbox in the "Video Acceleration" group. Click on "OK" button. Click on "OK" button

Oscillogram answered 11/6, 2010 at 12:26 Comment(2)
Totally off-topic but thanks anyway! So it's not a feature-by-design then?Carma
The overlay is not being captured in your screenshot because if you hit print, only the desktop is being captured. The video overlay is a hardware feature of your video card. I would imagine that it is rendered directly to the video buffer, so windows doesn't know what's going on there. But in Win7 for example, you will see the contents of your media player even with enabled overlay, because everything is hardware accelerated with Aero enabled and the print function will show you the contents of the video buffer.Weatherford
I
0

Windows 7 (shipping since July 2009) introduced the SetWindowDisplayAffinity() API. Setting the affinity for a window to a value of WDA_MONITOR has the effect1 of blanking out the window contents whenever it is captured. The setting is honored regardless of whether the capture is performed via a BitBlt() call or the newer screen capture API.

SetWindowDisplayAffinity() is available for desktop applications only. For UWP applications the corresponding functionality is exposed through the ApplicationView.IsScreenCaptureEnabled property.

The intended purpose of this API is to protect against accidental information disclosure. It is not to be confused with a security feature2.


Windows 10 2004 (Build 19041) extended the API with the WDA_EXCLUDEFROMCAPTURE affinity value. In contrast to WDA_MONITOR (which leaves a blanked-out window in a screen capture), this setting removes the window entirely. Whatever is behind this window will show up in a screen capture instead.

The designated use case is to allow screen recording software to show a control GUI without it occluding part of the recording.


1 This requires that desktop composition is enabled. Starting with Windows 8 desktop composition can no longer be disabled.

2 How do I make it more difficult for somebody to take a screenshot of my window?

Isooctane answered 15/4 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.