GetTabbedTextExtentW
関数シグネチャ
// USER32.dll (Unicode / -W)
#include <windows.h>
DWORD GetTabbedTextExtentW(
HDC hdc,
LPCWSTR lpString,
INT chCount,
INT nTabPositions,
const INT* lpnTabStopPositions // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hdc | HDC | in | デバイスコンテキストのハンドルです。 |
| lpString | LPCWSTR | in | 文字列へのポインターです。 |
| chCount | INT | in | テキスト文字列の長さです。ANSI 関数では BYTE 単位、Unicode 関数では WORD 単位のカウントになります。ANSI 関数では、SBCS コードページの文字は 1 文字あたり 1 バイトですが、DBCS コードページのほとんどの文字は 2 バイトを使用します。Unicode 関数では、現在定義されているほとんどの Unicode 文字 (基本多言語面 (BMP) に含まれるもの) は 1 WORD ですが、Unicode サロゲートは 2 WORD になります。 |
| nTabPositions | INT | in | lpnTabStopPositions パラメーターが指す配列内のタブストップ位置の数です。 |
| lpnTabStopPositions | INT* | inoptional | タブストップ位置をデバイス単位で格納した配列へのポインターです。タブストップは昇順にソートされている必要があり、最小の x 値が配列の最初の要素になるようにします。 |
戻り値の型: DWORD
公式ドキュメント
GetTabbedTextExtent 関数は、文字列の幅と高さを計算します。(Unicode)
戻り値
関数が成功すると、戻り値は文字列の寸法を論理単位で表します。高さは上位ワードに、幅は下位ワードに格納されます。
関数が失敗すると、戻り値は 0 になります。GetTabbedTextExtent は、hDC が無効な場合、および nTabPositions が 0 未満の場合に失敗します。
解説(Remarks)
現在のクリッピング領域は、GetTabbedTextExtent 関数が返す幅と高さには影響しません。
一部のデバイスでは文字を規則的なセル配列に配置しない (つまりカーニングを行う) ため、文字列内の各文字のエクステントの合計が文字列全体のエクステントと等しくならない場合があります。
nTabPositions パラメーターが 0 で、lpnTabStopPositions パラメーターが NULL の場合、タブは平均文字幅の 8 倍に展開されます。
nTabPositions が 1 の場合、タブストップは lpnTabStopPositions が指す配列の最初の値で指定された間隔で区切られます。
winuser.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetTabbedTextExtent を定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、不一致が生じ、コンパイルエラーまたは実行時エラーの原因となる場合があります。詳細については、「Conventions for Function Prototypes」を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// USER32.dll (Unicode / -W)
#include <windows.h>
DWORD GetTabbedTextExtentW(
HDC hdc,
LPCWSTR lpString,
INT chCount,
INT nTabPositions,
const INT* lpnTabStopPositions // optional
);[DllImport("USER32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint GetTabbedTextExtentW(
IntPtr hdc, // HDC
[MarshalAs(UnmanagedType.LPWStr)] string lpString, // LPCWSTR
int chCount, // INT
int nTabPositions, // INT
IntPtr lpnTabStopPositions // INT* optional
);<DllImport("USER32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function GetTabbedTextExtentW(
hdc As IntPtr, ' HDC
<MarshalAs(UnmanagedType.LPWStr)> lpString As String, ' LPCWSTR
chCount As Integer, ' INT
nTabPositions As Integer, ' INT
lpnTabStopPositions As IntPtr ' INT* optional
) As UInteger
End Function' hdc : HDC
' lpString : LPCWSTR
' chCount : INT
' nTabPositions : INT
' lpnTabStopPositions : INT* optional
Declare PtrSafe Function GetTabbedTextExtentW Lib "user32" ( _
ByVal hdc As LongPtr, _
ByVal lpString As LongPtr, _
ByVal chCount As Long, _
ByVal nTabPositions As Long, _
ByVal lpnTabStopPositions 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
GetTabbedTextExtentW = ctypes.windll.user32.GetTabbedTextExtentW
GetTabbedTextExtentW.restype = wintypes.DWORD
GetTabbedTextExtentW.argtypes = [
wintypes.HANDLE, # hdc : HDC
wintypes.LPCWSTR, # lpString : LPCWSTR
ctypes.c_int, # chCount : INT
ctypes.c_int, # nTabPositions : INT
ctypes.POINTER(ctypes.c_int), # lpnTabStopPositions : INT* optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
GetTabbedTextExtentW = Fiddle::Function.new(
lib['GetTabbedTextExtentW'],
[
Fiddle::TYPE_VOIDP, # hdc : HDC
Fiddle::TYPE_VOIDP, # lpString : LPCWSTR
Fiddle::TYPE_INT, # chCount : INT
Fiddle::TYPE_INT, # nTabPositions : INT
Fiddle::TYPE_VOIDP, # lpnTabStopPositions : INT* optional
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "user32")]
extern "system" {
fn GetTabbedTextExtentW(
hdc: *mut core::ffi::c_void, // HDC
lpString: *const u16, // LPCWSTR
chCount: i32, // INT
nTabPositions: i32, // INT
lpnTabStopPositions: *const i32 // INT* optional
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Unicode)]
public static extern uint GetTabbedTextExtentW(IntPtr hdc, [MarshalAs(UnmanagedType.LPWStr)] string lpString, int chCount, int nTabPositions, IntPtr lpnTabStopPositions);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_GetTabbedTextExtentW' -Namespace Win32 -PassThru
# $api::GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions)#uselib "USER32.dll"
#func global GetTabbedTextExtentW "GetTabbedTextExtentW" wptr, wptr, wptr, wptr, wptr
; GetTabbedTextExtentW hdc, lpString, chCount, nTabPositions, varptr(lpnTabStopPositions) ; 戻り値は stat
; hdc : HDC -> "wptr"
; lpString : LPCWSTR -> "wptr"
; chCount : INT -> "wptr"
; nTabPositions : INT -> "wptr"
; lpnTabStopPositions : INT* optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "USER32.dll" #cfunc global GetTabbedTextExtentW "GetTabbedTextExtentW" sptr, wstr, int, int, var ; res = GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions) ; hdc : HDC -> "sptr" ; lpString : LPCWSTR -> "wstr" ; chCount : INT -> "int" ; nTabPositions : INT -> "int" ; lpnTabStopPositions : INT* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "USER32.dll" #cfunc global GetTabbedTextExtentW "GetTabbedTextExtentW" sptr, wstr, int, int, sptr ; res = GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, varptr(lpnTabStopPositions)) ; hdc : HDC -> "sptr" ; lpString : LPCWSTR -> "wstr" ; chCount : INT -> "int" ; nTabPositions : INT -> "int" ; lpnTabStopPositions : INT* optional -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; DWORD GetTabbedTextExtentW(HDC hdc, LPCWSTR lpString, INT chCount, INT nTabPositions, INT* lpnTabStopPositions) #uselib "USER32.dll" #cfunc global GetTabbedTextExtentW "GetTabbedTextExtentW" intptr, wstr, int, int, var ; res = GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions) ; hdc : HDC -> "intptr" ; lpString : LPCWSTR -> "wstr" ; chCount : INT -> "int" ; nTabPositions : INT -> "int" ; lpnTabStopPositions : INT* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD GetTabbedTextExtentW(HDC hdc, LPCWSTR lpString, INT chCount, INT nTabPositions, INT* lpnTabStopPositions) #uselib "USER32.dll" #cfunc global GetTabbedTextExtentW "GetTabbedTextExtentW" intptr, wstr, int, int, intptr ; res = GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, varptr(lpnTabStopPositions)) ; hdc : HDC -> "intptr" ; lpString : LPCWSTR -> "wstr" ; chCount : INT -> "int" ; nTabPositions : INT -> "int" ; lpnTabStopPositions : INT* optional -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procGetTabbedTextExtentW = user32.NewProc("GetTabbedTextExtentW")
)
// hdc (HDC), lpString (LPCWSTR), chCount (INT), nTabPositions (INT), lpnTabStopPositions (INT* optional)
r1, _, err := procGetTabbedTextExtentW.Call(
uintptr(hdc),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpString))),
uintptr(chCount),
uintptr(nTabPositions),
uintptr(lpnTabStopPositions),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction GetTabbedTextExtentW(
hdc: THandle; // HDC
lpString: PWideChar; // LPCWSTR
chCount: Integer; // INT
nTabPositions: Integer; // INT
lpnTabStopPositions: Pointer // INT* optional
): DWORD; stdcall;
external 'USER32.dll' name 'GetTabbedTextExtentW';result := DllCall("USER32\GetTabbedTextExtentW"
, "Ptr", hdc ; HDC
, "WStr", lpString ; LPCWSTR
, "Int", chCount ; INT
, "Int", nTabPositions ; INT
, "Ptr", lpnTabStopPositions ; INT* optional
, "UInt") ; return: DWORD●GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions) = DLL("USER32.dll", "dword GetTabbedTextExtentW(void*, char*, int, int, void*)")
# 呼び出し: GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions)
# hdc : HDC -> "void*"
# lpString : LPCWSTR -> "char*"
# chCount : INT -> "int"
# nTabPositions : INT -> "int"
# lpnTabStopPositions : INT* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "user32" fn GetTabbedTextExtentW(
hdc: ?*anyopaque, // HDC
lpString: [*c]const u16, // LPCWSTR
chCount: i32, // INT
nTabPositions: i32, // INT
lpnTabStopPositions: [*c]i32 // INT* optional
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc GetTabbedTextExtentW(
hdc: pointer, # HDC
lpString: WideCString, # LPCWSTR
chCount: int32, # INT
nTabPositions: int32, # INT
lpnTabStopPositions: ptr int32 # INT* optional
): uint32 {.importc: "GetTabbedTextExtentW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "user32");
extern(Windows)
uint GetTabbedTextExtentW(
void* hdc, // HDC
const(wchar)* lpString, // LPCWSTR
int chCount, // INT
int nTabPositions, // INT
int* lpnTabStopPositions // INT* optional
);ccall((:GetTabbedTextExtentW, "USER32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cwstring, Int32, Int32, Ptr{Int32}),
hdc, lpString, chCount, nTabPositions, lpnTabStopPositions)
# hdc : HDC -> Ptr{Cvoid}
# lpString : LPCWSTR -> Cwstring
# chCount : INT -> Int32
# nTabPositions : INT -> Int32
# lpnTabStopPositions : INT* optional -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t GetTabbedTextExtentW(
void* hdc,
const uint16_t* lpString,
int32_t chCount,
int32_t nTabPositions,
int32_t* lpnTabStopPositions);
]]
local user32 = ffi.load("user32")
-- user32.GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions)
-- hdc : HDC
-- lpString : LPCWSTR
-- chCount : INT
-- nTabPositions : INT
-- lpnTabStopPositions : INT* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const GetTabbedTextExtentW = lib.func('__stdcall', 'GetTabbedTextExtentW', 'uint32_t', ['void *', 'str16', 'int32_t', 'int32_t', 'int32_t *']);
// GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions)
// hdc : HDC -> 'void *'
// lpString : LPCWSTR -> 'str16'
// chCount : INT -> 'int32_t'
// nTabPositions : INT -> 'int32_t'
// lpnTabStopPositions : INT* optional -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
GetTabbedTextExtentW: { parameters: ["pointer", "buffer", "i32", "i32", "pointer"], result: "u32" },
});
// lib.symbols.GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions)
// hdc : HDC -> "pointer"
// lpString : LPCWSTR -> "buffer"
// chCount : INT -> "i32"
// nTabPositions : INT -> "i32"
// lpnTabStopPositions : INT* optional -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t GetTabbedTextExtentW(
void* hdc,
const uint16_t* lpString,
int32_t chCount,
int32_t nTabPositions,
int32_t* lpnTabStopPositions);
C, "USER32.dll");
// $ffi->GetTabbedTextExtentW(hdc, lpString, chCount, nTabPositions, lpnTabStopPositions);
// hdc : HDC
// lpString : LPCWSTR
// chCount : INT
// nTabPositions : INT
// lpnTabStopPositions : INT* optional
// 構造体/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 User32 extends StdCallLibrary {
User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.UNICODE_OPTIONS);
int GetTabbedTextExtentW(
Pointer hdc, // HDC
WString lpString, // LPCWSTR
int chCount, // INT
int nTabPositions, // INT
IntByReference lpnTabStopPositions // INT* optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("user32")]
lib LibUSER32
fun GetTabbedTextExtentW = GetTabbedTextExtentW(
hdc : Void*, # HDC
lpString : UInt16*, # LPCWSTR
chCount : Int32, # INT
nTabPositions : Int32, # INT
lpnTabStopPositions : Int32* # INT* optional
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetTabbedTextExtentWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Int32, Int32, Pointer<Int32>);
typedef GetTabbedTextExtentWDart = int Function(Pointer<Void>, Pointer<Utf16>, int, int, Pointer<Int32>);
final GetTabbedTextExtentW = DynamicLibrary.open('USER32.dll')
.lookupFunction<GetTabbedTextExtentWNative, GetTabbedTextExtentWDart>('GetTabbedTextExtentW');
// hdc : HDC -> Pointer<Void>
// lpString : LPCWSTR -> Pointer<Utf16>
// chCount : INT -> Int32
// nTabPositions : INT -> Int32
// lpnTabStopPositions : INT* optional -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetTabbedTextExtentW(
hdc: THandle; // HDC
lpString: PWideChar; // LPCWSTR
chCount: Integer; // INT
nTabPositions: Integer; // INT
lpnTabStopPositions: Pointer // INT* optional
): DWORD; stdcall;
external 'USER32.dll' name 'GetTabbedTextExtentW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetTabbedTextExtentW"
c_GetTabbedTextExtentW :: Ptr () -> CWString -> Int32 -> Int32 -> Ptr Int32 -> IO Word32
-- hdc : HDC -> Ptr ()
-- lpString : LPCWSTR -> CWString
-- chCount : INT -> Int32
-- nTabPositions : INT -> Int32
-- lpnTabStopPositions : INT* optional -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let gettabbedtextextentw =
foreign "GetTabbedTextExtentW"
((ptr void) @-> (ptr uint16_t) @-> int32_t @-> int32_t @-> (ptr int32_t) @-> returning uint32_t)
(* hdc : HDC -> (ptr void) *)
(* lpString : LPCWSTR -> (ptr uint16_t) *)
(* chCount : INT -> int32_t *)
(* nTabPositions : INT -> int32_t *)
(* lpnTabStopPositions : INT* optional -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("GetTabbedTextExtentW" get-tabbed-text-extent-w :convention :stdcall) :uint32
(hdc :pointer) ; HDC
(lp-string (:string :encoding :utf-16le)) ; LPCWSTR
(ch-count :int32) ; INT
(n-tab-positions :int32) ; INT
(lpn-tab-stop-positions :pointer)) ; INT* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetTabbedTextExtentW = Win32::API::More->new('USER32',
'DWORD GetTabbedTextExtentW(HANDLE hdc, LPCWSTR lpString, int chCount, int nTabPositions, LPVOID lpnTabStopPositions)');
# my $ret = $GetTabbedTextExtentW->Call($hdc, $lpString, $chCount, $nTabPositions, $lpnTabStopPositions);
# hdc : HDC -> HANDLE
# lpString : LPCWSTR -> LPCWSTR
# chCount : INT -> int
# nTabPositions : INT -> int
# lpnTabStopPositions : INT* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f GetTabbedTextExtentA (ANSI版) — タブを展開した文字列の表示寸法を取得する(ANSI版)。
- f GetTextExtentPoint32W — 文字列を描画した際の幅と高さを取得する。
- f TabbedTextOutW — タブ位置を展開して文字列を描画する(Unicode版)。