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

DrawTextW

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

シグネチャ

// USER32.dll  (Unicode / -W)
#include <windows.h>

INT DrawTextW(
    HDC hdc,
    LPCWSTR lpchText,
    INT cchText,
    RECT* lprc,
    DRAW_TEXT_FORMAT format
);

パラメーター

名前方向
hdcHDCin
lpchTextLPCWSTRinout
cchTextINTin
lprcRECT*inout
formatDRAW_TEXT_FORMATin

戻り値の型: INT

各言語での呼び出し定義

// USER32.dll  (Unicode / -W)
#include <windows.h>

INT DrawTextW(
    HDC hdc,
    LPCWSTR lpchText,
    INT cchText,
    RECT* lprc,
    DRAW_TEXT_FORMAT format
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int DrawTextW(
    IntPtr hdc,   // HDC
    [MarshalAs(UnmanagedType.LPWStr)] string lpchText,   // LPCWSTR in/out
    int cchText,   // INT
    IntPtr lprc,   // RECT* in/out
    uint format   // DRAW_TEXT_FORMAT
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DrawTextW(
    hdc As IntPtr,   ' HDC
    <MarshalAs(UnmanagedType.LPWStr)> lpchText As String,   ' LPCWSTR 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 : LPCWSTR in/out
' cchText : INT
' lprc : RECT* in/out
' format : DRAW_TEXT_FORMAT
Declare PtrSafe Function DrawTextW Lib "user32" ( _
    ByVal hdc As LongPtr, _
    ByVal lpchText As LongPtr, _
    ByVal cchText As Long, _
    ByVal lprc As LongPtr, _
    ByVal format 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

DrawTextW = ctypes.windll.user32.DrawTextW
DrawTextW.restype = ctypes.c_int
DrawTextW.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    wintypes.LPCWSTR,  # lpchText : LPCWSTR 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')
DrawTextW = Fiddle::Function.new(
  lib['DrawTextW'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    Fiddle::TYPE_VOIDP,  # lpchText : LPCWSTR in/out
    Fiddle::TYPE_INT,  # cchText : INT
    Fiddle::TYPE_VOIDP,  # lprc : RECT* in/out
    -Fiddle::TYPE_INT,  # format : DRAW_TEXT_FORMAT
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn DrawTextW(
        hdc: *mut core::ffi::c_void,  // HDC
        lpchText: *const u16,  // LPCWSTR 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.Unicode)]
public static extern int DrawTextW(IntPtr hdc, [MarshalAs(UnmanagedType.LPWStr)] string lpchText, int cchText, IntPtr lprc, uint format);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_DrawTextW' -Namespace Win32 -PassThru
# $api::DrawTextW(hdc, lpchText, cchText, lprc, format)
#uselib "USER32.dll"
#func global DrawTextW "DrawTextW" wptr, wptr, wptr, wptr, wptr
; DrawTextW hdc, lpchText, cchText, varptr(lprc), format   ; 戻り値は stat
; hdc : HDC -> "wptr"
; lpchText : LPCWSTR in/out -> "wptr"
; cchText : INT -> "wptr"
; lprc : RECT* in/out -> "wptr"
; format : DRAW_TEXT_FORMAT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global DrawTextW "DrawTextW" sptr, wstr, int, var, int
; res = DrawTextW(hdc, lpchText, cchText, lprc, format)
; hdc : HDC -> "sptr"
; lpchText : LPCWSTR in/out -> "wstr"
; cchText : INT -> "int"
; lprc : RECT* in/out -> "var"
; format : DRAW_TEXT_FORMAT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT DrawTextW(HDC hdc, LPCWSTR lpchText, INT cchText, RECT* lprc, DRAW_TEXT_FORMAT format)
#uselib "USER32.dll"
#cfunc global DrawTextW "DrawTextW" intptr, wstr, int, var, int
; res = DrawTextW(hdc, lpchText, cchText, lprc, format)
; hdc : HDC -> "intptr"
; lpchText : LPCWSTR in/out -> "wstr"
; 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")
	procDrawTextW = user32.NewProc("DrawTextW")
)

// hdc (HDC), lpchText (LPCWSTR in/out), cchText (INT), lprc (RECT* in/out), format (DRAW_TEXT_FORMAT)
r1, _, err := procDrawTextW.Call(
	uintptr(hdc),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpchText))),
	uintptr(cchText),
	uintptr(lprc),
	uintptr(format),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function DrawTextW(
  hdc: THandle;   // HDC
  lpchText: PWideChar;   // LPCWSTR in/out
  cchText: Integer;   // INT
  lprc: Pointer;   // RECT* in/out
  format: DWORD   // DRAW_TEXT_FORMAT
): Integer; stdcall;
  external 'USER32.dll' name 'DrawTextW';
result := DllCall("USER32\DrawTextW"
    , "Ptr", hdc   ; HDC
    , "WStr", lpchText   ; LPCWSTR in/out
    , "Int", cchText   ; INT
    , "Ptr", lprc   ; RECT* in/out
    , "UInt", format   ; DRAW_TEXT_FORMAT
    , "Int")   ; return: INT
●DrawTextW(hdc, lpchText, cchText, lprc, format) = DLL("USER32.dll", "int DrawTextW(void*, char*, int, void*, dword)")
# 呼び出し: DrawTextW(hdc, lpchText, cchText, lprc, format)
# hdc : HDC -> "void*"
# lpchText : LPCWSTR 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)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。