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範囲を取得する対象となる、null 終端文字列へのポインター。
cchStringINTinlpszStr パラメーターが指す文字列の文字数。ANSI 呼び出しの場合はバイト単位での文字列長を、Unicode 呼び出しの場合は WORD 単位での文字列長を指定します。ANSI 関数では、SBCS コードページの文字はそれぞれ 1 バイトですが、DBCS コードページの大半の文字は 2 バイトを占めます。Unicode 関数では、現在定義されている Unicode 文字の大半(基本多言語面(BMP)に含まれるもの)は 1 WORD ですが、Unicode サロゲートは 2 WORD になります。
nMaxExtentINTin整形された文字列に許容される最大幅(論理単位)。
lpnFitINT*outoptionalnMaxExtent パラメーターで指定された領域に収まる最大文字数を受け取る整数へのポインター。lpnFit パラメーターが NULL の場合、nMaxExtent パラメーターは無視されます。
lpnDxINT*outoptional

部分文字列の範囲を受け取る整数の配列へのポインター。配列の各要素は、文字列の先頭から、nMaxExtent パラメーターで指定された領域に収まる各文字までの距離(論理単位)を示します。この配列は内部的に全体が使用されるため、少なくとも cchString パラメーターで指定された文字数と同じ数の要素を持つ必要があります。関数は、lpnFit パラメーターで指定された文字数分だけ有効な範囲を配列に格納します。配列の残りの値は無視してください。alpDxNULL の場合、関数は部分文字列の幅を計算しません。

複雑なスクリプト(一連の文字が任意の数のグリフで表現される場合)では、alpDx 配列内の lpnFit パラメーターで指定された数までの値が、コードポイントと 1 対 1 で対応します。この場合も、alpDx 配列の残りの値は無視してください。

lpSizeSIZE*out文字列の寸法(論理単位)を受け取る SIZE 構造体へのポインター。このパラメーターを NULL にすることはできません。

戻り値の型: BOOL

公式ドキュメント

GetTextExtentExPoint 関数は、指定した領域内に収まる文字数を指定した文字列から取得し、それらの各文字のテキスト範囲(エクステント)を配列に格納します。(Unicode)

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。

解説(Remarks)

lpnFit パラメーターと alpDx パラメーターの両方が NULL の場合、GetTextExtentExPoint 関数の呼び出しは GetTextExtentPoint 関数の呼び出しと同等になります。

ANSI 版の GetTextExtentExPoint では、lpDx 配列は lpString 内のバイト数と同じ数の INT 値を持ちます。DBCS 文字の 2 バイトに対応する各 INT 値は、いずれも合成文字全体の範囲を示します。

なお、GetTextExtentExPointalpDx の値は、ExtTextOutlpDx の値とは異なります。alpDx の値を lpDx として使用するには、事前に処理を行う必要があります。

この関数はテキストの範囲を返す際、テキストが水平であること、つまりエスケープメント(escapement)が常に 0 であることを前提とします。これはテキストの水平方向と垂直方向の両方の測定に当てはまります。0 以外のエスケープメントを指定するフォントを使用していても、この関数はテキスト範囲の計算時にその角度を使用しません。アプリケーションは明示的に変換を行う必要があります。ただし、グラフィックスモードが GM_ADVANCED に設定され、文字の向きが印刷の向きから 90 度になっている場合、この関数が返す値はこの規則に従いません。ある文字列について文字の向きと印刷の向きが一致する場合、この関数は SIZE 構造体に文字列の寸法を { cx : 116, cy : 18 } として返します。同じ文字列について文字の向きと印刷の向きが 90 度異なる場合、この関数は SIZE 構造体に文字列の寸法を { cx : 18, cy : 116 } として返します。

この関数は、文字列内の連続する各文字の範囲を返します。これらを論理単位に丸めると、各文字の幅を論理単位に丸めて返す GetCharWidth から返される結果とは異なる結果が得られます。

メモ

wingdi.h ヘッダーは GetTextExtentExPoint を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在して使用すると、不一致が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規約を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// 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 <MarshalAs(UnmanagedType.Bool)> 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 版の利用を推奨。
const std = @import("std");

extern "gdi32" fn GetTextExtentExPointW(
    hdc: ?*anyopaque, // HDC
    lpszString: [*c]const u16, // LPCWSTR
    cchString: i32, // INT
    nMaxExtent: i32, // INT
    lpnFit: [*c]i32, // INT* optional, out
    lpnDx: [*c]i32, // INT* optional, out
    lpSize: [*c]SIZE // SIZE* out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetTextExtentExPointW(
    hdc: pointer,  # HDC
    lpszString: WideCString,  # LPCWSTR
    cchString: int32,  # INT
    nMaxExtent: int32,  # INT
    lpnFit: ptr int32,  # INT* optional, out
    lpnDx: ptr int32,  # INT* optional, out
    lpSize: ptr SIZE  # SIZE* out
): int32 {.importc: "GetTextExtentExPointW", stdcall, dynlib: "GDI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "gdi32");
extern(Windows)
int GetTextExtentExPointW(
    void* hdc,   // HDC
    const(wchar)* lpszString,   // LPCWSTR
    int cchString,   // INT
    int nMaxExtent,   // INT
    int* lpnFit,   // INT* optional, out
    int* lpnDx,   // INT* optional, out
    SIZE* lpSize   // SIZE* out
);
ccall((:GetTextExtentExPointW, "GDI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Cwstring, Int32, Int32, Ptr{Int32}, Ptr{Int32}, Ptr{SIZE}),
      hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
# hdc : HDC -> Ptr{Cvoid}
# lpszString : LPCWSTR -> Cwstring
# cchString : INT -> Int32
# nMaxExtent : INT -> Int32
# lpnFit : INT* optional, out -> Ptr{Int32}
# lpnDx : INT* optional, out -> Ptr{Int32}
# lpSize : SIZE* out -> Ptr{SIZE}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetTextExtentExPointW(
    void* hdc,
    const uint16_t* lpszString,
    int32_t cchString,
    int32_t nMaxExtent,
    int32_t* lpnFit,
    int32_t* lpnDx,
    void* lpSize);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
-- hdc : HDC
-- lpszString : LPCWSTR
-- cchString : INT
-- nMaxExtent : INT
-- lpnFit : INT* optional, out
-- lpnDx : INT* optional, out
-- lpSize : SIZE* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const GetTextExtentExPointW = lib.func('__stdcall', 'GetTextExtentExPointW', 'int32_t', ['void *', 'str16', 'int32_t', 'int32_t', 'int32_t *', 'int32_t *', 'void *']);
// GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
// hdc : HDC -> 'void *'
// lpszString : LPCWSTR -> 'str16'
// cchString : INT -> 'int32_t'
// nMaxExtent : INT -> 'int32_t'
// lpnFit : INT* optional, out -> 'int32_t *'
// lpnDx : INT* optional, out -> 'int32_t *'
// lpSize : SIZE* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("GDI32.dll", {
  GetTextExtentExPointW: { parameters: ["pointer", "buffer", "i32", "i32", "pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
// hdc : HDC -> "pointer"
// lpszString : LPCWSTR -> "buffer"
// cchString : INT -> "i32"
// nMaxExtent : INT -> "i32"
// lpnFit : INT* optional, out -> "pointer"
// lpnDx : INT* optional, out -> "pointer"
// lpSize : SIZE* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetTextExtentExPointW(
    void* hdc,
    const uint16_t* lpszString,
    int32_t cchString,
    int32_t nMaxExtent,
    int32_t* lpnFit,
    int32_t* lpnDx,
    void* lpSize);
C, "GDI32.dll");
// $ffi->GetTextExtentExPointW(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize);
// hdc : HDC
// lpszString : LPCWSTR
// cchString : INT
// nMaxExtent : INT
// lpnFit : INT* optional, out
// lpnDx : INT* optional, out
// lpSize : SIZE* out
// 構造体/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 Gdi32 extends StdCallLibrary {
    Gdi32 INSTANCE = Native.load("gdi32", Gdi32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean GetTextExtentExPointW(
        Pointer hdc,   // HDC
        WString lpszString,   // LPCWSTR
        int cchString,   // INT
        int nMaxExtent,   // INT
        IntByReference lpnFit,   // INT* optional, out
        IntByReference lpnDx,   // INT* optional, out
        Pointer lpSize   // SIZE* out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("gdi32")]
lib LibGDI32
  fun GetTextExtentExPointW = GetTextExtentExPointW(
    hdc : Void*,   # HDC
    lpszString : UInt16*,   # LPCWSTR
    cchString : Int32,   # INT
    nMaxExtent : Int32,   # INT
    lpnFit : Int32*,   # INT* optional, out
    lpnDx : Int32*,   # INT* optional, out
    lpSize : SIZE*   # SIZE* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetTextExtentExPointWNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Int32, Int32, Pointer<Int32>, Pointer<Int32>, Pointer<Void>);
typedef GetTextExtentExPointWDart = int Function(Pointer<Void>, Pointer<Utf16>, int, int, Pointer<Int32>, Pointer<Int32>, Pointer<Void>);
final GetTextExtentExPointW = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<GetTextExtentExPointWNative, GetTextExtentExPointWDart>('GetTextExtentExPointW');
// hdc : HDC -> Pointer<Void>
// lpszString : LPCWSTR -> Pointer<Utf16>
// cchString : INT -> Int32
// nMaxExtent : INT -> Int32
// lpnFit : INT* optional, out -> Pointer<Int32>
// lpnDx : INT* optional, out -> Pointer<Int32>
// lpSize : SIZE* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
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';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetTextExtentExPointW"
  c_GetTextExtentExPointW :: Ptr () -> CWString -> Int32 -> Int32 -> Ptr Int32 -> Ptr Int32 -> Ptr () -> IO CInt
-- hdc : HDC -> Ptr ()
-- lpszString : LPCWSTR -> CWString
-- cchString : INT -> Int32
-- nMaxExtent : INT -> Int32
-- lpnFit : INT* optional, out -> Ptr Int32
-- lpnDx : INT* optional, out -> Ptr Int32
-- lpSize : SIZE* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let gettextextentexpointw =
  foreign "GetTextExtentExPointW"
    ((ptr void) @-> (ptr uint16_t) @-> int32_t @-> int32_t @-> (ptr int32_t) @-> (ptr int32_t) @-> (ptr void) @-> returning int32_t)
(* hdc : HDC -> (ptr void) *)
(* lpszString : LPCWSTR -> (ptr uint16_t) *)
(* cchString : INT -> int32_t *)
(* nMaxExtent : INT -> int32_t *)
(* lpnFit : INT* optional, out -> (ptr int32_t) *)
(* lpnDx : INT* optional, out -> (ptr int32_t) *)
(* lpSize : SIZE* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("GetTextExtentExPointW" get-text-extent-ex-point-w :convention :stdcall) :int32
  (hdc :pointer)   ; HDC
  (lpsz-string (:string :encoding :utf-16le))   ; LPCWSTR
  (cch-string :int32)   ; INT
  (n-max-extent :int32)   ; INT
  (lpn-fit :pointer)   ; INT* optional, out
  (lpn-dx :pointer)   ; INT* optional, out
  (lp-size :pointer))   ; SIZE* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetTextExtentExPointW = Win32::API::More->new('GDI32',
    'BOOL GetTextExtentExPointW(HANDLE hdc, LPCWSTR lpszString, int cchString, int nMaxExtent, LPVOID lpnFit, LPVOID lpnDx, LPVOID lpSize)');
# my $ret = $GetTextExtentExPointW->Call($hdc, $lpszString, $cchString, $nMaxExtent, $lpnFit, $lpnDx, $lpSize);
# hdc : HDC -> HANDLE
# lpszString : LPCWSTR -> LPCWSTR
# cchString : INT -> int
# nMaxExtent : INT -> int
# lpnFit : INT* optional, out -> LPVOID
# lpnDx : INT* optional, out -> LPVOID
# lpSize : SIZE* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目
使用する型