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

SetLayeredWindowAttributes

関数
レイヤードウィンドウの不透明度や色キーを設定する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL SetLayeredWindowAttributes(
    HWND hwnd,
    COLORREF crKey,
    BYTE bAlpha,
    LAYERED_WINDOW_ATTRIBUTES_FLAGS dwFlags
);

パラメーター

名前方向
hwndHWNDin
crKeyCOLORREFin
bAlphaBYTEin
dwFlagsLAYERED_WINDOW_ATTRIBUTES_FLAGSin

戻り値の型: BOOL

各言語での呼び出し定義

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

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

SetLayeredWindowAttributes = ctypes.windll.user32.SetLayeredWindowAttributes
SetLayeredWindowAttributes.restype = wintypes.BOOL
SetLayeredWindowAttributes.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND
    wintypes.DWORD,  # crKey : COLORREF
    ctypes.c_ubyte,  # bAlpha : BYTE
    wintypes.DWORD,  # dwFlags : LAYERED_WINDOW_ATTRIBUTES_FLAGS
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetLayeredWindowAttributes = Fiddle::Function.new(
  lib['SetLayeredWindowAttributes'],
  [
    Fiddle::TYPE_VOIDP,  # hwnd : HWND
    -Fiddle::TYPE_INT,  # crKey : COLORREF
    -Fiddle::TYPE_CHAR,  # bAlpha : BYTE
    -Fiddle::TYPE_INT,  # dwFlags : LAYERED_WINDOW_ATTRIBUTES_FLAGS
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn SetLayeredWindowAttributes(
        hwnd: *mut core::ffi::c_void,  // HWND
        crKey: u32,  // COLORREF
        bAlpha: u8,  // BYTE
        dwFlags: u32  // LAYERED_WINDOW_ATTRIBUTES_FLAGS
    ) -> 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 SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetLayeredWindowAttributes' -Namespace Win32 -PassThru
# $api::SetLayeredWindowAttributes(hwnd, crKey, bAlpha, dwFlags)
#uselib "USER32.dll"
#func global SetLayeredWindowAttributes "SetLayeredWindowAttributes" sptr, sptr, sptr, sptr
; SetLayeredWindowAttributes hwnd, crKey, bAlpha, dwFlags   ; 戻り値は stat
; hwnd : HWND -> "sptr"
; crKey : COLORREF -> "sptr"
; bAlpha : BYTE -> "sptr"
; dwFlags : LAYERED_WINDOW_ATTRIBUTES_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetLayeredWindowAttributes "SetLayeredWindowAttributes" sptr, int, int, int
; res = SetLayeredWindowAttributes(hwnd, crKey, bAlpha, dwFlags)
; hwnd : HWND -> "sptr"
; crKey : COLORREF -> "int"
; bAlpha : BYTE -> "int"
; dwFlags : LAYERED_WINDOW_ATTRIBUTES_FLAGS -> "int"
; BOOL SetLayeredWindowAttributes(HWND hwnd, COLORREF crKey, BYTE bAlpha, LAYERED_WINDOW_ATTRIBUTES_FLAGS dwFlags)
#uselib "USER32.dll"
#cfunc global SetLayeredWindowAttributes "SetLayeredWindowAttributes" intptr, int, int, int
; res = SetLayeredWindowAttributes(hwnd, crKey, bAlpha, dwFlags)
; hwnd : HWND -> "intptr"
; crKey : COLORREF -> "int"
; bAlpha : BYTE -> "int"
; dwFlags : LAYERED_WINDOW_ATTRIBUTES_FLAGS -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetLayeredWindowAttributes = user32.NewProc("SetLayeredWindowAttributes")
)

// hwnd (HWND), crKey (COLORREF), bAlpha (BYTE), dwFlags (LAYERED_WINDOW_ATTRIBUTES_FLAGS)
r1, _, err := procSetLayeredWindowAttributes.Call(
	uintptr(hwnd),
	uintptr(crKey),
	uintptr(bAlpha),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetLayeredWindowAttributes(
  hwnd: THandle;   // HWND
  crKey: DWORD;   // COLORREF
  bAlpha: Byte;   // BYTE
  dwFlags: DWORD   // LAYERED_WINDOW_ATTRIBUTES_FLAGS
): BOOL; stdcall;
  external 'USER32.dll' name 'SetLayeredWindowAttributes';
result := DllCall("USER32\SetLayeredWindowAttributes"
    , "Ptr", hwnd   ; HWND
    , "UInt", crKey   ; COLORREF
    , "UChar", bAlpha   ; BYTE
    , "UInt", dwFlags   ; LAYERED_WINDOW_ATTRIBUTES_FLAGS
    , "Int")   ; return: BOOL
●SetLayeredWindowAttributes(hwnd, crKey, bAlpha, dwFlags) = DLL("USER32.dll", "bool SetLayeredWindowAttributes(void*, dword, byte, dword)")
# 呼び出し: SetLayeredWindowAttributes(hwnd, crKey, bAlpha, dwFlags)
# hwnd : HWND -> "void*"
# crKey : COLORREF -> "dword"
# bAlpha : BYTE -> "byte"
# dwFlags : LAYERED_WINDOW_ATTRIBUTES_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。