ホーム › System.Console › SetConsoleTextAttribute
SetConsoleTextAttribute
関数以降書き込まれる文字の前景色と背景色の属性を設定する。
シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL SetConsoleTextAttribute(
HANDLE hConsoleOutput,
CONSOLE_CHARACTER_ATTRIBUTES wAttributes
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hConsoleOutput | HANDLE | in | 対象のコンソール画面バッファのハンドル。 |
| wAttributes | CONSOLE_CHARACTER_ATTRIBUTES | in | 以降の書き込みに適用する文字属性(前景色・背景色等)を示すCONSOLE_CHARACTER_ATTRIBUTES値。 |
戻り値の型: BOOL
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL SetConsoleTextAttribute(
HANDLE hConsoleOutput,
CONSOLE_CHARACTER_ATTRIBUTES wAttributes
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetConsoleTextAttribute(
IntPtr hConsoleOutput, // HANDLE
ushort wAttributes // CONSOLE_CHARACTER_ATTRIBUTES
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetConsoleTextAttribute(
hConsoleOutput As IntPtr, ' HANDLE
wAttributes As UShort ' CONSOLE_CHARACTER_ATTRIBUTES
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hConsoleOutput : HANDLE
' wAttributes : CONSOLE_CHARACTER_ATTRIBUTES
Declare PtrSafe Function SetConsoleTextAttribute Lib "kernel32" ( _
ByVal hConsoleOutput As LongPtr, _
ByVal wAttributes As Integer) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
SetConsoleTextAttribute.restype = wintypes.BOOL
SetConsoleTextAttribute.argtypes = [
wintypes.HANDLE, # hConsoleOutput : HANDLE
ctypes.c_ushort, # wAttributes : CONSOLE_CHARACTER_ATTRIBUTES
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
SetConsoleTextAttribute = Fiddle::Function.new(
lib['SetConsoleTextAttribute'],
[
Fiddle::TYPE_VOIDP, # hConsoleOutput : HANDLE
-Fiddle::TYPE_SHORT, # wAttributes : CONSOLE_CHARACTER_ATTRIBUTES
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn SetConsoleTextAttribute(
hConsoleOutput: *mut core::ffi::c_void, // HANDLE
wAttributes: u16 // CONSOLE_CHARACTER_ATTRIBUTES
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, ushort wAttributes);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetConsoleTextAttribute' -Namespace Win32 -PassThru
# $api::SetConsoleTextAttribute(hConsoleOutput, wAttributes)#uselib "KERNEL32.dll"
#func global SetConsoleTextAttribute "SetConsoleTextAttribute" sptr, sptr
; SetConsoleTextAttribute hConsoleOutput, wAttributes ; 戻り値は stat
; hConsoleOutput : HANDLE -> "sptr"
; wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global SetConsoleTextAttribute "SetConsoleTextAttribute" sptr, int
; res = SetConsoleTextAttribute(hConsoleOutput, wAttributes)
; hConsoleOutput : HANDLE -> "sptr"
; wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> "int"; BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, CONSOLE_CHARACTER_ATTRIBUTES wAttributes)
#uselib "KERNEL32.dll"
#cfunc global SetConsoleTextAttribute "SetConsoleTextAttribute" intptr, int
; res = SetConsoleTextAttribute(hConsoleOutput, wAttributes)
; hConsoleOutput : HANDLE -> "intptr"
; wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
)
// hConsoleOutput (HANDLE), wAttributes (CONSOLE_CHARACTER_ATTRIBUTES)
r1, _, err := procSetConsoleTextAttribute.Call(
uintptr(hConsoleOutput),
uintptr(wAttributes),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetConsoleTextAttribute(
hConsoleOutput: THandle; // HANDLE
wAttributes: Word // CONSOLE_CHARACTER_ATTRIBUTES
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetConsoleTextAttribute';result := DllCall("KERNEL32\SetConsoleTextAttribute"
, "Ptr", hConsoleOutput ; HANDLE
, "UShort", wAttributes ; CONSOLE_CHARACTER_ATTRIBUTES
, "Int") ; return: BOOL●SetConsoleTextAttribute(hConsoleOutput, wAttributes) = DLL("KERNEL32.dll", "bool SetConsoleTextAttribute(void*, int)")
# 呼び出し: SetConsoleTextAttribute(hConsoleOutput, wAttributes)
# hConsoleOutput : HANDLE -> "void*"
# wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn SetConsoleTextAttribute(
hConsoleOutput: ?*anyopaque, // HANDLE
wAttributes: u16 // CONSOLE_CHARACTER_ATTRIBUTES
) callconv(std.os.windows.WINAPI) i32;proc SetConsoleTextAttribute(
hConsoleOutput: pointer, # HANDLE
wAttributes: uint16 # CONSOLE_CHARACTER_ATTRIBUTES
): int32 {.importc: "SetConsoleTextAttribute", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int SetConsoleTextAttribute(
void* hConsoleOutput, // HANDLE
ushort wAttributes // CONSOLE_CHARACTER_ATTRIBUTES
);ccall((:SetConsoleTextAttribute, "KERNEL32.dll"), stdcall, Int32,
(Ptr{Cvoid}, UInt16),
hConsoleOutput, wAttributes)
# hConsoleOutput : HANDLE -> Ptr{Cvoid}
# wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> UInt16
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetConsoleTextAttribute(
void* hConsoleOutput,
uint16_t wAttributes);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.SetConsoleTextAttribute(hConsoleOutput, wAttributes)
-- hConsoleOutput : HANDLE
-- wAttributes : CONSOLE_CHARACTER_ATTRIBUTES
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const SetConsoleTextAttribute = lib.func('__stdcall', 'SetConsoleTextAttribute', 'int32_t', ['void *', 'uint16_t']);
// SetConsoleTextAttribute(hConsoleOutput, wAttributes)
// hConsoleOutput : HANDLE -> 'void *'
// wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> 'uint16_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
SetConsoleTextAttribute: { parameters: ["pointer", "u16"], result: "i32" },
});
// lib.symbols.SetConsoleTextAttribute(hConsoleOutput, wAttributes)
// hConsoleOutput : HANDLE -> "pointer"
// wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> "u16"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetConsoleTextAttribute(
void* hConsoleOutput,
uint16_t wAttributes);
C, "KERNEL32.dll");
// $ffi->SetConsoleTextAttribute(hConsoleOutput, wAttributes);
// hConsoleOutput : HANDLE
// wAttributes : CONSOLE_CHARACTER_ATTRIBUTES
// 構造体/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 Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
boolean SetConsoleTextAttribute(
Pointer hConsoleOutput, // HANDLE
short wAttributes // CONSOLE_CHARACTER_ATTRIBUTES
);
}@[Link("kernel32")]
lib LibKERNEL32
fun SetConsoleTextAttribute = SetConsoleTextAttribute(
hConsoleOutput : Void*, # HANDLE
wAttributes : UInt16 # CONSOLE_CHARACTER_ATTRIBUTES
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetConsoleTextAttributeNative = Int32 Function(Pointer<Void>, Uint16);
typedef SetConsoleTextAttributeDart = int Function(Pointer<Void>, int);
final SetConsoleTextAttribute = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<SetConsoleTextAttributeNative, SetConsoleTextAttributeDart>('SetConsoleTextAttribute');
// hConsoleOutput : HANDLE -> Pointer<Void>
// wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> Uint16
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetConsoleTextAttribute(
hConsoleOutput: THandle; // HANDLE
wAttributes: Word // CONSOLE_CHARACTER_ATTRIBUTES
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetConsoleTextAttribute';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetConsoleTextAttribute"
c_SetConsoleTextAttribute :: Ptr () -> Word16 -> IO CInt
-- hConsoleOutput : HANDLE -> Ptr ()
-- wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> Word16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setconsoletextattribute =
foreign "SetConsoleTextAttribute"
((ptr void) @-> uint16_t @-> returning int32_t)
(* hConsoleOutput : HANDLE -> (ptr void) *)
(* wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> uint16_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("SetConsoleTextAttribute" set-console-text-attribute :convention :stdcall) :int32
(h-console-output :pointer) ; HANDLE
(w-attributes :uint16)) ; CONSOLE_CHARACTER_ATTRIBUTES
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetConsoleTextAttribute = Win32::API::More->new('KERNEL32',
'BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes)');
# my $ret = $SetConsoleTextAttribute->Call($hConsoleOutput, $wAttributes);
# hConsoleOutput : HANDLE -> HANDLE
# wAttributes : CONSOLE_CHARACTER_ATTRIBUTES -> WORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。