Win32 API 日本語リファレンス
ホームUI.WindowsAndMessaging › SetWindowDisplayAffinity

SetWindowDisplayAffinity

関数
ウィンドウの画面キャプチャ可否を設定する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 7 以降

シグネチャ

// USER32.dll
#include <windows.h>

BOOL SetWindowDisplayAffinity(
    HWND hWnd,
    WINDOW_DISPLAY_AFFINITY dwAffinity
);

パラメーター

名前方向説明
hWndHWNDinトップレベルウィンドウへのハンドル。ウィンドウは現在のプロセスに属している必要があります。
dwAffinityWINDOW_DISPLAY_AFFINITYin

ウィンドウのコンテンツを表示できる場所を指定する表示アフィニティ設定。

このパラメーターには、次のいずれかの値を指定できます。

意味
WDA_NONE
0x00000000
ウィンドウを表示できる場所に制限を課しません。
WDA_MONITOR
0x00000001
ウィンドウのコンテンツはモニター上にのみ表示されます。 それ以外の場所では、ウィンドウはコンテンツなしで表示されます。
WDA_EXCLUDEFROMCAPTURE
0x00000011
ウィンドウはモニター上にのみ表示されます。 それ以外の場所では、ウィンドウはまったく表示されません。

このアフィニティの用途の一つは、ビデオ録画コントロールを表示するウィンドウであり、これによりコントロールがキャプチャに含まれないようにします。

Windows 10 バージョン 2004 で導入されました。以前のバージョンの Windows との互換性については、注釈を参照してください。

戻り値の型: BOOL

公式ドキュメント

ウィンドウに関連付けられた hWnd に対して、表示アフィニティ設定をカーネルモードで保存します。

戻り値

型: BOOL

関数が成功した場合は TRUE を返します。それ以外の場合、たとえばトップレベルではないウィンドウに対して関数を呼び出したときなどは FALSE を返します。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

この関数と GetWindowDisplayAffinity は、Windows 7 で新たに導入されたウィンドウコンテンツ保護機能をサポートするために設計されています。この機能により、アプリケーションは、特定の一連の公開オペレーティングシステム機能および API を通じて、自身の画面上のウィンドウコンテンツがキャプチャまたはコピーされるのを防ぐことができます。ただし、これはデスクトップウィンドウマネージャー (DWM) がデスクトップを合成している場合にのみ機能します。

重要な点として、これはセキュリティ機能やデジタル著作権管理 (DRM) の実装とは異なり、SetWindowDisplayAffinityGetWindowDisplayAffinity、および DwmIsCompositionEnabled などの必要な関数を使用しても、ウィンドウコンテンツが厳密に保護されることは保証されません。たとえば、誰かが画面を写真撮影する場合などです。

Windows 10 バージョン 2004 以降では、WDA_EXCLUDEFROMCAPTURE がサポートされる値となっています。以前のバージョンの Windows で表示アフィニティを WDA_EXCLUDEFROMCAPTURE に設定した場合は、WDA_MONITOR が適用されたかのように動作します。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// USER32.dll
#include <windows.h>

BOOL SetWindowDisplayAffinity(
    HWND hWnd,
    WINDOW_DISPLAY_AFFINITY dwAffinity
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetWindowDisplayAffinity(
    IntPtr hWnd,   // HWND
    uint dwAffinity   // WINDOW_DISPLAY_AFFINITY
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWindowDisplayAffinity(
    hWnd As IntPtr,   ' HWND
    dwAffinity As UInteger   ' WINDOW_DISPLAY_AFFINITY
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hWnd : HWND
' dwAffinity : WINDOW_DISPLAY_AFFINITY
Declare PtrSafe Function SetWindowDisplayAffinity Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal dwAffinity As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity
SetWindowDisplayAffinity.restype = wintypes.BOOL
SetWindowDisplayAffinity.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    wintypes.DWORD,  # dwAffinity : WINDOW_DISPLAY_AFFINITY
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetWindowDisplayAffinity = Fiddle::Function.new(
  lib['SetWindowDisplayAffinity'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    -Fiddle::TYPE_INT,  # dwAffinity : WINDOW_DISPLAY_AFFINITY
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn SetWindowDisplayAffinity(
        hWnd: *mut core::ffi::c_void,  // HWND
        dwAffinity: u32  // WINDOW_DISPLAY_AFFINITY
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true)]
public static extern bool SetWindowDisplayAffinity(IntPtr hWnd, uint dwAffinity);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetWindowDisplayAffinity' -Namespace Win32 -PassThru
# $api::SetWindowDisplayAffinity(hWnd, dwAffinity)
#uselib "USER32.dll"
#func global SetWindowDisplayAffinity "SetWindowDisplayAffinity" sptr, sptr
; SetWindowDisplayAffinity hWnd, dwAffinity   ; 戻り値は stat
; hWnd : HWND -> "sptr"
; dwAffinity : WINDOW_DISPLAY_AFFINITY -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetWindowDisplayAffinity "SetWindowDisplayAffinity" sptr, int
; res = SetWindowDisplayAffinity(hWnd, dwAffinity)
; hWnd : HWND -> "sptr"
; dwAffinity : WINDOW_DISPLAY_AFFINITY -> "int"
; BOOL SetWindowDisplayAffinity(HWND hWnd, WINDOW_DISPLAY_AFFINITY dwAffinity)
#uselib "USER32.dll"
#cfunc global SetWindowDisplayAffinity "SetWindowDisplayAffinity" intptr, int
; res = SetWindowDisplayAffinity(hWnd, dwAffinity)
; hWnd : HWND -> "intptr"
; dwAffinity : WINDOW_DISPLAY_AFFINITY -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowDisplayAffinity = user32.NewProc("SetWindowDisplayAffinity")
)

// hWnd (HWND), dwAffinity (WINDOW_DISPLAY_AFFINITY)
r1, _, err := procSetWindowDisplayAffinity.Call(
	uintptr(hWnd),
	uintptr(dwAffinity),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetWindowDisplayAffinity(
  hWnd: THandle;   // HWND
  dwAffinity: DWORD   // WINDOW_DISPLAY_AFFINITY
): BOOL; stdcall;
  external 'USER32.dll' name 'SetWindowDisplayAffinity';
result := DllCall("USER32\SetWindowDisplayAffinity"
    , "Ptr", hWnd   ; HWND
    , "UInt", dwAffinity   ; WINDOW_DISPLAY_AFFINITY
    , "Int")   ; return: BOOL
●SetWindowDisplayAffinity(hWnd, dwAffinity) = DLL("USER32.dll", "bool SetWindowDisplayAffinity(void*, dword)")
# 呼び出し: SetWindowDisplayAffinity(hWnd, dwAffinity)
# hWnd : HWND -> "void*"
# dwAffinity : WINDOW_DISPLAY_AFFINITY -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "user32" fn SetWindowDisplayAffinity(
    hWnd: ?*anyopaque, // HWND
    dwAffinity: u32 // WINDOW_DISPLAY_AFFINITY
) callconv(std.os.windows.WINAPI) i32;
proc SetWindowDisplayAffinity(
    hWnd: pointer,  # HWND
    dwAffinity: uint32  # WINDOW_DISPLAY_AFFINITY
): int32 {.importc: "SetWindowDisplayAffinity", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
int SetWindowDisplayAffinity(
    void* hWnd,   // HWND
    uint dwAffinity   // WINDOW_DISPLAY_AFFINITY
);
ccall((:SetWindowDisplayAffinity, "USER32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32),
      hWnd, dwAffinity)
# hWnd : HWND -> Ptr{Cvoid}
# dwAffinity : WINDOW_DISPLAY_AFFINITY -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SetWindowDisplayAffinity(
    void* hWnd,
    uint32_t dwAffinity);
]]
local user32 = ffi.load("user32")
-- user32.SetWindowDisplayAffinity(hWnd, dwAffinity)
-- hWnd : HWND
-- dwAffinity : WINDOW_DISPLAY_AFFINITY
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetWindowDisplayAffinity = lib.func('__stdcall', 'SetWindowDisplayAffinity', 'int32_t', ['void *', 'uint32_t']);
// SetWindowDisplayAffinity(hWnd, dwAffinity)
// hWnd : HWND -> 'void *'
// dwAffinity : WINDOW_DISPLAY_AFFINITY -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  SetWindowDisplayAffinity: { parameters: ["pointer", "u32"], result: "i32" },
});
// lib.symbols.SetWindowDisplayAffinity(hWnd, dwAffinity)
// hWnd : HWND -> "pointer"
// dwAffinity : WINDOW_DISPLAY_AFFINITY -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SetWindowDisplayAffinity(
    void* hWnd,
    uint32_t dwAffinity);
C, "USER32.dll");
// $ffi->SetWindowDisplayAffinity(hWnd, dwAffinity);
// hWnd : HWND
// dwAffinity : WINDOW_DISPLAY_AFFINITY
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface User32 extends StdCallLibrary {
    User32 INSTANCE = Native.load("user32", User32.class);
    boolean SetWindowDisplayAffinity(
        Pointer hWnd,   // HWND
        int dwAffinity   // WINDOW_DISPLAY_AFFINITY
    );
}
@[Link("user32")]
lib LibUSER32
  fun SetWindowDisplayAffinity = SetWindowDisplayAffinity(
    hWnd : Void*,   # HWND
    dwAffinity : UInt32   # WINDOW_DISPLAY_AFFINITY
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetWindowDisplayAffinityNative = Int32 Function(Pointer<Void>, Uint32);
typedef SetWindowDisplayAffinityDart = int Function(Pointer<Void>, int);
final SetWindowDisplayAffinity = DynamicLibrary.open('USER32.dll')
    .lookupFunction<SetWindowDisplayAffinityNative, SetWindowDisplayAffinityDart>('SetWindowDisplayAffinity');
// hWnd : HWND -> Pointer<Void>
// dwAffinity : WINDOW_DISPLAY_AFFINITY -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetWindowDisplayAffinity(
  hWnd: THandle;   // HWND
  dwAffinity: DWORD   // WINDOW_DISPLAY_AFFINITY
): BOOL; stdcall;
  external 'USER32.dll' name 'SetWindowDisplayAffinity';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetWindowDisplayAffinity"
  c_SetWindowDisplayAffinity :: Ptr () -> Word32 -> IO CInt
-- hWnd : HWND -> Ptr ()
-- dwAffinity : WINDOW_DISPLAY_AFFINITY -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setwindowdisplayaffinity =
  foreign "SetWindowDisplayAffinity"
    ((ptr void) @-> uint32_t @-> returning int32_t)
(* hWnd : HWND -> (ptr void) *)
(* dwAffinity : WINDOW_DISPLAY_AFFINITY -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("SetWindowDisplayAffinity" set-window-display-affinity :convention :stdcall) :int32
  (h-wnd :pointer)   ; HWND
  (dw-affinity :uint32))   ; WINDOW_DISPLAY_AFFINITY
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetWindowDisplayAffinity = Win32::API::More->new('USER32',
    'BOOL SetWindowDisplayAffinity(HANDLE hWnd, DWORD dwAffinity)');
# my $ret = $SetWindowDisplayAffinity->Call($hWnd, $dwAffinity);
# hWnd : HWND -> HANDLE
# dwAffinity : WINDOW_DISPLAY_AFFINITY -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型