DrawTextExA
関数シグネチャ
// USER32.dll (ANSI / -A)
#include <windows.h>
INT DrawTextExA(
HDC hdc,
LPSTR lpchText,
INT cchText,
RECT* lprc,
DRAW_TEXT_FORMAT format,
DRAWTEXTPARAMS* lpdtp // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hdc | HDC | in | 描画先のデバイスコンテキストへのハンドル。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| lpchText | LPSTR | inout | 描画するテキストを含む文字列へのポインター。cchText パラメーターが -1 の場合、文字列は null で終端されている必要があります。 dwDTFormat に DT_MODIFYSTRING が含まれている場合、この関数はこの文字列に最大 4 文字を追加することがあります。文字列を格納するバッファーは、これらの追加文字を収容できる十分な大きさである必要があります。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cchText | INT | in | lpchText が指す文字列の長さ。cchText が -1 の場合、lpchText パラメーターは null で終端された文字列へのポインターであるとみなされ、DrawTextEx は文字数を自動的に計算します。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| lprc | RECT* | inout | テキストを書式設定する矩形を論理座標で含む RECT 構造体へのポインター。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| format | DRAW_TEXT_FORMAT | in | 書式設定オプション。このパラメーターには、次の値を 1 つ以上指定できます。
| ||||||||||||||||||||||||||||||||||||||||||||||||||
| lpdtp | DRAWTEXTPARAMS* | inoptional | 追加の書式設定オプションを指定する DRAWTEXTPARAMS 構造体へのポインター。このパラメーターには NULL を指定できます。 |
戻り値の型: INT
公式ドキュメント
DrawTextEx 関数は、指定した矩形内に書式付きテキストを描画します。(ANSI)
戻り値
関数が成功した場合、戻り値はテキストの高さ(論理単位)です。DT_VCENTER または DT_BOTTOM が指定されている場合、戻り値は lprc->top から描画されたテキストの下端までのオフセットです。
関数が失敗した場合、戻り値は 0 です。
解説(Remarks)
DrawTextEx 関数は、エスケープメント(escapement)と向き(orientation)がともに 0 のフォントのみをサポートします。
デバイスコンテキストのテキスト配置モードには、TA_LEFT、TA_TOP、および TA_NOUPDATECP フラグが含まれている必要があります。
winuser.h ヘッダーは DrawTextEx を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、コンパイルエラーまたは実行時エラーを引き起こす不整合が生じる可能性があります。詳細については、関数プロトタイプの規約を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// USER32.dll (ANSI / -A)
#include <windows.h>
INT DrawTextExA(
HDC hdc,
LPSTR lpchText,
INT cchText,
RECT* lprc,
DRAW_TEXT_FORMAT format,
DRAWTEXTPARAMS* lpdtp // optional
);[DllImport("USER32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int DrawTextExA(
IntPtr hdc, // HDC
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpchText, // LPSTR in/out
int cchText, // INT
IntPtr lprc, // RECT* in/out
uint format, // DRAW_TEXT_FORMAT
IntPtr lpdtp // DRAWTEXTPARAMS* optional
);<DllImport("USER32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function DrawTextExA(
hdc As IntPtr, ' HDC
<MarshalAs(UnmanagedType.LPStr)> lpchText As System.Text.StringBuilder, ' LPSTR in/out
cchText As Integer, ' INT
lprc As IntPtr, ' RECT* in/out
format As UInteger, ' DRAW_TEXT_FORMAT
lpdtp As IntPtr ' DRAWTEXTPARAMS* optional
) As Integer
End Function' hdc : HDC
' lpchText : LPSTR in/out
' cchText : INT
' lprc : RECT* in/out
' format : DRAW_TEXT_FORMAT
' lpdtp : DRAWTEXTPARAMS* optional
Declare PtrSafe Function DrawTextExA Lib "user32" ( _
ByVal hdc As LongPtr, _
ByVal lpchText As String, _
ByVal cchText As Long, _
ByVal lprc As LongPtr, _
ByVal format As Long, _
ByVal lpdtp As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
DrawTextExA = ctypes.windll.user32.DrawTextExA
DrawTextExA.restype = ctypes.c_int
DrawTextExA.argtypes = [
wintypes.HANDLE, # hdc : HDC
wintypes.LPSTR, # lpchText : LPSTR in/out
ctypes.c_int, # cchText : INT
ctypes.c_void_p, # lprc : RECT* in/out
wintypes.DWORD, # format : DRAW_TEXT_FORMAT
ctypes.c_void_p, # lpdtp : DRAWTEXTPARAMS* optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
DrawTextExA = Fiddle::Function.new(
lib['DrawTextExA'],
[
Fiddle::TYPE_VOIDP, # hdc : HDC
Fiddle::TYPE_VOIDP, # lpchText : LPSTR in/out
Fiddle::TYPE_INT, # cchText : INT
Fiddle::TYPE_VOIDP, # lprc : RECT* in/out
-Fiddle::TYPE_INT, # format : DRAW_TEXT_FORMAT
Fiddle::TYPE_VOIDP, # lpdtp : DRAWTEXTPARAMS* optional
],
Fiddle::TYPE_INT)#[link(name = "user32")]
extern "system" {
fn DrawTextExA(
hdc: *mut core::ffi::c_void, // HDC
lpchText: *mut u8, // LPSTR in/out
cchText: i32, // INT
lprc: *mut RECT, // RECT* in/out
format: u32, // DRAW_TEXT_FORMAT
lpdtp: *mut DRAWTEXTPARAMS // DRAWTEXTPARAMS* optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Ansi)]
public static extern int DrawTextExA(IntPtr hdc, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpchText, int cchText, IntPtr lprc, uint format, IntPtr lpdtp);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_DrawTextExA' -Namespace Win32 -PassThru
# $api::DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp)#uselib "USER32.dll"
#func global DrawTextExA "DrawTextExA" sptr, sptr, sptr, sptr, sptr, sptr
; DrawTextExA hdc, varptr(lpchText), cchText, varptr(lprc), format, varptr(lpdtp) ; 戻り値は stat
; hdc : HDC -> "sptr"
; lpchText : LPSTR in/out -> "sptr"
; cchText : INT -> "sptr"
; lprc : RECT* in/out -> "sptr"
; format : DRAW_TEXT_FORMAT -> "sptr"
; lpdtp : DRAWTEXTPARAMS* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "USER32.dll" #cfunc global DrawTextExA "DrawTextExA" sptr, var, int, var, int, var ; res = DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp) ; hdc : HDC -> "sptr" ; lpchText : LPSTR in/out -> "var" ; cchText : INT -> "int" ; lprc : RECT* in/out -> "var" ; format : DRAW_TEXT_FORMAT -> "int" ; lpdtp : DRAWTEXTPARAMS* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "USER32.dll" #cfunc global DrawTextExA "DrawTextExA" sptr, sptr, int, sptr, int, sptr ; res = DrawTextExA(hdc, varptr(lpchText), cchText, varptr(lprc), format, varptr(lpdtp)) ; hdc : HDC -> "sptr" ; lpchText : LPSTR in/out -> "sptr" ; cchText : INT -> "int" ; lprc : RECT* in/out -> "sptr" ; format : DRAW_TEXT_FORMAT -> "int" ; lpdtp : DRAWTEXTPARAMS* optional -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; INT DrawTextExA(HDC hdc, LPSTR lpchText, INT cchText, RECT* lprc, DRAW_TEXT_FORMAT format, DRAWTEXTPARAMS* lpdtp) #uselib "USER32.dll" #cfunc global DrawTextExA "DrawTextExA" intptr, var, int, var, int, var ; res = DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp) ; hdc : HDC -> "intptr" ; lpchText : LPSTR in/out -> "var" ; cchText : INT -> "int" ; lprc : RECT* in/out -> "var" ; format : DRAW_TEXT_FORMAT -> "int" ; lpdtp : DRAWTEXTPARAMS* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT DrawTextExA(HDC hdc, LPSTR lpchText, INT cchText, RECT* lprc, DRAW_TEXT_FORMAT format, DRAWTEXTPARAMS* lpdtp) #uselib "USER32.dll" #cfunc global DrawTextExA "DrawTextExA" intptr, intptr, int, intptr, int, intptr ; res = DrawTextExA(hdc, varptr(lpchText), cchText, varptr(lprc), format, varptr(lpdtp)) ; hdc : HDC -> "intptr" ; lpchText : LPSTR in/out -> "intptr" ; cchText : INT -> "int" ; lprc : RECT* in/out -> "intptr" ; format : DRAW_TEXT_FORMAT -> "int" ; lpdtp : DRAWTEXTPARAMS* optional -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procDrawTextExA = user32.NewProc("DrawTextExA")
)
// hdc (HDC), lpchText (LPSTR in/out), cchText (INT), lprc (RECT* in/out), format (DRAW_TEXT_FORMAT), lpdtp (DRAWTEXTPARAMS* optional)
r1, _, err := procDrawTextExA.Call(
uintptr(hdc),
uintptr(lpchText),
uintptr(cchText),
uintptr(lprc),
uintptr(format),
uintptr(lpdtp),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction DrawTextExA(
hdc: THandle; // HDC
lpchText: PAnsiChar; // LPSTR in/out
cchText: Integer; // INT
lprc: Pointer; // RECT* in/out
format: DWORD; // DRAW_TEXT_FORMAT
lpdtp: Pointer // DRAWTEXTPARAMS* optional
): Integer; stdcall;
external 'USER32.dll' name 'DrawTextExA';result := DllCall("USER32\DrawTextExA"
, "Ptr", hdc ; HDC
, "Ptr", lpchText ; LPSTR in/out
, "Int", cchText ; INT
, "Ptr", lprc ; RECT* in/out
, "UInt", format ; DRAW_TEXT_FORMAT
, "Ptr", lpdtp ; DRAWTEXTPARAMS* optional
, "Int") ; return: INT●DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp) = DLL("USER32.dll", "int DrawTextExA(void*, char*, int, void*, dword, void*)")
# 呼び出し: DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp)
# hdc : HDC -> "void*"
# lpchText : LPSTR in/out -> "char*"
# cchText : INT -> "int"
# lprc : RECT* in/out -> "void*"
# format : DRAW_TEXT_FORMAT -> "dword"
# lpdtp : DRAWTEXTPARAMS* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "user32" fn DrawTextExA(
hdc: ?*anyopaque, // HDC
lpchText: [*c]u8, // LPSTR in/out
cchText: i32, // INT
lprc: [*c]RECT, // RECT* in/out
format: u32, // DRAW_TEXT_FORMAT
lpdtp: [*c]DRAWTEXTPARAMS // DRAWTEXTPARAMS* optional
) callconv(std.os.windows.WINAPI) i32;proc DrawTextExA(
hdc: pointer, # HDC
lpchText: ptr char, # LPSTR in/out
cchText: int32, # INT
lprc: ptr RECT, # RECT* in/out
format: uint32, # DRAW_TEXT_FORMAT
lpdtp: ptr DRAWTEXTPARAMS # DRAWTEXTPARAMS* optional
): int32 {.importc: "DrawTextExA", stdcall, dynlib: "USER32.dll".}pragma(lib, "user32");
extern(Windows)
int DrawTextExA(
void* hdc, // HDC
char* lpchText, // LPSTR in/out
int cchText, // INT
RECT* lprc, // RECT* in/out
uint format, // DRAW_TEXT_FORMAT
DRAWTEXTPARAMS* lpdtp // DRAWTEXTPARAMS* optional
);ccall((:DrawTextExA, "USER32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{UInt8}, Int32, Ptr{RECT}, UInt32, Ptr{DRAWTEXTPARAMS}),
hdc, lpchText, cchText, lprc, format, lpdtp)
# hdc : HDC -> Ptr{Cvoid}
# lpchText : LPSTR in/out -> Ptr{UInt8}
# cchText : INT -> Int32
# lprc : RECT* in/out -> Ptr{RECT}
# format : DRAW_TEXT_FORMAT -> UInt32
# lpdtp : DRAWTEXTPARAMS* optional -> Ptr{DRAWTEXTPARAMS}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t DrawTextExA(
void* hdc,
char* lpchText,
int32_t cchText,
void* lprc,
uint32_t format,
void* lpdtp);
]]
local user32 = ffi.load("user32")
-- user32.DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp)
-- hdc : HDC
-- lpchText : LPSTR in/out
-- cchText : INT
-- lprc : RECT* in/out
-- format : DRAW_TEXT_FORMAT
-- lpdtp : DRAWTEXTPARAMS* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const DrawTextExA = lib.func('__stdcall', 'DrawTextExA', 'int32_t', ['void *', 'char *', 'int32_t', 'void *', 'uint32_t', 'void *']);
// DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp)
// hdc : HDC -> 'void *'
// lpchText : LPSTR in/out -> 'char *'
// cchText : INT -> 'int32_t'
// lprc : RECT* in/out -> 'void *'
// format : DRAW_TEXT_FORMAT -> 'uint32_t'
// lpdtp : DRAWTEXTPARAMS* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
DrawTextExA: { parameters: ["pointer", "buffer", "i32", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp)
// hdc : HDC -> "pointer"
// lpchText : LPSTR in/out -> "buffer"
// cchText : INT -> "i32"
// lprc : RECT* in/out -> "pointer"
// format : DRAW_TEXT_FORMAT -> "u32"
// lpdtp : DRAWTEXTPARAMS* optional -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t DrawTextExA(
void* hdc,
char* lpchText,
int32_t cchText,
void* lprc,
uint32_t format,
void* lpdtp);
C, "USER32.dll");
// $ffi->DrawTextExA(hdc, lpchText, cchText, lprc, format, lpdtp);
// hdc : HDC
// lpchText : LPSTR in/out
// cchText : INT
// lprc : RECT* in/out
// format : DRAW_TEXT_FORMAT
// lpdtp : DRAWTEXTPARAMS* optional
// 構造体/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, W32APIOptions.ASCII_OPTIONS);
int DrawTextExA(
Pointer hdc, // HDC
byte[] lpchText, // LPSTR in/out
int cchText, // INT
Pointer lprc, // RECT* in/out
int format, // DRAW_TEXT_FORMAT
Pointer lpdtp // DRAWTEXTPARAMS* optional
);
}@[Link("user32")]
lib LibUSER32
fun DrawTextExA = DrawTextExA(
hdc : Void*, # HDC
lpchText : UInt8*, # LPSTR in/out
cchText : Int32, # INT
lprc : RECT*, # RECT* in/out
format : UInt32, # DRAW_TEXT_FORMAT
lpdtp : DRAWTEXTPARAMS* # DRAWTEXTPARAMS* optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef DrawTextExANative = Int32 Function(Pointer<Void>, Pointer<Utf8>, Int32, Pointer<Void>, Uint32, Pointer<Void>);
typedef DrawTextExADart = int Function(Pointer<Void>, Pointer<Utf8>, int, Pointer<Void>, int, Pointer<Void>);
final DrawTextExA = DynamicLibrary.open('USER32.dll')
.lookupFunction<DrawTextExANative, DrawTextExADart>('DrawTextExA');
// hdc : HDC -> Pointer<Void>
// lpchText : LPSTR in/out -> Pointer<Utf8>
// cchText : INT -> Int32
// lprc : RECT* in/out -> Pointer<Void>
// format : DRAW_TEXT_FORMAT -> Uint32
// lpdtp : DRAWTEXTPARAMS* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function DrawTextExA(
hdc: THandle; // HDC
lpchText: PAnsiChar; // LPSTR in/out
cchText: Integer; // INT
lprc: Pointer; // RECT* in/out
format: DWORD; // DRAW_TEXT_FORMAT
lpdtp: Pointer // DRAWTEXTPARAMS* optional
): Integer; stdcall;
external 'USER32.dll' name 'DrawTextExA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "DrawTextExA"
c_DrawTextExA :: Ptr () -> CString -> Int32 -> Ptr () -> Word32 -> Ptr () -> IO Int32
-- hdc : HDC -> Ptr ()
-- lpchText : LPSTR in/out -> CString
-- cchText : INT -> Int32
-- lprc : RECT* in/out -> Ptr ()
-- format : DRAW_TEXT_FORMAT -> Word32
-- lpdtp : DRAWTEXTPARAMS* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let drawtextexa =
foreign "DrawTextExA"
((ptr void) @-> string @-> int32_t @-> (ptr void) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* hdc : HDC -> (ptr void) *)
(* lpchText : LPSTR in/out -> string *)
(* cchText : INT -> int32_t *)
(* lprc : RECT* in/out -> (ptr void) *)
(* format : DRAW_TEXT_FORMAT -> uint32_t *)
(* lpdtp : DRAWTEXTPARAMS* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("DrawTextExA" draw-text-ex-a :convention :stdcall) :int32
(hdc :pointer) ; HDC
(lpch-text :pointer) ; LPSTR in/out
(cch-text :int32) ; INT
(lprc :pointer) ; RECT* in/out
(format :uint32) ; DRAW_TEXT_FORMAT
(lpdtp :pointer)) ; DRAWTEXTPARAMS* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $DrawTextExA = Win32::API::More->new('USER32',
'int DrawTextExA(HANDLE hdc, LPSTR lpchText, int cchText, LPVOID lprc, DWORD format, LPVOID lpdtp)');
# my $ret = $DrawTextExA->Call($hdc, $lpchText, $cchText, $lprc, $format, $lpdtp);
# hdc : HDC -> HANDLE
# lpchText : LPSTR in/out -> LPSTR
# cchText : INT -> int
# lprc : RECT* in/out -> LPVOID
# format : DRAW_TEXT_FORMAT -> DWORD
# lpdtp : DRAWTEXTPARAMS* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f DrawTextExW (Unicode版) — 書式オプションを指定して矩形内に文字列を描画する(Unicode版)。
- f DrawTextA — 矩形内に書式を指定して文字列を描画する(ANSI版)。