Win32 API 日本語リファレンス
ホームGraphics.Gdi › SetSysColors

SetSysColors

関数
指定したシステム表示要素の色を変更する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL SetSysColors(
    INT cElements,
    const INT* lpaElements,
    const COLORREF* lpaRgbValues
);

パラメーター

名前方向説明
cElementsINTinlpaElements 配列内の表示要素の数。
lpaElementsINT*in変更する表示要素を指定する整数の配列。表示要素の一覧については、 GetSysColor を参照してください。
lpaRgbValuesCOLORREF*in

lpaElements パラメーターが指す配列内の表示要素に対する、新しい赤、緑、青 (RGB) の色値を含む COLORREF 値の配列。

COLORREF を生成するには、 RGB マクロを使用します。

戻り値の型: BOOL

公式ドキュメント

指定された表示要素の色を設定します。

戻り値

型: BOOL

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

SetSysColors 関数は、色の変更を通知するためにすべてのウィンドウへ WM_SYSCOLORCHANGE メッセージを送信します。また、現在表示されているすべてのウィンドウの影響を受ける部分を再描画するようシステムに指示します。

ユーザーが指定した色設定を尊重することが最善です。ユーザーが色を変更できるようにするアプリケーションを作成する場合は、この関数を使用するのが適切です。ただし、この関数は現在のセッションにのみ影響します。システムが終了すると、新しい色は保存されません。

次の例は、GetSysColor 関数と SetSysColors 関数の使用方法を示しています。まず、この例では GetSysColor を使用してウィンドウの背景とアクティブなキャプションの色を取得し、赤、緑、青 (RGB) の値を 16 進表記で表示します。次に、SetSysColors を使用してウィンドウの背景の色を明るい灰色に、アクティブなタイトルバーを濃い紫に変更します。10 秒間の遅延の後、この例では SetSysColors を使用してこれらの要素を以前の色に復元します。

#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

void main()
{
    int aElements[2] = {COLOR_WINDOW, COLOR_ACTIVECAPTION};
    DWORD aOldColors[2];
    DWORD aNewColors[2];

    // Get the current color of the window background. 
 
    aOldColors[0] = GetSysColor(aElements[0]); 

    printf("Current window color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aOldColors[0]), 
        GetGValue(aOldColors[0]), 
        GetBValue(aOldColors[0]));

    // Get the current color of the active caption. 
 
    aOldColors[1] = GetSysColor(aElements[1]); 

    printf("Current active caption color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aOldColors[1]), 
        GetGValue(aOldColors[1]), 
        GetBValue(aOldColors[1]));

    // Define new colors for the elements

    aNewColors[0] = RGB(0x80, 0x80, 0x80);  // light gray 
    aNewColors[1] = RGB(0x80, 0x00, 0x80);  // dark purple 

    printf("\nNew window color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aNewColors[0]), 
        GetGValue(aNewColors[0]), 
        GetBValue(aNewColors[0]));

    printf("New active caption color: {0x%x, 0x%x, 0x%x}\n", 
        GetRValue(aNewColors[1]), 
        GetGValue(aNewColors[1]), 
        GetBValue(aNewColors[1]));

    // Set the elements defined in aElements to the colors defined
    // in aNewColors

    SetSysColors(2, aElements, aNewColors); 

    printf("\nWindow background and active border have been changed.\n");
    printf("Reverting to previous colors in 10 seconds...\n");

    Sleep(10000);    

    // Restore the elements to their original colors

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

各言語での呼び出し定義

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

BOOL SetSysColors(
    INT cElements,
    const INT* lpaElements,
    const COLORREF* lpaRgbValues
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetSysColors(
    int cElements,   // INT
    ref int lpaElements,   // INT*
    ref uint lpaRgbValues   // COLORREF*
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetSysColors(
    cElements As Integer,   ' INT
    ByRef lpaElements As Integer,   ' INT*
    ByRef lpaRgbValues As UInteger   ' COLORREF*
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' cElements : INT
' lpaElements : INT*
' lpaRgbValues : COLORREF*
Declare PtrSafe Function SetSysColors Lib "user32" ( _
    ByVal cElements As Long, _
    ByRef lpaElements As Long, _
    ByRef lpaRgbValues As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetSysColors = ctypes.windll.user32.SetSysColors
SetSysColors.restype = wintypes.BOOL
SetSysColors.argtypes = [
    ctypes.c_int,  # cElements : INT
    ctypes.POINTER(ctypes.c_int),  # lpaElements : INT*
    ctypes.c_void_p,  # lpaRgbValues : COLORREF*
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetSysColors = Fiddle::Function.new(
  lib['SetSysColors'],
  [
    Fiddle::TYPE_INT,  # cElements : INT
    Fiddle::TYPE_VOIDP,  # lpaElements : INT*
    Fiddle::TYPE_VOIDP,  # lpaRgbValues : COLORREF*
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn SetSysColors(
        cElements: i32,  // INT
        lpaElements: *const i32,  // INT*
        lpaRgbValues: *const u32  // COLORREF*
    ) -> 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 SetSysColors(int cElements, ref int lpaElements, ref uint lpaRgbValues);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetSysColors' -Namespace Win32 -PassThru
# $api::SetSysColors(cElements, lpaElements, lpaRgbValues)
#uselib "USER32.dll"
#func global SetSysColors "SetSysColors" sptr, sptr, sptr
; SetSysColors cElements, varptr(lpaElements), varptr(lpaRgbValues)   ; 戻り値は stat
; cElements : INT -> "sptr"
; lpaElements : INT* -> "sptr"
; lpaRgbValues : COLORREF* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global SetSysColors "SetSysColors" int, var, var
; res = SetSysColors(cElements, lpaElements, lpaRgbValues)
; cElements : INT -> "int"
; lpaElements : INT* -> "var"
; lpaRgbValues : COLORREF* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetSysColors(INT cElements, INT* lpaElements, COLORREF* lpaRgbValues)
#uselib "USER32.dll"
#cfunc global SetSysColors "SetSysColors" int, var, var
; res = SetSysColors(cElements, lpaElements, lpaRgbValues)
; cElements : INT -> "int"
; lpaElements : INT* -> "var"
; lpaRgbValues : COLORREF* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetSysColors = user32.NewProc("SetSysColors")
)

// cElements (INT), lpaElements (INT*), lpaRgbValues (COLORREF*)
r1, _, err := procSetSysColors.Call(
	uintptr(cElements),
	uintptr(lpaElements),
	uintptr(lpaRgbValues),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetSysColors(
  cElements: Integer;   // INT
  lpaElements: Pointer;   // INT*
  lpaRgbValues: Pointer   // COLORREF*
): BOOL; stdcall;
  external 'USER32.dll' name 'SetSysColors';
result := DllCall("USER32\SetSysColors"
    , "Int", cElements   ; INT
    , "Ptr", lpaElements   ; INT*
    , "Ptr", lpaRgbValues   ; COLORREF*
    , "Int")   ; return: BOOL
●SetSysColors(cElements, lpaElements, lpaRgbValues) = DLL("USER32.dll", "bool SetSysColors(int, void*, void*)")
# 呼び出し: SetSysColors(cElements, lpaElements, lpaRgbValues)
# cElements : INT -> "int"
# lpaElements : INT* -> "void*"
# lpaRgbValues : COLORREF* -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "user32" fn SetSysColors(
    cElements: i32, // INT
    lpaElements: [*c]i32, // INT*
    lpaRgbValues: [*c]u32 // COLORREF*
) callconv(std.os.windows.WINAPI) i32;
proc SetSysColors(
    cElements: int32,  # INT
    lpaElements: ptr int32,  # INT*
    lpaRgbValues: ptr uint32  # COLORREF*
): int32 {.importc: "SetSysColors", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
int SetSysColors(
    int cElements,   // INT
    int* lpaElements,   // INT*
    uint* lpaRgbValues   // COLORREF*
);
ccall((:SetSysColors, "USER32.dll"), stdcall, Int32,
      (Int32, Ptr{Int32}, Ptr{UInt32}),
      cElements, lpaElements, lpaRgbValues)
# cElements : INT -> Int32
# lpaElements : INT* -> Ptr{Int32}
# lpaRgbValues : COLORREF* -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SetSysColors(
    int32_t cElements,
    int32_t* lpaElements,
    uint32_t* lpaRgbValues);
]]
local user32 = ffi.load("user32")
-- user32.SetSysColors(cElements, lpaElements, lpaRgbValues)
-- cElements : INT
-- lpaElements : INT*
-- lpaRgbValues : COLORREF*
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetSysColors = lib.func('__stdcall', 'SetSysColors', 'int32_t', ['int32_t', 'int32_t *', 'uint32_t *']);
// SetSysColors(cElements, lpaElements, lpaRgbValues)
// cElements : INT -> 'int32_t'
// lpaElements : INT* -> 'int32_t *'
// lpaRgbValues : COLORREF* -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  SetSysColors: { parameters: ["i32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.SetSysColors(cElements, lpaElements, lpaRgbValues)
// cElements : INT -> "i32"
// lpaElements : INT* -> "pointer"
// lpaRgbValues : COLORREF* -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SetSysColors(
    int32_t cElements,
    int32_t* lpaElements,
    uint32_t* lpaRgbValues);
C, "USER32.dll");
// $ffi->SetSysColors(cElements, lpaElements, lpaRgbValues);
// cElements : INT
// lpaElements : INT*
// lpaRgbValues : COLORREF*
// 構造体/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 SetSysColors(
        int cElements,   // INT
        IntByReference lpaElements,   // INT*
        IntByReference lpaRgbValues   // COLORREF*
    );
}
@[Link("user32")]
lib LibUSER32
  fun SetSysColors = SetSysColors(
    cElements : Int32,   # INT
    lpaElements : Int32*,   # INT*
    lpaRgbValues : UInt32*   # COLORREF*
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetSysColorsNative = Int32 Function(Int32, Pointer<Int32>, Pointer<Uint32>);
typedef SetSysColorsDart = int Function(int, Pointer<Int32>, Pointer<Uint32>);
final SetSysColors = DynamicLibrary.open('USER32.dll')
    .lookupFunction<SetSysColorsNative, SetSysColorsDart>('SetSysColors');
// cElements : INT -> Int32
// lpaElements : INT* -> Pointer<Int32>
// lpaRgbValues : COLORREF* -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetSysColors(
  cElements: Integer;   // INT
  lpaElements: Pointer;   // INT*
  lpaRgbValues: Pointer   // COLORREF*
): BOOL; stdcall;
  external 'USER32.dll' name 'SetSysColors';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetSysColors"
  c_SetSysColors :: Int32 -> Ptr Int32 -> Ptr Word32 -> IO CInt
-- cElements : INT -> Int32
-- lpaElements : INT* -> Ptr Int32
-- lpaRgbValues : COLORREF* -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setsyscolors =
  foreign "SetSysColors"
    (int32_t @-> (ptr int32_t) @-> (ptr uint32_t) @-> returning int32_t)
(* cElements : INT -> int32_t *)
(* lpaElements : INT* -> (ptr int32_t) *)
(* lpaRgbValues : COLORREF* -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("SetSysColors" set-sys-colors :convention :stdcall) :int32
  (c-elements :int32)   ; INT
  (lpa-elements :pointer)   ; INT*
  (lpa-rgb-values :pointer))   ; COLORREF*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetSysColors = Win32::API::More->new('USER32',
    'BOOL SetSysColors(int cElements, LPVOID lpaElements, LPVOID lpaRgbValues)');
# my $ret = $SetSysColors->Call($cElements, $lpaElements, $lpaRgbValues);
# cElements : INT -> int
# lpaElements : INT* -> LPVOID
# lpaRgbValues : COLORREF* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目