ホーム › 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 |
戻り値の型: 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 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)。