이번 글에서는 윈도우의 내용을 보호하는 Windows API 함수, SetWindowDisplayAffinity에 대해 알아보겠습니다.
SetWindowDisplayAffinity 를 사용하기 위해선 아래 조건을 만족해야 합니다.
Minimum supported client | Windows 7 [desktop apps only] |
Minimum supported server | Windows Server 2008 R2 [desktop apps only] |
Header | winuser.h (include Windows.h) |
Library | User32.lib |
DLL | User32.dll |
SetWindowDisplayAffinity 함수 원형은 아래와 같습니다.
BOOL SetWindowDisplayAffinity( HWND hWnd, DWORD dwAffinity );
첫번째 인자는 윈도우의 핸들이고, 두번째는 WDA_MONITOR, WDA_NONE 중 하나를 인자로 사용하시면 됩니다.
WDA_MONITOR는 보호, WDA_NONE는 보호 해제입니다.
반환 값은 해당 작업이 성공하면 TRUE, 실패하면 FALSE 입니다.
MFC 대화상자에 아래와 같이 코딩할 수 있습니다.
void CMFCApplication1Dlg::OnBnClickedButton1()
{
BOOL bSet = SetWindowDisplayAffinity( this->GetSafeHwnd(), WDA_MONITOR );
if( !bSet )
TRACE( _T( "Set Error\n" ) );
}
void CMFCApplication1Dlg::OnBnClickedButton2()
{
// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
BOOL bRel = SetWindowDisplayAffinity( this->GetSafeHwnd(), WDA_NONE );
if( !bRel )
TRACE( _T( "Release Error\n" ) );
}
버튼 두개를 만드셔서 한곳엔 설정, 한곳에 해제하는 함수를 코딩하시면 됩니다.
실제 실행 해보면 아래와 같이 됩니다.

동영상 녹화 프로그램을 실행 하고, SetWindowDisplayAffinity를 사용했습니다. 그랬더니 검게 변합니다.
Windows 10 version 2004부턴 검게 변하지 않고 그냥 없는 것처럼 안보이네요.

만약 윈도우의 현재 Affinity를 알고싶다면 GetWindowDisplayAffinity함수를 사용하여 현재 어떤 상태인지 알 수 있습니다.
다만 이 함수를 사용할 때 주의할 점은, Desktop Window Manager(DWM)을 사용하고 있어야한다는 겁니다.
DwmIsCompositionEnabled함수를 통해 현재 DWM을 사용하는지 알 수 있습니다.
또한 첫번째 인자에 핸들은, 내가 소유하고 있는 핸들만 가능합니다. 다른 프로세스엔 안된다는 겁니다.