Win32 API 日本語リファレンス
ホームGraphics.Gdi › DrawTextA

DrawTextA

関数
矩形内に書式を指定して文字列を描画する(ANSI版)。
DLLUSER32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// USER32.dll  (ANSI / -A)
#include <windows.h>

INT DrawTextA(
    HDC hdc,
    LPCSTR lpchText,
    INT cchText,
    RECT* lprc,
    DRAW_TEXT_FORMAT format
);

パラメーター

名前方向
hdcHDCin
lpchTextLPCSTRinout
cchTextINTin
lprcRECT*inout
formatDRAW_TEXT_FORMATin

戻り値の型: INT

各言語での呼び出し定義

// 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 方式にも切替可。
出力引数:
; 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 方式にも切替可。
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   // INT
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';
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)。