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

GetCharWidthI

関数
グリフインデックスごとの文字幅を取得する。
DLLGDI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

BOOL GetCharWidthI(
    HDC hdc,
    DWORD giFirst,
    DWORD cgi,
    WORD* pgi,   // optional
    INT* piWidths
);

パラメーター

名前方向説明
hdcHDCinデバイスコンテキストへのハンドルです。
giFirstDWORDin連続するグリフインデックスのグループ内で最初のグリフインデックスです。
cgiDWORDinグリフインデックスの数です。
pgiWORD*inoptionalグリフインデックスの配列へのポインターです。このパラメーターが NULL でない場合、giFirst パラメーターの代わりに使用されます。
piWidthsINT*out幅を論理座標で受け取るバッファーへのポインターです。

戻り値の型: BOOL

公式ドキュメント

GetCharWidthI 関数は、現在のフォントから、指定された範囲の連続するグリフインデックスの幅を論理座標で取得します。

戻り値

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

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

解説(Remarks)

GetCharWidthI 関数は、pgi パラメーターが NULL の場合、連続するグリフインデックスを処理します。このとき、giFirst パラメーターが処理対象の最初のグリフインデックスを示し、cgi パラメーターが処理するグリフインデックスの数を示します。それ以外の場合、GetCharWidthI 関数は pgi パラメーターが指すグリフインデックスの配列を処理し、cgi パラメーターが処理するグリフインデックスの数を示します。

文字が現在のフォントに存在しない場合、その文字には既定の文字の幅が割り当てられます。

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

各言語での呼び出し定義

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

BOOL GetCharWidthI(
    HDC hdc,
    DWORD giFirst,
    DWORD cgi,
    WORD* pgi,   // optional
    INT* piWidths
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", ExactSpelling = true)]
static extern bool GetCharWidthI(
    IntPtr hdc,   // HDC
    uint giFirst,   // DWORD
    uint cgi,   // DWORD
    IntPtr pgi,   // WORD* optional
    out int piWidths   // INT* out
);
<DllImport("GDI32.dll", ExactSpelling:=True)>
Public Shared Function GetCharWidthI(
    hdc As IntPtr,   ' HDC
    giFirst As UInteger,   ' DWORD
    cgi As UInteger,   ' DWORD
    pgi As IntPtr,   ' WORD* optional
    <Out> ByRef piWidths As Integer   ' INT* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hdc : HDC
' giFirst : DWORD
' cgi : DWORD
' pgi : WORD* optional
' piWidths : INT* out
Declare PtrSafe Function GetCharWidthI Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal giFirst As Long, _
    ByVal cgi As Long, _
    ByVal pgi As LongPtr, _
    ByRef piWidths As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetCharWidthI = ctypes.windll.gdi32.GetCharWidthI
GetCharWidthI.restype = wintypes.BOOL
GetCharWidthI.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    wintypes.DWORD,  # giFirst : DWORD
    wintypes.DWORD,  # cgi : DWORD
    ctypes.POINTER(ctypes.c_ushort),  # pgi : WORD* optional
    ctypes.POINTER(ctypes.c_int),  # piWidths : INT* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
GetCharWidthI = Fiddle::Function.new(
  lib['GetCharWidthI'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    -Fiddle::TYPE_INT,  # giFirst : DWORD
    -Fiddle::TYPE_INT,  # cgi : DWORD
    Fiddle::TYPE_VOIDP,  # pgi : WORD* optional
    Fiddle::TYPE_VOIDP,  # piWidths : INT* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "gdi32")]
extern "system" {
    fn GetCharWidthI(
        hdc: *mut core::ffi::c_void,  // HDC
        giFirst: u32,  // DWORD
        cgi: u32,  // DWORD
        pgi: *mut u16,  // WORD* optional
        piWidths: *mut i32  // INT* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll")]
public static extern bool GetCharWidthI(IntPtr hdc, uint giFirst, uint cgi, IntPtr pgi, out int piWidths);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_GetCharWidthI' -Namespace Win32 -PassThru
# $api::GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths)
#uselib "GDI32.dll"
#func global GetCharWidthI "GetCharWidthI" sptr, sptr, sptr, sptr, sptr
; GetCharWidthI hdc, giFirst, cgi, varptr(pgi), varptr(piWidths)   ; 戻り値は stat
; hdc : HDC -> "sptr"
; giFirst : DWORD -> "sptr"
; cgi : DWORD -> "sptr"
; pgi : WORD* optional -> "sptr"
; piWidths : INT* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "GDI32.dll"
#cfunc global GetCharWidthI "GetCharWidthI" sptr, int, int, var, var
; res = GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths)
; hdc : HDC -> "sptr"
; giFirst : DWORD -> "int"
; cgi : DWORD -> "int"
; pgi : WORD* optional -> "var"
; piWidths : INT* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetCharWidthI(HDC hdc, DWORD giFirst, DWORD cgi, WORD* pgi, INT* piWidths)
#uselib "GDI32.dll"
#cfunc global GetCharWidthI "GetCharWidthI" intptr, int, int, var, var
; res = GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths)
; hdc : HDC -> "intptr"
; giFirst : DWORD -> "int"
; cgi : DWORD -> "int"
; pgi : WORD* optional -> "var"
; piWidths : INT* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procGetCharWidthI = gdi32.NewProc("GetCharWidthI")
)

// hdc (HDC), giFirst (DWORD), cgi (DWORD), pgi (WORD* optional), piWidths (INT* out)
r1, _, err := procGetCharWidthI.Call(
	uintptr(hdc),
	uintptr(giFirst),
	uintptr(cgi),
	uintptr(pgi),
	uintptr(piWidths),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetCharWidthI(
  hdc: THandle;   // HDC
  giFirst: DWORD;   // DWORD
  cgi: DWORD;   // DWORD
  pgi: Pointer;   // WORD* optional
  piWidths: Pointer   // INT* out
): BOOL; stdcall;
  external 'GDI32.dll' name 'GetCharWidthI';
result := DllCall("GDI32\GetCharWidthI"
    , "Ptr", hdc   ; HDC
    , "UInt", giFirst   ; DWORD
    , "UInt", cgi   ; DWORD
    , "Ptr", pgi   ; WORD* optional
    , "Ptr", piWidths   ; INT* out
    , "Int")   ; return: BOOL
●GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths) = DLL("GDI32.dll", "bool GetCharWidthI(void*, dword, dword, void*, void*)")
# 呼び出し: GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths)
# hdc : HDC -> "void*"
# giFirst : DWORD -> "dword"
# cgi : DWORD -> "dword"
# pgi : WORD* optional -> "void*"
# piWidths : INT* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "gdi32" fn GetCharWidthI(
    hdc: ?*anyopaque, // HDC
    giFirst: u32, // DWORD
    cgi: u32, // DWORD
    pgi: [*c]u16, // WORD* optional
    piWidths: [*c]i32 // INT* out
) callconv(std.os.windows.WINAPI) i32;
proc GetCharWidthI(
    hdc: pointer,  # HDC
    giFirst: uint32,  # DWORD
    cgi: uint32,  # DWORD
    pgi: ptr uint16,  # WORD* optional
    piWidths: ptr int32  # INT* out
): int32 {.importc: "GetCharWidthI", stdcall, dynlib: "GDI32.dll".}
pragma(lib, "gdi32");
extern(Windows)
int GetCharWidthI(
    void* hdc,   // HDC
    uint giFirst,   // DWORD
    uint cgi,   // DWORD
    ushort* pgi,   // WORD* optional
    int* piWidths   // INT* out
);
ccall((:GetCharWidthI, "GDI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32, UInt32, Ptr{UInt16}, Ptr{Int32}),
      hdc, giFirst, cgi, pgi, piWidths)
# hdc : HDC -> Ptr{Cvoid}
# giFirst : DWORD -> UInt32
# cgi : DWORD -> UInt32
# pgi : WORD* optional -> Ptr{UInt16}
# piWidths : INT* out -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetCharWidthI(
    void* hdc,
    uint32_t giFirst,
    uint32_t cgi,
    uint16_t* pgi,
    int32_t* piWidths);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths)
-- hdc : HDC
-- giFirst : DWORD
-- cgi : DWORD
-- pgi : WORD* optional
-- piWidths : INT* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const GetCharWidthI = lib.func('__stdcall', 'GetCharWidthI', 'int32_t', ['void *', 'uint32_t', 'uint32_t', 'uint16_t *', 'int32_t *']);
// GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths)
// hdc : HDC -> 'void *'
// giFirst : DWORD -> 'uint32_t'
// cgi : DWORD -> 'uint32_t'
// pgi : WORD* optional -> 'uint16_t *'
// piWidths : INT* out -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("GDI32.dll", {
  GetCharWidthI: { parameters: ["pointer", "u32", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths)
// hdc : HDC -> "pointer"
// giFirst : DWORD -> "u32"
// cgi : DWORD -> "u32"
// pgi : WORD* optional -> "pointer"
// piWidths : INT* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetCharWidthI(
    void* hdc,
    uint32_t giFirst,
    uint32_t cgi,
    uint16_t* pgi,
    int32_t* piWidths);
C, "GDI32.dll");
// $ffi->GetCharWidthI(hdc, giFirst, cgi, pgi, piWidths);
// hdc : HDC
// giFirst : DWORD
// cgi : DWORD
// pgi : WORD* optional
// piWidths : INT* 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);
    boolean GetCharWidthI(
        Pointer hdc,   // HDC
        int giFirst,   // DWORD
        int cgi,   // DWORD
        ShortByReference pgi,   // WORD* optional
        IntByReference piWidths   // INT* out
    );
}
@[Link("gdi32")]
lib LibGDI32
  fun GetCharWidthI = GetCharWidthI(
    hdc : Void*,   # HDC
    giFirst : UInt32,   # DWORD
    cgi : UInt32,   # DWORD
    pgi : UInt16*,   # WORD* optional
    piWidths : Int32*   # INT* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetCharWidthINative = Int32 Function(Pointer<Void>, Uint32, Uint32, Pointer<Uint16>, Pointer<Int32>);
typedef GetCharWidthIDart = int Function(Pointer<Void>, int, int, Pointer<Uint16>, Pointer<Int32>);
final GetCharWidthI = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<GetCharWidthINative, GetCharWidthIDart>('GetCharWidthI');
// hdc : HDC -> Pointer<Void>
// giFirst : DWORD -> Uint32
// cgi : DWORD -> Uint32
// pgi : WORD* optional -> Pointer<Uint16>
// piWidths : INT* out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetCharWidthI(
  hdc: THandle;   // HDC
  giFirst: DWORD;   // DWORD
  cgi: DWORD;   // DWORD
  pgi: Pointer;   // WORD* optional
  piWidths: Pointer   // INT* out
): BOOL; stdcall;
  external 'GDI32.dll' name 'GetCharWidthI';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetCharWidthI"
  c_GetCharWidthI :: Ptr () -> Word32 -> Word32 -> Ptr Word16 -> Ptr Int32 -> IO CInt
-- hdc : HDC -> Ptr ()
-- giFirst : DWORD -> Word32
-- cgi : DWORD -> Word32
-- pgi : WORD* optional -> Ptr Word16
-- piWidths : INT* out -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getcharwidthi =
  foreign "GetCharWidthI"
    ((ptr void) @-> uint32_t @-> uint32_t @-> (ptr uint16_t) @-> (ptr int32_t) @-> returning int32_t)
(* hdc : HDC -> (ptr void) *)
(* giFirst : DWORD -> uint32_t *)
(* cgi : DWORD -> uint32_t *)
(* pgi : WORD* optional -> (ptr uint16_t) *)
(* piWidths : INT* out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("GetCharWidthI" get-char-width-i :convention :stdcall) :int32
  (hdc :pointer)   ; HDC
  (gi-first :uint32)   ; DWORD
  (cgi :uint32)   ; DWORD
  (pgi :pointer)   ; WORD* optional
  (pi-widths :pointer))   ; INT* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetCharWidthI = Win32::API::More->new('GDI32',
    'BOOL GetCharWidthI(HANDLE hdc, DWORD giFirst, DWORD cgi, LPVOID pgi, LPVOID piWidths)');
# my $ret = $GetCharWidthI->Call($hdc, $giFirst, $cgi, $pgi, $piWidths);
# hdc : HDC -> HANDLE
# giFirst : DWORD -> DWORD
# cgi : DWORD -> DWORD
# pgi : WORD* optional -> LPVOID
# piWidths : INT* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目