TextOutW
関数シグネチャ
// GDI32.dll (Unicode / -W)
#include <windows.h>
BOOL TextOutW(
HDC hdc,
INT x,
INT y,
LPCWSTR lpString,
INT c
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hdc | HDC | in | デバイスコンテキストへのハンドル。 |
| x | INT | in | システムが文字列の配置に使用する基準点の x 座標(論理座標)。 |
| y | INT | in | システムが文字列の配置に使用する基準点の y 座標(論理座標)。 |
| lpString | LPCWSTR | in | 描画する文字列へのポインター。cchString が文字列の長さを指定するため、文字列はゼロ終端されている必要はありません。 |
| c | INT | in | lpString が指す文字列の長さ(文字数)。 |
戻り値の型: BOOL
公式ドキュメント
TextOut 関数は、現在選択されているフォント、背景色、テキスト色を使用して、指定した位置に文字列を描画します。(Unicode)
戻り値
関数が成功した場合、戻り値は 0 以外の値です。
関数が失敗した場合、戻り値は 0 です。
解説(Remarks)
基準点の解釈は、現在のテキスト配置モードに依存します。アプリケーションは GetTextAlign 関数を呼び出してこのモードを取得でき、SetTextAlign 関数を呼び出してこのモードを変更できます。テキスト配置には次の値を使用できます。水平配置と垂直配置に影響するフラグからは、それぞれ 1 つのみを選択できます。さらに、現在位置を変更する 2 つのフラグからも、いずれか 1 つのみを選択できます。
| 用語 | 説明 |
|---|---|
| TA_BASELINE | 基準点はテキストのベースライン上に置かれます。 |
| TA_BOTTOM | 基準点は外接矩形の下端に置かれます。 |
| TA_TOP | 基準点は外接矩形の上端に置かれます。 |
| TA_CENTER | 基準点は外接矩形の中央に水平方向に揃えられます。 |
| TA_LEFT | 基準点は外接矩形の左端に置かれます。 |
| TA_RIGHT | 基準点は外接矩形の右端に置かれます。 |
| TA_NOUPDATECP | テキスト出力呼び出しのたびに現在位置は更新されません。基準点はテキスト出力関数に渡されます。 |
| TA_RTLREADING | Windows の中東言語版: テキストは既定の左から右への順序ではなく、右から左への読み取り順序でレイアウトされます。これは、デバイスコンテキストに選択されたフォントがヘブライ語またはアラビア語の場合にのみ適用されます。 |
| TA_UPDATECP | テキスト出力呼び出しのたびに現在位置が更新されます。現在位置が基準点として使用されます。 |
既定では、この関数は現在位置を使用も更新もしません。ただし、アプリケーションが fMode パラメーターに TA_UPDATECP を設定して SetTextAlign 関数を呼び出すと、指定したデバイスコンテキストに対してアプリケーションが TextOut を呼び出すたびに、システムが現在位置を使用および更新するようになります。このフラグが設定されている場合、システムは以降の TextOut 呼び出しで nXStart パラメーターと nYStart パラメーターを無視します。
TextOut 関数をパスブラケット内に配置すると、システムは各文字とその文字ボックスを含む TrueType テキストのパスを生成します。生成される領域は、テキスト自体ではなく、文字ボックスからテキストを除いた部分です。TextOut 関数をパスブラケット内に配置する前に背景モードを透過に設定することで、TrueType テキストのアウトラインで囲まれた領域を取得できます。次に、この手順を示すサンプルコードを示します。
// ウィンドウのクライアント矩形を取得する
GetClientRect(hwnd, &r);
// 修正方法: 背景モードを透過に設定することで、
// 領域がテキスト自体になる
// SetBkMode(hdc, TRANSPARENT);
// パスを開始するブラケット
BeginPath(hdc);
// テキストを出力する
TCHAR text[ ] = "Defenestration can be hazardous";
TextOut(hdc,r.left,r.top,text, ARRAYSIZE(text));
// パスを終了するブラケット
EndPath(hdc);
// そのパスから領域を導出する
SelectClipPath(hdc, RGN_AND);
// これは SelectClipPath() と同じ結果を生成する
// SelectClipRgn(hdc, PathToRegion(hdc));
// 領域をグレーで塗りつぶす
FillRect(hdc, &r, GetStockObject(GRAY_BRUSH));
例
例については、インストールされているフォントの列挙を参照してください。
wingdi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして TextOut を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規約を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// GDI32.dll (Unicode / -W)
#include <windows.h>
BOOL TextOutW(
HDC hdc,
INT x,
INT y,
LPCWSTR lpString,
INT c
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool TextOutW(
IntPtr hdc, // HDC
int x, // INT
int y, // INT
[MarshalAs(UnmanagedType.LPWStr)] string lpString, // LPCWSTR
int c // INT
);<DllImport("GDI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function TextOutW(
hdc As IntPtr, ' HDC
x As Integer, ' INT
y As Integer, ' INT
<MarshalAs(UnmanagedType.LPWStr)> lpString As String, ' LPCWSTR
c As Integer ' INT
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hdc : HDC
' x : INT
' y : INT
' lpString : LPCWSTR
' c : INT
Declare PtrSafe Function TextOutW Lib "gdi32" ( _
ByVal hdc As LongPtr, _
ByVal x As Long, _
ByVal y As Long, _
ByVal lpString As LongPtr, _
ByVal c As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
TextOutW = ctypes.windll.gdi32.TextOutW
TextOutW.restype = wintypes.BOOL
TextOutW.argtypes = [
wintypes.HANDLE, # hdc : HDC
ctypes.c_int, # x : INT
ctypes.c_int, # y : INT
wintypes.LPCWSTR, # lpString : LPCWSTR
ctypes.c_int, # c : INT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('GDI32.dll')
TextOutW = Fiddle::Function.new(
lib['TextOutW'],
[
Fiddle::TYPE_VOIDP, # hdc : HDC
Fiddle::TYPE_INT, # x : INT
Fiddle::TYPE_INT, # y : INT
Fiddle::TYPE_VOIDP, # lpString : LPCWSTR
Fiddle::TYPE_INT, # c : INT
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "gdi32")]
extern "system" {
fn TextOutW(
hdc: *mut core::ffi::c_void, // HDC
x: i32, // INT
y: i32, // INT
lpString: *const u16, // LPCWSTR
c: i32 // INT
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", CharSet = CharSet.Unicode)]
public static extern bool TextOutW(IntPtr hdc, int x, int y, [MarshalAs(UnmanagedType.LPWStr)] string lpString, int c);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_TextOutW' -Namespace Win32 -PassThru
# $api::TextOutW(hdc, x, y, lpString, c)#uselib "GDI32.dll"
#func global TextOutW "TextOutW" wptr, wptr, wptr, wptr, wptr
; TextOutW hdc, x, y, lpString, c ; 戻り値は stat
; hdc : HDC -> "wptr"
; x : INT -> "wptr"
; y : INT -> "wptr"
; lpString : LPCWSTR -> "wptr"
; c : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "GDI32.dll"
#cfunc global TextOutW "TextOutW" sptr, int, int, wstr, int
; res = TextOutW(hdc, x, y, lpString, c)
; hdc : HDC -> "sptr"
; x : INT -> "int"
; y : INT -> "int"
; lpString : LPCWSTR -> "wstr"
; c : INT -> "int"; BOOL TextOutW(HDC hdc, INT x, INT y, LPCWSTR lpString, INT c)
#uselib "GDI32.dll"
#cfunc global TextOutW "TextOutW" intptr, int, int, wstr, int
; res = TextOutW(hdc, x, y, lpString, c)
; hdc : HDC -> "intptr"
; x : INT -> "int"
; y : INT -> "int"
; lpString : LPCWSTR -> "wstr"
; c : INT -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
gdi32 = windows.NewLazySystemDLL("GDI32.dll")
procTextOutW = gdi32.NewProc("TextOutW")
)
// hdc (HDC), x (INT), y (INT), lpString (LPCWSTR), c (INT)
r1, _, err := procTextOutW.Call(
uintptr(hdc),
uintptr(x),
uintptr(y),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString))),
uintptr(c),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction TextOutW(
hdc: THandle; // HDC
x: Integer; // INT
y: Integer; // INT
lpString: PWideChar; // LPCWSTR
c: Integer // INT
): BOOL; stdcall;
external 'GDI32.dll' name 'TextOutW';result := DllCall("GDI32\TextOutW"
, "Ptr", hdc ; HDC
, "Int", x ; INT
, "Int", y ; INT
, "WStr", lpString ; LPCWSTR
, "Int", c ; INT
, "Int") ; return: BOOL●TextOutW(hdc, x, y, lpString, c) = DLL("GDI32.dll", "bool TextOutW(void*, int, int, char*, int)")
# 呼び出し: TextOutW(hdc, x, y, lpString, c)
# hdc : HDC -> "void*"
# x : INT -> "int"
# y : INT -> "int"
# lpString : LPCWSTR -> "char*"
# c : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "gdi32" fn TextOutW(
hdc: ?*anyopaque, // HDC
x: i32, // INT
y: i32, // INT
lpString: [*c]const u16, // LPCWSTR
c: i32 // INT
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc TextOutW(
hdc: pointer, # HDC
x: int32, # INT
y: int32, # INT
lpString: WideCString, # LPCWSTR
c: int32 # INT
): int32 {.importc: "TextOutW", stdcall, dynlib: "GDI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "gdi32");
extern(Windows)
int TextOutW(
void* hdc, // HDC
int x, // INT
int y, // INT
const(wchar)* lpString, // LPCWSTR
int c // INT
);ccall((:TextOutW, "GDI32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Int32, Int32, Cwstring, Int32),
hdc, x, y, lpString, c)
# hdc : HDC -> Ptr{Cvoid}
# x : INT -> Int32
# y : INT -> Int32
# lpString : LPCWSTR -> Cwstring
# c : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t TextOutW(
void* hdc,
int32_t x,
int32_t y,
const uint16_t* lpString,
int32_t c);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.TextOutW(hdc, x, y, lpString, c)
-- hdc : HDC
-- x : INT
-- y : INT
-- lpString : LPCWSTR
-- c : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const TextOutW = lib.func('__stdcall', 'TextOutW', 'int32_t', ['void *', 'int32_t', 'int32_t', 'str16', 'int32_t']);
// TextOutW(hdc, x, y, lpString, c)
// hdc : HDC -> 'void *'
// x : INT -> 'int32_t'
// y : INT -> 'int32_t'
// lpString : LPCWSTR -> 'str16'
// c : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("GDI32.dll", {
TextOutW: { parameters: ["pointer", "i32", "i32", "buffer", "i32"], result: "i32" },
});
// lib.symbols.TextOutW(hdc, x, y, lpString, c)
// hdc : HDC -> "pointer"
// x : INT -> "i32"
// y : INT -> "i32"
// lpString : LPCWSTR -> "buffer"
// c : INT -> "i32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t TextOutW(
void* hdc,
int32_t x,
int32_t y,
const uint16_t* lpString,
int32_t c);
C, "GDI32.dll");
// $ffi->TextOutW(hdc, x, y, lpString, c);
// hdc : HDC
// x : INT
// y : INT
// lpString : LPCWSTR
// c : INT
// 構造体/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 Gdi32 extends StdCallLibrary {
Gdi32 INSTANCE = Native.load("gdi32", Gdi32.class, W32APIOptions.UNICODE_OPTIONS);
boolean TextOutW(
Pointer hdc, // HDC
int x, // INT
int y, // INT
WString lpString, // LPCWSTR
int c // INT
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("gdi32")]
lib LibGDI32
fun TextOutW = TextOutW(
hdc : Void*, # HDC
x : Int32, # INT
y : Int32, # INT
lpString : UInt16*, # LPCWSTR
c : Int32 # INT
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef TextOutWNative = Int32 Function(Pointer<Void>, Int32, Int32, Pointer<Utf16>, Int32);
typedef TextOutWDart = int Function(Pointer<Void>, int, int, Pointer<Utf16>, int);
final TextOutW = DynamicLibrary.open('GDI32.dll')
.lookupFunction<TextOutWNative, TextOutWDart>('TextOutW');
// hdc : HDC -> Pointer<Void>
// x : INT -> Int32
// y : INT -> Int32
// lpString : LPCWSTR -> Pointer<Utf16>
// c : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function TextOutW(
hdc: THandle; // HDC
x: Integer; // INT
y: Integer; // INT
lpString: PWideChar; // LPCWSTR
c: Integer // INT
): BOOL; stdcall;
external 'GDI32.dll' name 'TextOutW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "TextOutW"
c_TextOutW :: Ptr () -> Int32 -> Int32 -> CWString -> Int32 -> IO CInt
-- hdc : HDC -> Ptr ()
-- x : INT -> Int32
-- y : INT -> Int32
-- lpString : LPCWSTR -> CWString
-- c : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let textoutw =
foreign "TextOutW"
((ptr void) @-> int32_t @-> int32_t @-> (ptr uint16_t) @-> int32_t @-> returning int32_t)
(* hdc : HDC -> (ptr void) *)
(* x : INT -> int32_t *)
(* y : INT -> int32_t *)
(* lpString : LPCWSTR -> (ptr uint16_t) *)
(* c : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)
(cffi:defcfun ("TextOutW" text-out-w :convention :stdcall) :int32
(hdc :pointer) ; HDC
(x :int32) ; INT
(y :int32) ; INT
(lp-string (:string :encoding :utf-16le)) ; LPCWSTR
(c :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $TextOutW = Win32::API::More->new('GDI32',
'BOOL TextOutW(HANDLE hdc, int x, int y, LPCWSTR lpString, int c)');
# my $ret = $TextOutW->Call($hdc, $x, $y, $lpString, $c);
# hdc : HDC -> HANDLE
# x : INT -> int
# y : INT -> int
# lpString : LPCWSTR -> LPCWSTR
# c : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f TextOutA (ANSI版) — 指定座標に文字列を出力する(ANSI版)。
- f GetTextAlign — DCの現在のテキスト配置設定を取得する。
- f SelectObject — デバイスコンテキストにGDIオブジェクトを選択し、同種の前のオブジェクトを返す。
- f SetBkColor — デバイスコンテキストの現在の背景色を設定する。
- f SetTextAlign — テキスト出力時の文字位置揃え方法を設定する。
- f SetTextColor — デバイスコンテキストの文字色を設定する。
- f TabbedTextOutW — タブ位置を展開して文字列を描画する(Unicode版)。