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

SetWindowTheme

関数
ウィンドウに適用するビジュアルスタイルのサブアプリ名とサブIDを設定する。
DLLUXTHEME.dll呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

HRESULT SetWindowTheme(
    HWND hwnd,
    LPCWSTR pszSubAppName,   // optional
    LPCWSTR pszSubIdList   // optional
);

パラメーター

名前方向説明
hwndHWNDinビジュアルスタイル情報を変更するウィンドウのハンドルです。
pszSubAppNameLPCWSTRinoptional呼び出し元アプリケーションの名前の代わりに使用するアプリケーション名を格納した文字列へのポインターです。このパラメーターが NULL の場合、呼び出し元アプリケーションの名前が使用されます。
pszSubIdListLPCWSTRinoptionalウィンドウのクラスが渡す実際のリストの代わりに使用する、セミコロン区切りの CLSID 名のリストを格納した文字列へのポインターです。このパラメーターが NULL の場合、呼び出し元クラスの ID リストが使用されます。

戻り値の型: HRESULT

公式ドキュメント

ウィンドウに対して、そのクラスが通常使用するものとは異なるビジュアルスタイル情報のセットを使用させます。

戻り値

型: HRESULT

この関数が成功した場合は S_OK を返します。それ以外の場合は HRESULT エラーコードを返します。

解説(Remarks)

テーママネージャーは、ビジュアルスタイルがその後変更された場合でも、ウィンドウのライフタイムを通じて pszSubAppNamepszSubIdList の関連付けを保持します。SetWindowTheme の呼び出しの最後に、ウィンドウへ WM_THEMECHANGED メッセージが送信され、新しいビジュアルスタイルを見つけて適用できるようになります。

pszSubAppNamepszSubIdListNULL の場合、テーママネージャーは以前に適用された関連付けを削除します。空の文字列 (L"") を指定すると、どのセクションエントリにも一致しないため、指定したウィンドウへのビジュアルスタイルの適用を防止できます。

Examples

次のサンプルコードは、リストビューコントロールに Windows エクスプローラーのリストのような外観を与えます。

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

各言語での呼び出し定義

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

HRESULT SetWindowTheme(
    HWND hwnd,
    LPCWSTR pszSubAppName,   // optional
    LPCWSTR pszSubIdList   // optional
);
[DllImport("UXTHEME.dll", ExactSpelling = true)]
static extern int SetWindowTheme(
    IntPtr hwnd,   // HWND
    [MarshalAs(UnmanagedType.LPWStr)] string pszSubAppName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string pszSubIdList   // LPCWSTR optional
);
<DllImport("UXTHEME.dll", ExactSpelling:=True)>
Public Shared Function SetWindowTheme(
    hwnd As IntPtr,   ' HWND
    <MarshalAs(UnmanagedType.LPWStr)> pszSubAppName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> pszSubIdList As String   ' LPCWSTR optional
) As Integer
End Function
' hwnd : HWND
' pszSubAppName : LPCWSTR optional
' pszSubIdList : LPCWSTR optional
Declare PtrSafe Function SetWindowTheme Lib "uxtheme" ( _
    ByVal hwnd As LongPtr, _
    ByVal pszSubAppName As LongPtr, _
    ByVal pszSubIdList As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetWindowTheme = ctypes.windll.uxtheme.SetWindowTheme
SetWindowTheme.restype = ctypes.c_int
SetWindowTheme.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND
    wintypes.LPCWSTR,  # pszSubAppName : LPCWSTR optional
    wintypes.LPCWSTR,  # pszSubIdList : LPCWSTR optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('UXTHEME.dll')
SetWindowTheme = Fiddle::Function.new(
  lib['SetWindowTheme'],
  [
    Fiddle::TYPE_VOIDP,  # hwnd : HWND
    Fiddle::TYPE_VOIDP,  # pszSubAppName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # pszSubIdList : LPCWSTR optional
  ],
  Fiddle::TYPE_INT)
#[link(name = "uxtheme")]
extern "system" {
    fn SetWindowTheme(
        hwnd: *mut core::ffi::c_void,  // HWND
        pszSubAppName: *const u16,  // LPCWSTR optional
        pszSubIdList: *const u16  // LPCWSTR optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("UXTHEME.dll")]
public static extern int SetWindowTheme(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszSubAppName, [MarshalAs(UnmanagedType.LPWStr)] string pszSubIdList);
"@
$api = Add-Type -MemberDefinition $sig -Name 'UXTHEME_SetWindowTheme' -Namespace Win32 -PassThru
# $api::SetWindowTheme(hwnd, pszSubAppName, pszSubIdList)
#uselib "UXTHEME.dll"
#func global SetWindowTheme "SetWindowTheme" sptr, sptr, sptr
; SetWindowTheme hwnd, pszSubAppName, pszSubIdList   ; 戻り値は stat
; hwnd : HWND -> "sptr"
; pszSubAppName : LPCWSTR optional -> "sptr"
; pszSubIdList : LPCWSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "UXTHEME.dll"
#cfunc global SetWindowTheme "SetWindowTheme" sptr, wstr, wstr
; res = SetWindowTheme(hwnd, pszSubAppName, pszSubIdList)
; hwnd : HWND -> "sptr"
; pszSubAppName : LPCWSTR optional -> "wstr"
; pszSubIdList : LPCWSTR optional -> "wstr"
; HRESULT SetWindowTheme(HWND hwnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList)
#uselib "UXTHEME.dll"
#cfunc global SetWindowTheme "SetWindowTheme" intptr, wstr, wstr
; res = SetWindowTheme(hwnd, pszSubAppName, pszSubIdList)
; hwnd : HWND -> "intptr"
; pszSubAppName : LPCWSTR optional -> "wstr"
; pszSubIdList : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	uxtheme = windows.NewLazySystemDLL("UXTHEME.dll")
	procSetWindowTheme = uxtheme.NewProc("SetWindowTheme")
)

// hwnd (HWND), pszSubAppName (LPCWSTR optional), pszSubIdList (LPCWSTR optional)
r1, _, err := procSetWindowTheme.Call(
	uintptr(hwnd),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszSubAppName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszSubIdList))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function SetWindowTheme(
  hwnd: THandle;   // HWND
  pszSubAppName: PWideChar;   // LPCWSTR optional
  pszSubIdList: PWideChar   // LPCWSTR optional
): Integer; stdcall;
  external 'UXTHEME.dll' name 'SetWindowTheme';
result := DllCall("UXTHEME\SetWindowTheme"
    , "Ptr", hwnd   ; HWND
    , "WStr", pszSubAppName   ; LPCWSTR optional
    , "WStr", pszSubIdList   ; LPCWSTR optional
    , "Int")   ; return: HRESULT
●SetWindowTheme(hwnd, pszSubAppName, pszSubIdList) = DLL("UXTHEME.dll", "int SetWindowTheme(void*, char*, char*)")
# 呼び出し: SetWindowTheme(hwnd, pszSubAppName, pszSubIdList)
# hwnd : HWND -> "void*"
# pszSubAppName : LPCWSTR optional -> "char*"
# pszSubIdList : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "uxtheme" fn SetWindowTheme(
    hwnd: ?*anyopaque, // HWND
    pszSubAppName: [*c]const u16, // LPCWSTR optional
    pszSubIdList: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;
proc SetWindowTheme(
    hwnd: pointer,  # HWND
    pszSubAppName: WideCString,  # LPCWSTR optional
    pszSubIdList: WideCString  # LPCWSTR optional
): int32 {.importc: "SetWindowTheme", stdcall, dynlib: "UXTHEME.dll".}
pragma(lib, "uxtheme");
extern(Windows)
int SetWindowTheme(
    void* hwnd,   // HWND
    const(wchar)* pszSubAppName,   // LPCWSTR optional
    const(wchar)* pszSubIdList   // LPCWSTR optional
);
ccall((:SetWindowTheme, "UXTHEME.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Cwstring, Cwstring),
      hwnd, pszSubAppName, pszSubIdList)
# hwnd : HWND -> Ptr{Cvoid}
# pszSubAppName : LPCWSTR optional -> Cwstring
# pszSubIdList : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SetWindowTheme(
    void* hwnd,
    const uint16_t* pszSubAppName,
    const uint16_t* pszSubIdList);
]]
local uxtheme = ffi.load("uxtheme")
-- uxtheme.SetWindowTheme(hwnd, pszSubAppName, pszSubIdList)
-- hwnd : HWND
-- pszSubAppName : LPCWSTR optional
-- pszSubIdList : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('UXTHEME.dll');
const SetWindowTheme = lib.func('__stdcall', 'SetWindowTheme', 'int32_t', ['void *', 'str16', 'str16']);
// SetWindowTheme(hwnd, pszSubAppName, pszSubIdList)
// hwnd : HWND -> 'void *'
// pszSubAppName : LPCWSTR optional -> 'str16'
// pszSubIdList : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("UXTHEME.dll", {
  SetWindowTheme: { parameters: ["pointer", "buffer", "buffer"], result: "i32" },
});
// lib.symbols.SetWindowTheme(hwnd, pszSubAppName, pszSubIdList)
// hwnd : HWND -> "pointer"
// pszSubAppName : LPCWSTR optional -> "buffer"
// pszSubIdList : LPCWSTR optional -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SetWindowTheme(
    void* hwnd,
    const uint16_t* pszSubAppName,
    const uint16_t* pszSubIdList);
C, "UXTHEME.dll");
// $ffi->SetWindowTheme(hwnd, pszSubAppName, pszSubIdList);
// hwnd : HWND
// pszSubAppName : LPCWSTR optional
// pszSubIdList : LPCWSTR optional
// 構造体/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 Uxtheme extends StdCallLibrary {
    Uxtheme INSTANCE = Native.load("uxtheme", Uxtheme.class);
    int SetWindowTheme(
        Pointer hwnd,   // HWND
        WString pszSubAppName,   // LPCWSTR optional
        WString pszSubIdList   // LPCWSTR optional
    );
}
@[Link("uxtheme")]
lib LibUXTHEME
  fun SetWindowTheme = SetWindowTheme(
    hwnd : Void*,   # HWND
    pszSubAppName : UInt16*,   # LPCWSTR optional
    pszSubIdList : UInt16*   # LPCWSTR optional
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetWindowThemeNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>);
typedef SetWindowThemeDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>);
final SetWindowTheme = DynamicLibrary.open('UXTHEME.dll')
    .lookupFunction<SetWindowThemeNative, SetWindowThemeDart>('SetWindowTheme');
// hwnd : HWND -> Pointer<Void>
// pszSubAppName : LPCWSTR optional -> Pointer<Utf16>
// pszSubIdList : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetWindowTheme(
  hwnd: THandle;   // HWND
  pszSubAppName: PWideChar;   // LPCWSTR optional
  pszSubIdList: PWideChar   // LPCWSTR optional
): Integer; stdcall;
  external 'UXTHEME.dll' name 'SetWindowTheme';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetWindowTheme"
  c_SetWindowTheme :: Ptr () -> CWString -> CWString -> IO Int32
-- hwnd : HWND -> Ptr ()
-- pszSubAppName : LPCWSTR optional -> CWString
-- pszSubIdList : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setwindowtheme =
  foreign "SetWindowTheme"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning int32_t)
(* hwnd : HWND -> (ptr void) *)
(* pszSubAppName : LPCWSTR optional -> (ptr uint16_t) *)
(* pszSubIdList : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library uxtheme (t "UXTHEME.dll"))
(cffi:use-foreign-library uxtheme)

(cffi:defcfun ("SetWindowTheme" set-window-theme :convention :stdcall) :int32
  (hwnd :pointer)   ; HWND
  (psz-sub-app-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (psz-sub-id-list (:string :encoding :utf-16le)))   ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetWindowTheme = Win32::API::More->new('UXTHEME',
    'int SetWindowTheme(HANDLE hwnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList)');
# my $ret = $SetWindowTheme->Call($hwnd, $pszSubAppName, $pszSubIdList);
# hwnd : HWND -> HANDLE
# pszSubAppName : LPCWSTR optional -> LPCWSTR
# pszSubIdList : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。