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

GdipMeasureString

関数
文字列を描画した際の境界矩形や収まる文字数を測定する。
DLLgdiplus.dll呼出規約winapi

シグネチャ

// gdiplus.dll
#include <windows.h>

Status GdipMeasureString(
    GpGraphics* graphics,
    LPCWSTR string,
    INT length,
    const GpFont* font,
    const RectF* layoutRect,
    const GpStringFormat* stringFormat,
    RectF* boundingBox,
    INT* codepointsFitted,
    INT* linesFilled
);

パラメーター

名前方向
graphicsGpGraphics*inout
stringLPCWSTRin
lengthINTin
fontGpFont*in
layoutRectRectF*in
stringFormatGpStringFormat*in
boundingBoxRectF*inout
codepointsFittedINT*inout
linesFilledINT*inout

戻り値の型: Status

各言語での呼び出し定義

// gdiplus.dll
#include <windows.h>

Status GdipMeasureString(
    GpGraphics* graphics,
    LPCWSTR string,
    INT length,
    const GpFont* font,
    const RectF* layoutRect,
    const GpStringFormat* stringFormat,
    RectF* boundingBox,
    INT* codepointsFitted,
    INT* linesFilled
);
[DllImport("gdiplus.dll", ExactSpelling = true)]
static extern int GdipMeasureString(
    IntPtr graphics,   // GpGraphics* in/out
    [MarshalAs(UnmanagedType.LPWStr)] string string,   // LPCWSTR
    int length,   // INT
    IntPtr font,   // GpFont*
    IntPtr layoutRect,   // RectF*
    IntPtr stringFormat,   // GpStringFormat*
    IntPtr boundingBox,   // RectF* in/out
    ref int codepointsFitted,   // INT* in/out
    ref int linesFilled   // INT* in/out
);
<DllImport("gdiplus.dll", ExactSpelling:=True)>
Public Shared Function GdipMeasureString(
    graphics As IntPtr,   ' GpGraphics* in/out
    <MarshalAs(UnmanagedType.LPWStr)> [string] As String,   ' LPCWSTR
    length As Integer,   ' INT
    font As IntPtr,   ' GpFont*
    layoutRect As IntPtr,   ' RectF*
    stringFormat As IntPtr,   ' GpStringFormat*
    boundingBox As IntPtr,   ' RectF* in/out
    ByRef codepointsFitted As Integer,   ' INT* in/out
    ByRef linesFilled As Integer   ' INT* in/out
) As Integer
End Function
' graphics : GpGraphics* in/out
' string : LPCWSTR
' length : INT
' font : GpFont*
' layoutRect : RectF*
' stringFormat : GpStringFormat*
' boundingBox : RectF* in/out
' codepointsFitted : INT* in/out
' linesFilled : INT* in/out
Declare PtrSafe Function GdipMeasureString Lib "gdiplus" ( _
    ByVal graphics As LongPtr, _
    ByVal string As LongPtr, _
    ByVal length As Long, _
    ByVal font As LongPtr, _
    ByVal layoutRect As LongPtr, _
    ByVal stringFormat As LongPtr, _
    ByVal boundingBox As LongPtr, _
    ByRef codepointsFitted As Long, _
    ByRef linesFilled As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GdipMeasureString = ctypes.windll.gdiplus.GdipMeasureString
GdipMeasureString.restype = ctypes.c_int
GdipMeasureString.argtypes = [
    ctypes.c_void_p,  # graphics : GpGraphics* in/out
    wintypes.LPCWSTR,  # string : LPCWSTR
    ctypes.c_int,  # length : INT
    ctypes.c_void_p,  # font : GpFont*
    ctypes.c_void_p,  # layoutRect : RectF*
    ctypes.c_void_p,  # stringFormat : GpStringFormat*
    ctypes.c_void_p,  # boundingBox : RectF* in/out
    ctypes.POINTER(ctypes.c_int),  # codepointsFitted : INT* in/out
    ctypes.POINTER(ctypes.c_int),  # linesFilled : INT* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('gdiplus.dll')
GdipMeasureString = Fiddle::Function.new(
  lib['GdipMeasureString'],
  [
    Fiddle::TYPE_VOIDP,  # graphics : GpGraphics* in/out
    Fiddle::TYPE_VOIDP,  # string : LPCWSTR
    Fiddle::TYPE_INT,  # length : INT
    Fiddle::TYPE_VOIDP,  # font : GpFont*
    Fiddle::TYPE_VOIDP,  # layoutRect : RectF*
    Fiddle::TYPE_VOIDP,  # stringFormat : GpStringFormat*
    Fiddle::TYPE_VOIDP,  # boundingBox : RectF* in/out
    Fiddle::TYPE_VOIDP,  # codepointsFitted : INT* in/out
    Fiddle::TYPE_VOIDP,  # linesFilled : INT* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "gdiplus")]
extern "system" {
    fn GdipMeasureString(
        graphics: *mut GpGraphics,  // GpGraphics* in/out
        string: *const u16,  // LPCWSTR
        length: i32,  // INT
        font: *const GpFont,  // GpFont*
        layoutRect: *const RectF,  // RectF*
        stringFormat: *const GpStringFormat,  // GpStringFormat*
        boundingBox: *mut RectF,  // RectF* in/out
        codepointsFitted: *mut i32,  // INT* in/out
        linesFilled: *mut i32  // INT* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("gdiplus.dll")]
public static extern int GdipMeasureString(IntPtr graphics, [MarshalAs(UnmanagedType.LPWStr)] string string, int length, IntPtr font, IntPtr layoutRect, IntPtr stringFormat, IntPtr boundingBox, ref int codepointsFitted, ref int linesFilled);
"@
$api = Add-Type -MemberDefinition $sig -Name 'gdiplus_GdipMeasureString' -Namespace Win32 -PassThru
# $api::GdipMeasureString(graphics, string, length, font, layoutRect, stringFormat, boundingBox, codepointsFitted, linesFilled)
#uselib "gdiplus.dll"
#func global GdipMeasureString "GdipMeasureString" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; GdipMeasureString varptr(graphics), string, length, varptr(font), varptr(layoutRect), varptr(stringFormat), varptr(boundingBox), varptr(codepointsFitted), varptr(linesFilled)   ; 戻り値は stat
; graphics : GpGraphics* in/out -> "sptr"
; string : LPCWSTR -> "sptr"
; length : INT -> "sptr"
; font : GpFont* -> "sptr"
; layoutRect : RectF* -> "sptr"
; stringFormat : GpStringFormat* -> "sptr"
; boundingBox : RectF* in/out -> "sptr"
; codepointsFitted : INT* in/out -> "sptr"
; linesFilled : INT* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "gdiplus.dll"
#cfunc global GdipMeasureString "GdipMeasureString" var, wstr, int, var, var, var, var, var, var
; res = GdipMeasureString(graphics, string, length, font, layoutRect, stringFormat, boundingBox, codepointsFitted, linesFilled)
; graphics : GpGraphics* in/out -> "var"
; string : LPCWSTR -> "wstr"
; length : INT -> "int"
; font : GpFont* -> "var"
; layoutRect : RectF* -> "var"
; stringFormat : GpStringFormat* -> "var"
; boundingBox : RectF* in/out -> "var"
; codepointsFitted : INT* in/out -> "var"
; linesFilled : INT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; Status GdipMeasureString(GpGraphics* graphics, LPCWSTR string, INT length, GpFont* font, RectF* layoutRect, GpStringFormat* stringFormat, RectF* boundingBox, INT* codepointsFitted, INT* linesFilled)
#uselib "gdiplus.dll"
#cfunc global GdipMeasureString "GdipMeasureString" var, wstr, int, var, var, var, var, var, var
; res = GdipMeasureString(graphics, string, length, font, layoutRect, stringFormat, boundingBox, codepointsFitted, linesFilled)
; graphics : GpGraphics* in/out -> "var"
; string : LPCWSTR -> "wstr"
; length : INT -> "int"
; font : GpFont* -> "var"
; layoutRect : RectF* -> "var"
; stringFormat : GpStringFormat* -> "var"
; boundingBox : RectF* in/out -> "var"
; codepointsFitted : INT* in/out -> "var"
; linesFilled : INT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdiplus = windows.NewLazySystemDLL("gdiplus.dll")
	procGdipMeasureString = gdiplus.NewProc("GdipMeasureString")
)

// graphics (GpGraphics* in/out), string (LPCWSTR), length (INT), font (GpFont*), layoutRect (RectF*), stringFormat (GpStringFormat*), boundingBox (RectF* in/out), codepointsFitted (INT* in/out), linesFilled (INT* in/out)
r1, _, err := procGdipMeasureString.Call(
	uintptr(graphics),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(string))),
	uintptr(length),
	uintptr(font),
	uintptr(layoutRect),
	uintptr(stringFormat),
	uintptr(boundingBox),
	uintptr(codepointsFitted),
	uintptr(linesFilled),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // Status
function GdipMeasureString(
  graphics: Pointer;   // GpGraphics* in/out
  string: PWideChar;   // LPCWSTR
  length: Integer;   // INT
  font: Pointer;   // GpFont*
  layoutRect: Pointer;   // RectF*
  stringFormat: Pointer;   // GpStringFormat*
  boundingBox: Pointer;   // RectF* in/out
  codepointsFitted: Pointer;   // INT* in/out
  linesFilled: Pointer   // INT* in/out
): Integer; stdcall;
  external 'gdiplus.dll' name 'GdipMeasureString';
result := DllCall("gdiplus\GdipMeasureString"
    , "Ptr", graphics   ; GpGraphics* in/out
    , "WStr", string   ; LPCWSTR
    , "Int", length   ; INT
    , "Ptr", font   ; GpFont*
    , "Ptr", layoutRect   ; RectF*
    , "Ptr", stringFormat   ; GpStringFormat*
    , "Ptr", boundingBox   ; RectF* in/out
    , "Ptr", codepointsFitted   ; INT* in/out
    , "Ptr", linesFilled   ; INT* in/out
    , "Int")   ; return: Status
●GdipMeasureString(graphics, string, length, font, layoutRect, stringFormat, boundingBox, codepointsFitted, linesFilled) = DLL("gdiplus.dll", "int GdipMeasureString(void*, char*, int, void*, void*, void*, void*, void*, void*)")
# 呼び出し: GdipMeasureString(graphics, string, length, font, layoutRect, stringFormat, boundingBox, codepointsFitted, linesFilled)
# graphics : GpGraphics* in/out -> "void*"
# string : LPCWSTR -> "char*"
# length : INT -> "int"
# font : GpFont* -> "void*"
# layoutRect : RectF* -> "void*"
# stringFormat : GpStringFormat* -> "void*"
# boundingBox : RectF* in/out -> "void*"
# codepointsFitted : INT* in/out -> "void*"
# linesFilled : INT* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。