TextOutA
関数シグネチャ
// GDI32.dll (ANSI / -A)
#include <windows.h>
BOOL TextOutA(
HDC hdc,
INT x,
INT y,
LPCSTR lpString,
INT c
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hdc | HDC | in | デバイスコンテキストのハンドル。 |
| x | INT | in | システムが文字列の配置に使用する基準点の x 座標(論理座標)。 |
| y | INT | in | システムが文字列の配置に使用する基準点の y 座標(論理座標)。 |
| lpString | LPCSTR | in | 描画する文字列へのポインター。文字列の長さは cchString で指定されるため、文字列はゼロ終端されている必要はありません。 |
| c | INT | in | lpString が指す文字列の長さ(文字数単位)。 |
戻り値の型: BOOL
公式ドキュメント
TextOut 関数は、現在選択されているフォント、背景色、テキスト色を使用して、指定した位置に文字列を書き込みます。(ANSI)
戻り値
関数が成功した場合、戻り値は 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));
例
例については、Enumerating the Installed Fonts を参照してください。
wingdi.h ヘッダーは TextOut を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング中立なエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーやランタイムエラーを引き起こす不一致が発生する可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// GDI32.dll (ANSI / -A)
#include <windows.h>
BOOL TextOutA(
HDC hdc,
INT x,
INT y,
LPCSTR lpString,
INT c
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool TextOutA(
IntPtr hdc, // HDC
int x, // INT
int y, // INT
[MarshalAs(UnmanagedType.LPStr)] string lpString, // LPCSTR
int c // INT
);<DllImport("GDI32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function TextOutA(
hdc As IntPtr, ' HDC
x As Integer, ' INT
y As Integer, ' INT
<MarshalAs(UnmanagedType.LPStr)> lpString As String, ' LPCSTR
c As Integer ' INT
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hdc : HDC
' x : INT
' y : INT
' lpString : LPCSTR
' c : INT
Declare PtrSafe Function TextOutA Lib "gdi32" ( _
ByVal hdc As LongPtr, _
ByVal x As Long, _
ByVal y As Long, _
ByVal lpString As String, _
ByVal c As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
TextOutA = ctypes.windll.gdi32.TextOutA
TextOutA.restype = wintypes.BOOL
TextOutA.argtypes = [
wintypes.HANDLE, # hdc : HDC
ctypes.c_int, # x : INT
ctypes.c_int, # y : INT
wintypes.LPCSTR, # lpString : LPCSTR
ctypes.c_int, # c : INT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('GDI32.dll')
TextOutA = Fiddle::Function.new(
lib['TextOutA'],
[
Fiddle::TYPE_VOIDP, # hdc : HDC
Fiddle::TYPE_INT, # x : INT
Fiddle::TYPE_INT, # y : INT
Fiddle::TYPE_VOIDP, # lpString : LPCSTR
Fiddle::TYPE_INT, # c : INT
],
Fiddle::TYPE_INT)#[link(name = "gdi32")]
extern "system" {
fn TextOutA(
hdc: *mut core::ffi::c_void, // HDC
x: i32, // INT
y: i32, // INT
lpString: *const u8, // LPCSTR
c: i32 // INT
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", CharSet = CharSet.Ansi)]
public static extern bool TextOutA(IntPtr hdc, int x, int y, [MarshalAs(UnmanagedType.LPStr)] string lpString, int c);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_TextOutA' -Namespace Win32 -PassThru
# $api::TextOutA(hdc, x, y, lpString, c)#uselib "GDI32.dll"
#func global TextOutA "TextOutA" sptr, sptr, sptr, sptr, sptr
; TextOutA hdc, x, y, lpString, c ; 戻り値は stat
; hdc : HDC -> "sptr"
; x : INT -> "sptr"
; y : INT -> "sptr"
; lpString : LPCSTR -> "sptr"
; c : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "GDI32.dll"
#cfunc global TextOutA "TextOutA" sptr, int, int, str, int
; res = TextOutA(hdc, x, y, lpString, c)
; hdc : HDC -> "sptr"
; x : INT -> "int"
; y : INT -> "int"
; lpString : LPCSTR -> "str"
; c : INT -> "int"; BOOL TextOutA(HDC hdc, INT x, INT y, LPCSTR lpString, INT c)
#uselib "GDI32.dll"
#cfunc global TextOutA "TextOutA" intptr, int, int, str, int
; res = TextOutA(hdc, x, y, lpString, c)
; hdc : HDC -> "intptr"
; x : INT -> "int"
; y : INT -> "int"
; lpString : LPCSTR -> "str"
; c : INT -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
gdi32 = windows.NewLazySystemDLL("GDI32.dll")
procTextOutA = gdi32.NewProc("TextOutA")
)
// hdc (HDC), x (INT), y (INT), lpString (LPCSTR), c (INT)
r1, _, err := procTextOutA.Call(
uintptr(hdc),
uintptr(x),
uintptr(y),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpString))),
uintptr(c),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction TextOutA(
hdc: THandle; // HDC
x: Integer; // INT
y: Integer; // INT
lpString: PAnsiChar; // LPCSTR
c: Integer // INT
): BOOL; stdcall;
external 'GDI32.dll' name 'TextOutA';result := DllCall("GDI32\TextOutA"
, "Ptr", hdc ; HDC
, "Int", x ; INT
, "Int", y ; INT
, "AStr", lpString ; LPCSTR
, "Int", c ; INT
, "Int") ; return: BOOL●TextOutA(hdc, x, y, lpString, c) = DLL("GDI32.dll", "bool TextOutA(void*, int, int, char*, int)")
# 呼び出し: TextOutA(hdc, x, y, lpString, c)
# hdc : HDC -> "void*"
# x : INT -> "int"
# y : INT -> "int"
# lpString : LPCSTR -> "char*"
# c : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "gdi32" fn TextOutA(
hdc: ?*anyopaque, // HDC
x: i32, // INT
y: i32, // INT
lpString: [*c]const u8, // LPCSTR
c: i32 // INT
) callconv(std.os.windows.WINAPI) i32;proc TextOutA(
hdc: pointer, # HDC
x: int32, # INT
y: int32, # INT
lpString: cstring, # LPCSTR
c: int32 # INT
): int32 {.importc: "TextOutA", stdcall, dynlib: "GDI32.dll".}pragma(lib, "gdi32");
extern(Windows)
int TextOutA(
void* hdc, // HDC
int x, // INT
int y, // INT
const(char)* lpString, // LPCSTR
int c // INT
);ccall((:TextOutA, "GDI32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Int32, Int32, Cstring, Int32),
hdc, x, y, lpString, c)
# hdc : HDC -> Ptr{Cvoid}
# x : INT -> Int32
# y : INT -> Int32
# lpString : LPCSTR -> Cstring
# c : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t TextOutA(
void* hdc,
int32_t x,
int32_t y,
const char* lpString,
int32_t c);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.TextOutA(hdc, x, y, lpString, c)
-- hdc : HDC
-- x : INT
-- y : INT
-- lpString : LPCSTR
-- c : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const TextOutA = lib.func('__stdcall', 'TextOutA', 'int32_t', ['void *', 'int32_t', 'int32_t', 'str', 'int32_t']);
// TextOutA(hdc, x, y, lpString, c)
// hdc : HDC -> 'void *'
// x : INT -> 'int32_t'
// y : INT -> 'int32_t'
// lpString : LPCSTR -> 'str'
// c : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("GDI32.dll", {
TextOutA: { parameters: ["pointer", "i32", "i32", "buffer", "i32"], result: "i32" },
});
// lib.symbols.TextOutA(hdc, x, y, lpString, c)
// hdc : HDC -> "pointer"
// x : INT -> "i32"
// y : INT -> "i32"
// lpString : LPCSTR -> "buffer"
// c : INT -> "i32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t TextOutA(
void* hdc,
int32_t x,
int32_t y,
const char* lpString,
int32_t c);
C, "GDI32.dll");
// $ffi->TextOutA(hdc, x, y, lpString, c);
// hdc : HDC
// x : INT
// y : INT
// lpString : LPCSTR
// 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.ASCII_OPTIONS);
boolean TextOutA(
Pointer hdc, // HDC
int x, // INT
int y, // INT
String lpString, // LPCSTR
int c // INT
);
}@[Link("gdi32")]
lib LibGDI32
fun TextOutA = TextOutA(
hdc : Void*, # HDC
x : Int32, # INT
y : Int32, # INT
lpString : UInt8*, # LPCSTR
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 TextOutANative = Int32 Function(Pointer<Void>, Int32, Int32, Pointer<Utf8>, Int32);
typedef TextOutADart = int Function(Pointer<Void>, int, int, Pointer<Utf8>, int);
final TextOutA = DynamicLibrary.open('GDI32.dll')
.lookupFunction<TextOutANative, TextOutADart>('TextOutA');
// hdc : HDC -> Pointer<Void>
// x : INT -> Int32
// y : INT -> Int32
// lpString : LPCSTR -> Pointer<Utf8>
// c : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function TextOutA(
hdc: THandle; // HDC
x: Integer; // INT
y: Integer; // INT
lpString: PAnsiChar; // LPCSTR
c: Integer // INT
): BOOL; stdcall;
external 'GDI32.dll' name 'TextOutA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "TextOutA"
c_TextOutA :: Ptr () -> Int32 -> Int32 -> CString -> Int32 -> IO CInt
-- hdc : HDC -> Ptr ()
-- x : INT -> Int32
-- y : INT -> Int32
-- lpString : LPCSTR -> CString
-- c : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let textouta =
foreign "TextOutA"
((ptr void) @-> int32_t @-> int32_t @-> string @-> int32_t @-> returning int32_t)
(* hdc : HDC -> (ptr void) *)
(* x : INT -> int32_t *)
(* y : INT -> int32_t *)
(* lpString : LPCSTR -> string *)
(* 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 ("TextOutA" text-out-a :convention :stdcall) :int32
(hdc :pointer) ; HDC
(x :int32) ; INT
(y :int32) ; INT
(lp-string :string) ; LPCSTR
(c :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $TextOutA = Win32::API::More->new('GDI32',
'BOOL TextOutA(HANDLE hdc, int x, int y, LPCSTR lpString, int c)');
# my $ret = $TextOutA->Call($hdc, $x, $y, $lpString, $c);
# hdc : HDC -> HANDLE
# x : INT -> int
# y : INT -> int
# lpString : LPCSTR -> LPCSTR
# c : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f TextOutW (Unicode版) — 指定座標に文字列を出力する(Unicode版)。
- f GetTextAlign — DCの現在のテキスト配置設定を取得する。
- f SelectObject — デバイスコンテキストにGDIオブジェクトを選択し、同種の前のオブジェクトを返す。
- f SetBkColor — デバイスコンテキストの現在の背景色を設定する。
- f SetTextAlign — テキスト出力時の文字位置揃え方法を設定する。
- f SetTextColor — デバイスコンテキストの文字色を設定する。
- f TabbedTextOutA — タブ位置を展開して文字列を描画する(ANSI版)。