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

GetTextExtentExPointW

関数
指定幅に収まる文字数と寸法を取得する。
DLLGDI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

BOOL GetTextExtentExPointW(
    HDC hdc,
    LPCWSTR lpszString,
    INT cchString,
    INT nMaxExtent,
    INT* lpnFit,   // optional
    INT* lpnDx,   // optional
    SIZE* lpSize
);

パラメーター

名前方向
hdcHDCin
lpszStringLPCWSTRin
cchStringINTin
nMaxExtentINTin
lpnFitINT*outoptional
lpnDxINT*outoptional
lpSizeSIZE*out

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL GetTextExtentExPointW(
    HDC hdc,
    LPCWSTR lpszString,
    INT cchString,
    INT nMaxExtent,
    INT* lpnFit,   // optional
    INT* lpnDx,   // optional
    SIZE* lpSize
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool GetTextExtentExPointW(
    IntPtr hdc,   // HDC
    [MarshalAs(UnmanagedType.LPWStr)] string lpszString,   // LPCWSTR
    int cchString,   // INT
    int nMaxExtent,   // INT
    IntPtr lpnFit,   // INT* optional, out
    IntPtr lpnDx,   // INT* optional, out
    IntPtr lpSize   // SIZE* out
);
<DllImport("GDI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function GetTextExtentExPointW(
    hdc As IntPtr,   ' HDC
    <MarshalAs(UnmanagedType.LPWStr)> lpszString As String,   ' LPCWSTR
    cchString As Integer,   ' INT
    nMaxExtent As Integer,   ' INT
    lpnFit As IntPtr,   ' INT* optional, out
    lpnDx As IntPtr,   ' INT* optional, out
    lpSize As IntPtr   ' SIZE* out
) As Boolean
End Function
' hdc : HDC
' lpszString : LPCWSTR
' cchString : INT
' nMaxExtent : INT
' lpnFit : INT* optional, out
' lpnDx : INT* optional, out
' lpSize : SIZE* out
Declare PtrSafe Function GetTextExtentExPointW Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal lpszString As LongPtr, _
    ByVal cchString As Long, _
    ByVal nMaxExtent As Long, _
    ByVal lpnFit As LongPtr, _
    ByVal lpnDx As LongPtr, _
    ByVal lpSize As LongPtr) 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

GetTextExtentExPointW = ctypes.windll.gdi32.GetTextExtentExPointW
GetTextExtentExPointW.restype = wintypes.BOOL
GetTextExtentExPointW.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    wintypes.LPCWSTR,  # lpszString : LPCWSTR
    ctypes.c_int,  # cchString : INT
    ctypes.c_int,  # nMaxExtent : INT
    ctypes.POINTER(ctypes.c_int),  # lpnFit : INT* optional, out
    ctypes.POINTER(ctypes.c_int),  # lpnDx : INT* optional, out
    ctypes.c_void_p,  # lpSize : SIZE* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
GetTextExtentExPointW = Fiddle::Function.new(
  lib['GetTextExtentExPointW'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    Fiddle::TYPE_VOIDP,  # lpszString : LPCWSTR
    Fiddle::TYPE_INT,  # cchString : INT
    Fiddle::TYPE_INT,  # nMaxExtent : INT
    Fiddle::TYPE_VOIDP,  # lpnFit : INT* optional, out
    Fiddle::TYPE_VOIDP,  # lpnDx : INT* optional, out
    Fiddle::TYPE_VOIDP,  # lpSize : SIZE* out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "gdi32")]
extern "system" {
    fn GetTextExtentExPointW(
        hdc: *mut core::ffi::c_void,  // HDC
        lpszString: *const u16,  // LPCWSTR
        cchString: i32,  // INT
        nMaxExtent: i32,  // INT
        lpnFit: *mut i32,  // INT* optional, out
        lpnDx: *mut i32,  // INT* optional, out
        lpSize: *mut SIZE  // SIZE* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetTextExtentExPointW(IntPtr hdc, [MarshalAs(UnmanagedType.LPWStr)] string lpszString, int cchString, int nMaxExtent, IntPtr lpnFit, IntPtr lpnDx, IntPtr lpSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_GetTextExtentExPointW' -Namespace Win32 -PassThru
# $api::GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
#uselib "GDI32.dll"
#func global GetTextExtentExPointW "GetTextExtentExPointW" wptr, wptr, wptr, wptr, wptr, wptr, wptr
; GetTextExtentExPointW hdc, lpszString, cchString, nMaxExtent, varptr(lpnFit), varptr(lpnDx), varptr(lpSize)   ; 戻り値は stat
; hdc : HDC -> "wptr"
; lpszString : LPCWSTR -> "wptr"
; cchString : INT -> "wptr"
; nMaxExtent : INT -> "wptr"
; lpnFit : INT* optional, out -> "wptr"
; lpnDx : INT* optional, out -> "wptr"
; lpSize : SIZE* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "GDI32.dll"
#cfunc global GetTextExtentExPointW "GetTextExtentExPointW" sptr, wstr, int, int, var, var, var
; res = GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
; hdc : HDC -> "sptr"
; lpszString : LPCWSTR -> "wstr"
; cchString : INT -> "int"
; nMaxExtent : INT -> "int"
; lpnFit : INT* optional, out -> "var"
; lpnDx : INT* optional, out -> "var"
; lpSize : SIZE* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetTextExtentExPointW(HDC hdc, LPCWSTR lpszString, INT cchString, INT nMaxExtent, INT* lpnFit, INT* lpnDx, SIZE* lpSize)
#uselib "GDI32.dll"
#cfunc global GetTextExtentExPointW "GetTextExtentExPointW" intptr, wstr, int, int, var, var, var
; res = GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
; hdc : HDC -> "intptr"
; lpszString : LPCWSTR -> "wstr"
; cchString : INT -> "int"
; nMaxExtent : INT -> "int"
; lpnFit : INT* optional, out -> "var"
; lpnDx : INT* optional, out -> "var"
; lpSize : SIZE* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procGetTextExtentExPointW = gdi32.NewProc("GetTextExtentExPointW")
)

// hdc (HDC), lpszString (LPCWSTR), cchString (INT), nMaxExtent (INT), lpnFit (INT* optional, out), lpnDx (INT* optional, out), lpSize (SIZE* out)
r1, _, err := procGetTextExtentExPointW.Call(
	uintptr(hdc),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszString))),
	uintptr(cchString),
	uintptr(nMaxExtent),
	uintptr(lpnFit),
	uintptr(lpnDx),
	uintptr(lpSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetTextExtentExPointW(
  hdc: THandle;   // HDC
  lpszString: PWideChar;   // LPCWSTR
  cchString: Integer;   // INT
  nMaxExtent: Integer;   // INT
  lpnFit: Pointer;   // INT* optional, out
  lpnDx: Pointer;   // INT* optional, out
  lpSize: Pointer   // SIZE* out
): BOOL; stdcall;
  external 'GDI32.dll' name 'GetTextExtentExPointW';
result := DllCall("GDI32\GetTextExtentExPointW"
    , "Ptr", hdc   ; HDC
    , "WStr", lpszString   ; LPCWSTR
    , "Int", cchString   ; INT
    , "Int", nMaxExtent   ; INT
    , "Ptr", lpnFit   ; INT* optional, out
    , "Ptr", lpnDx   ; INT* optional, out
    , "Ptr", lpSize   ; SIZE* out
    , "Int")   ; return: BOOL
●GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize) = DLL("GDI32.dll", "bool GetTextExtentExPointW(void*, char*, int, int, void*, void*, void*)")
# 呼び出し: GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
# hdc : HDC -> "void*"
# lpszString : LPCWSTR -> "char*"
# cchString : INT -> "int"
# nMaxExtent : INT -> "int"
# lpnFit : INT* optional, out -> "void*"
# lpnDx : INT* optional, out -> "void*"
# lpSize : SIZE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。