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