DrawTextW
関数シグネチャ
// USER32.dll (Unicode / -W)
#include <windows.h>
INT DrawTextW(
HDC hdc,
LPCWSTR lpchText,
INT cchText,
RECT* lprc,
DRAW_TEXT_FORMAT format
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hdc | HDC | in | デバイスコンテキストへのハンドル。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| lpchText | LPCWSTR | inout | 描画するテキストを指定する文字列へのポインター。nCount パラメーターが -1 の場合、文字列は null で終端されている必要があります。 uFormat に DT_MODIFYSTRING が含まれる場合、関数はこの文字列に最大 4 文字を追加することがあります。文字列を格納するバッファーは、これらの追加文字を収容できる十分な大きさである必要があります。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cchText | INT | in | 文字列の長さ(文字数)。nCount が -1 の場合、lpchText パラメーターは null 終端文字列へのポインターであるとみなされ、DrawText は文字数を自動的に計算します。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| lprc | RECT* | inout | テキストを書式設定する矩形(論理座標)を含む RECT 構造体へのポインター。 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| format | DRAW_TEXT_FORMAT | in | テキストの書式設定方法。このパラメーターには、次の値を 1 つ以上指定できます。
|
戻り値の型: INT
公式ドキュメント
DrawTextW (Unicode) 関数は、指定した矩形内に書式設定されたテキストを描画します。(DrawTextW 関数)
戻り値
関数が成功した場合、戻り値はテキストの高さ(論理単位)です。DT_VCENTER または DT_BOTTOM が指定されている場合、戻り値は lpRect->top から描画されたテキストの下端までのオフセットです。
関数が失敗した場合、戻り値は 0 です。
解説(Remarks)
DrawText 関数は、デバイスコンテキストで選択されているフォント、テキスト色、背景色を使用してテキストを描画します。DT_NOCLIP 書式が使用されていない限り、DrawText はテキストが指定した矩形の外に表示されないようにクリッピングします。オーバーハングが大きいテキスト、たとえばテキスト文字列の先頭の "W" や斜体のテキストは、クリッピングされる場合がある点に注意してください。DT_SINGLELINE 書式が指定されていない限り、すべての書式設定は複数行であるとみなされます。
選択されているフォントが指定した矩形に対して大きすぎる場合でも、DrawText 関数はより小さいフォントへの代替を試みません。
デバイスコンテキストのテキスト配置モードには、TA_LEFT、TA_TOP、TA_NOUPDATECP の各フラグが含まれている必要があります。
winuser.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして DrawText を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因となる場合があります。詳細については、関数プロトタイプの規約 を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 方式にも切替可。#uselib "USER32.dll" #cfunc global DrawTextW "DrawTextW" sptr, wstr, int, sptr, int ; res = DrawTextW(hdc, lpchText, cchText, varptr(lprc), format) ; hdc : HDC -> "sptr" ; lpchText : LPCWSTR in/out -> "wstr" ; cchText : INT -> "int" ; lprc : RECT* in/out -> "sptr" ; format : DRAW_TEXT_FORMAT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; INT DrawTextW(HDC hdc, LPCWSTR lpchText, INT cchText, RECT* lprc, DRAW_TEXT_FORMAT format) #uselib "USER32.dll" #cfunc global DrawTextW "DrawTextW" intptr, wstr, int, intptr, int ; res = DrawTextW(hdc, lpchText, cchText, varptr(lprc), format) ; hdc : HDC -> "intptr" ; lpchText : LPCWSTR in/out -> "wstr" ; 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")
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 // INTfunction 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 版の利用を推奨。const std = @import("std");
extern "user32" fn DrawTextW(
hdc: ?*anyopaque, // HDC
lpchText: [*c]const u16, // LPCWSTR in/out
cchText: i32, // INT
lprc: [*c]RECT, // RECT* in/out
format: u32 // DRAW_TEXT_FORMAT
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc DrawTextW(
hdc: pointer, # HDC
lpchText: WideCString, # LPCWSTR in/out
cchText: int32, # INT
lprc: ptr RECT, # RECT* in/out
format: uint32 # DRAW_TEXT_FORMAT
): int32 {.importc: "DrawTextW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "user32");
extern(Windows)
int DrawTextW(
void* hdc, // HDC
const(wchar)* lpchText, // LPCWSTR in/out
int cchText, // INT
RECT* lprc, // RECT* in/out
uint format // DRAW_TEXT_FORMAT
);ccall((:DrawTextW, "USER32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Cwstring, Int32, Ptr{RECT}, UInt32),
hdc, lpchText, cchText, lprc, format)
# hdc : HDC -> Ptr{Cvoid}
# lpchText : LPCWSTR in/out -> Cwstring
# cchText : INT -> Int32
# lprc : RECT* in/out -> Ptr{RECT}
# format : DRAW_TEXT_FORMAT -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t DrawTextW(
void* hdc,
const uint16_t* lpchText,
int32_t cchText,
void* lprc,
uint32_t format);
]]
local user32 = ffi.load("user32")
-- user32.DrawTextW(hdc, lpchText, cchText, lprc, format)
-- hdc : HDC
-- lpchText : LPCWSTR in/out
-- cchText : INT
-- lprc : RECT* in/out
-- format : DRAW_TEXT_FORMAT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const DrawTextW = lib.func('__stdcall', 'DrawTextW', 'int32_t', ['void *', 'str16', 'int32_t', 'void *', 'uint32_t']);
// DrawTextW(hdc, lpchText, cchText, lprc, format)
// hdc : HDC -> 'void *'
// lpchText : LPCWSTR in/out -> 'str16'
// 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", {
DrawTextW: { parameters: ["pointer", "buffer", "i32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.DrawTextW(hdc, lpchText, cchText, lprc, format)
// hdc : HDC -> "pointer"
// lpchText : LPCWSTR in/out -> "buffer"
// cchText : INT -> "i32"
// lprc : RECT* in/out -> "pointer"
// format : DRAW_TEXT_FORMAT -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t DrawTextW(
void* hdc,
const uint16_t* lpchText,
int32_t cchText,
void* lprc,
uint32_t format);
C, "USER32.dll");
// $ffi->DrawTextW(hdc, lpchText, cchText, lprc, format);
// hdc : HDC
// lpchText : LPCWSTR 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.UNICODE_OPTIONS);
int DrawTextW(
Pointer hdc, // HDC
WString lpchText, // LPCWSTR in/out
int cchText, // INT
Pointer lprc, // RECT* in/out
int format // DRAW_TEXT_FORMAT
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("user32")]
lib LibUSER32
fun DrawTextW = DrawTextW(
hdc : Void*, # HDC
lpchText : UInt16*, # LPCWSTR 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 DrawTextWNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Int32, Pointer<Void>, Uint32);
typedef DrawTextWDart = int Function(Pointer<Void>, Pointer<Utf16>, int, Pointer<Void>, int);
final DrawTextW = DynamicLibrary.open('USER32.dll')
.lookupFunction<DrawTextWNative, DrawTextWDart>('DrawTextW');
// hdc : HDC -> Pointer<Void>
// lpchText : LPCWSTR in/out -> Pointer<Utf16>
// cchText : INT -> Int32
// lprc : RECT* in/out -> Pointer<Void>
// format : DRAW_TEXT_FORMAT -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
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';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "DrawTextW"
c_DrawTextW :: Ptr () -> CWString -> Int32 -> Ptr () -> Word32 -> IO Int32
-- hdc : HDC -> Ptr ()
-- lpchText : LPCWSTR in/out -> CWString
-- 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 drawtextw =
foreign "DrawTextW"
((ptr void) @-> (ptr uint16_t) @-> int32_t @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* hdc : HDC -> (ptr void) *)
(* lpchText : LPCWSTR in/out -> (ptr uint16_t) *)
(* 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 ("DrawTextW" draw-text-w :convention :stdcall) :int32
(hdc :pointer) ; HDC
(lpch-text (:string :encoding :utf-16le)) ; LPCWSTR 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 $DrawTextW = Win32::API::More->new('USER32',
'int DrawTextW(HANDLE hdc, LPCWSTR lpchText, int cchText, LPVOID lprc, DWORD format)');
# my $ret = $DrawTextW->Call($hdc, $lpchText, $cchText, $lprc, $format);
# hdc : HDC -> HANDLE
# lpchText : LPCWSTR in/out -> LPCWSTR
# cchText : INT -> int
# lprc : RECT* in/out -> LPVOID
# format : DRAW_TEXT_FORMAT -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f DrawTextA (ANSI版) — 矩形内に書式を指定して文字列を描画する(ANSI版)。
- f DrawTextExW — 書式オプションを指定して矩形内に文字列を描画する(Unicode版)。
- f GrayStringW — 文字列を淡色化して指定位置に描画する(Unicode版)。
- f TabbedTextOutW — タブ位置を展開して文字列を描画する(Unicode版)。
- f TextOutW — 指定座標に文字列を出力する(Unicode版)。