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

GetTextFaceW

関数
現在選択中のフォントの書体名を取得する(Unicode版)。
DLLGDI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// GDI32.dll  (Unicode / -W)
#include <windows.h>

INT GetTextFaceW(
    HDC hdc,
    INT c,
    LPWSTR lpName   // optional
);

パラメーター

名前方向説明
hdcHDCinデバイスコンテキストへのハンドル。
cINTinlpFaceName が指すバッファーの長さ。ANSI 関数では BYTE 単位、Unicode 関数では WORD 単位の値です。なお、ANSI 関数では SBCS コードページの文字は 1 文字あたり 1 バイトですが、DBCS コードページのほとんどの文字は 2 バイトを使用します。Unicode 関数では、現在定義されているほとんどの Unicode 文字(基本多言語面 (BMP) に含まれるもの)は 1 WORD ですが、Unicode サロゲートは 2 WORD になります。
lpNameLPWSTRoutoptional書体名を受け取るバッファーへのポインター。このパラメーターが NULL の場合、関数は終端の null 文字を含む名前の文字数を返します。

戻り値の型: INT

公式ドキュメント

GetTextFace 関数は、指定したデバイスコンテキストに選択されているフォントの書体名を取得します。(Unicode)

戻り値

関数が成功すると、戻り値はバッファーにコピーされた文字数です。

関数が失敗すると、戻り値は 0 です。

解説(Remarks)

書体名は null で終わる文字列としてコピーされます。

名前が nCount パラメーターで指定された文字数より長い場合、名前は切り詰められます。

メモ

wingdi.h ヘッダーは、GetTextFace を、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>

INT GetTextFaceW(
    HDC hdc,
    INT c,
    LPWSTR lpName   // optional
);
[DllImport("GDI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int GetTextFaceW(
    IntPtr hdc,   // HDC
    int c,   // INT
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpName   // LPWSTR optional, out
);
<DllImport("GDI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function GetTextFaceW(
    hdc As IntPtr,   ' HDC
    c As Integer,   ' INT
    <MarshalAs(UnmanagedType.LPWStr)> lpName As System.Text.StringBuilder   ' LPWSTR optional, out
) As Integer
End Function
' hdc : HDC
' c : INT
' lpName : LPWSTR optional, out
Declare PtrSafe Function GetTextFaceW Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal c As Long, _
    ByVal lpName 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

GetTextFaceW = ctypes.windll.gdi32.GetTextFaceW
GetTextFaceW.restype = ctypes.c_int
GetTextFaceW.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    ctypes.c_int,  # c : INT
    wintypes.LPWSTR,  # lpName : LPWSTR optional, out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
GetTextFaceW = Fiddle::Function.new(
  lib['GetTextFaceW'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    Fiddle::TYPE_INT,  # c : INT
    Fiddle::TYPE_VOIDP,  # lpName : LPWSTR optional, out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "gdi32")]
extern "system" {
    fn GetTextFaceW(
        hdc: *mut core::ffi::c_void,  // HDC
        c: i32,  // INT
        lpName: *mut u16  // LPWSTR optional, out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("GDI32.dll", CharSet = CharSet.Unicode)]
public static extern int GetTextFaceW(IntPtr hdc, int c, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_GetTextFaceW' -Namespace Win32 -PassThru
# $api::GetTextFaceW(hdc, c, lpName)
#uselib "GDI32.dll"
#func global GetTextFaceW "GetTextFaceW" wptr, wptr, wptr
; GetTextFaceW hdc, c, varptr(lpName)   ; 戻り値は stat
; hdc : HDC -> "wptr"
; c : INT -> "wptr"
; lpName : LPWSTR optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "GDI32.dll"
#cfunc global GetTextFaceW "GetTextFaceW" sptr, int, var
; res = GetTextFaceW(hdc, c, lpName)
; hdc : HDC -> "sptr"
; c : INT -> "int"
; lpName : LPWSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT GetTextFaceW(HDC hdc, INT c, LPWSTR lpName)
#uselib "GDI32.dll"
#cfunc global GetTextFaceW "GetTextFaceW" intptr, int, var
; res = GetTextFaceW(hdc, c, lpName)
; hdc : HDC -> "intptr"
; c : INT -> "int"
; lpName : LPWSTR optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procGetTextFaceW = gdi32.NewProc("GetTextFaceW")
)

// hdc (HDC), c (INT), lpName (LPWSTR optional, out)
r1, _, err := procGetTextFaceW.Call(
	uintptr(hdc),
	uintptr(c),
	uintptr(lpName),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function GetTextFaceW(
  hdc: THandle;   // HDC
  c: Integer;   // INT
  lpName: PWideChar   // LPWSTR optional, out
): Integer; stdcall;
  external 'GDI32.dll' name 'GetTextFaceW';
result := DllCall("GDI32\GetTextFaceW"
    , "Ptr", hdc   ; HDC
    , "Int", c   ; INT
    , "Ptr", lpName   ; LPWSTR optional, out
    , "Int")   ; return: INT
●GetTextFaceW(hdc, c, lpName) = DLL("GDI32.dll", "int GetTextFaceW(void*, int, char*)")
# 呼び出し: GetTextFaceW(hdc, c, lpName)
# hdc : HDC -> "void*"
# c : INT -> "int"
# lpName : LPWSTR optional, out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef GetTextFaceWNative = Int32 Function(Pointer<Void>, Int32, Pointer<Utf16>);
typedef GetTextFaceWDart = int Function(Pointer<Void>, int, Pointer<Utf16>);
final GetTextFaceW = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<GetTextFaceWNative, GetTextFaceWDart>('GetTextFaceW');
// hdc : HDC -> Pointer<Void>
// c : INT -> Int32
// lpName : LPWSTR optional, out -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetTextFaceW(
  hdc: THandle;   // HDC
  c: Integer;   // INT
  lpName: PWideChar   // LPWSTR optional, out
): Integer; stdcall;
  external 'GDI32.dll' name 'GetTextFaceW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetTextFaceW"
  c_GetTextFaceW :: Ptr () -> Int32 -> CWString -> IO Int32
-- hdc : HDC -> Ptr ()
-- c : INT -> Int32
-- lpName : LPWSTR optional, out -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let gettextfacew =
  foreign "GetTextFaceW"
    ((ptr void) @-> int32_t @-> (ptr uint16_t) @-> returning int32_t)
(* hdc : HDC -> (ptr void) *)
(* c : INT -> int32_t *)
(* lpName : LPWSTR optional, out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("GetTextFaceW" get-text-face-w :convention :stdcall) :int32
  (hdc :pointer)   ; HDC
  (c :int32)   ; INT
  (lp-name :pointer))   ; LPWSTR optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetTextFaceW = Win32::API::More->new('GDI32',
    'int GetTextFaceW(HANDLE hdc, int c, LPWSTR lpName)');
# my $ret = $GetTextFaceW->Call($hdc, $c, $lpName);
# hdc : HDC -> HANDLE
# c : INT -> int
# lpName : LPWSTR optional, out -> LPWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目