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

GetTextExtentExPointA

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

シグネチャ

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

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

パラメーター

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

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL GetTextExtentExPointA(
    HDC hdc,
    LPCSTR lpszString,
    INT cchString,
    INT nMaxExtent,
    INT* lpnFit,   // optional
    INT* lpnDx,   // optional
    SIZE* lpSize
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool GetTextExtentExPointA(
    IntPtr hdc,   // HDC
    [MarshalAs(UnmanagedType.LPStr)] string lpszString,   // LPCSTR
    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.Ansi, ExactSpelling:=True)>
Public Shared Function GetTextExtentExPointA(
    hdc As IntPtr,   ' HDC
    <MarshalAs(UnmanagedType.LPStr)> lpszString As String,   ' LPCSTR
    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 : LPCSTR
' cchString : INT
' nMaxExtent : INT
' lpnFit : INT* optional, out
' lpnDx : INT* optional, out
' lpSize : SIZE* out
Declare PtrSafe Function GetTextExtentExPointA Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal lpszString As String, _
    ByVal cchString As Long, _
    ByVal nMaxExtent As Long, _
    ByVal lpnFit As LongPtr, _
    ByVal lpnDx As LongPtr, _
    ByVal lpSize As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetTextExtentExPointA = ctypes.windll.gdi32.GetTextExtentExPointA
GetTextExtentExPointA.restype = wintypes.BOOL
GetTextExtentExPointA.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    wintypes.LPCSTR,  # lpszString : LPCSTR
    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')
GetTextExtentExPointA = Fiddle::Function.new(
  lib['GetTextExtentExPointA'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    Fiddle::TYPE_VOIDP,  # lpszString : LPCSTR
    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)
#[link(name = "gdi32")]
extern "system" {
    fn GetTextExtentExPointA(
        hdc: *mut core::ffi::c_void,  // HDC
        lpszString: *const u8,  // LPCSTR
        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.Ansi)]
public static extern bool GetTextExtentExPointA(IntPtr hdc, [MarshalAs(UnmanagedType.LPStr)] string lpszString, int cchString, int nMaxExtent, IntPtr lpnFit, IntPtr lpnDx, IntPtr lpSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_GetTextExtentExPointA' -Namespace Win32 -PassThru
# $api::GetTextExtentExPointA(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
#uselib "GDI32.dll"
#func global GetTextExtentExPointA "GetTextExtentExPointA" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; GetTextExtentExPointA hdc, lpszString, cchString, nMaxExtent, varptr(lpnFit), varptr(lpnDx), varptr(lpSize)   ; 戻り値は stat
; hdc : HDC -> "sptr"
; lpszString : LPCSTR -> "sptr"
; cchString : INT -> "sptr"
; nMaxExtent : INT -> "sptr"
; lpnFit : INT* optional, out -> "sptr"
; lpnDx : INT* optional, out -> "sptr"
; lpSize : SIZE* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "GDI32.dll"
#cfunc global GetTextExtentExPointA "GetTextExtentExPointA" sptr, str, int, int, var, var, var
; res = GetTextExtentExPointA(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
; hdc : HDC -> "sptr"
; lpszString : LPCSTR -> "str"
; cchString : INT -> "int"
; nMaxExtent : INT -> "int"
; lpnFit : INT* optional, out -> "var"
; lpnDx : INT* optional, out -> "var"
; lpSize : SIZE* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetTextExtentExPointA(HDC hdc, LPCSTR lpszString, INT cchString, INT nMaxExtent, INT* lpnFit, INT* lpnDx, SIZE* lpSize)
#uselib "GDI32.dll"
#cfunc global GetTextExtentExPointA "GetTextExtentExPointA" intptr, str, int, int, var, var, var
; res = GetTextExtentExPointA(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
; hdc : HDC -> "intptr"
; lpszString : LPCSTR -> "str"
; 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")
	procGetTextExtentExPointA = gdi32.NewProc("GetTextExtentExPointA")
)

// hdc (HDC), lpszString (LPCSTR), cchString (INT), nMaxExtent (INT), lpnFit (INT* optional, out), lpnDx (INT* optional, out), lpSize (SIZE* out)
r1, _, err := procGetTextExtentExPointA.Call(
	uintptr(hdc),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpszString))),
	uintptr(cchString),
	uintptr(nMaxExtent),
	uintptr(lpnFit),
	uintptr(lpnDx),
	uintptr(lpSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetTextExtentExPointA(
  hdc: THandle;   // HDC
  lpszString: PAnsiChar;   // LPCSTR
  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 'GetTextExtentExPointA';
result := DllCall("GDI32\GetTextExtentExPointA"
    , "Ptr", hdc   ; HDC
    , "AStr", lpszString   ; LPCSTR
    , "Int", cchString   ; INT
    , "Int", nMaxExtent   ; INT
    , "Ptr", lpnFit   ; INT* optional, out
    , "Ptr", lpnDx   ; INT* optional, out
    , "Ptr", lpSize   ; SIZE* out
    , "Int")   ; return: BOOL
●GetTextExtentExPointA(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize) = DLL("GDI32.dll", "bool GetTextExtentExPointA(void*, char*, int, int, void*, void*, void*)")
# 呼び出し: GetTextExtentExPointA(hdc, lpszString, cchString, nMaxExtent, lpnFit, lpnDx, lpSize)
# hdc : HDC -> "void*"
# lpszString : LPCSTR -> "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)。