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

CreateEnhMetaFileW

関数
拡張メタファイル記録用のデバイスコンテキストを作成する(Unicode版)。
DLLGDI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

HDC CreateEnhMetaFileW(
    HDC hdc,   // optional
    LPCWSTR lpFilename,   // optional
    const RECT* lprc,   // optional
    LPCWSTR lpDesc   // optional
);

パラメーター

名前型方向説明
hdcHDCinoptional拡張メタファイルの参照デバイスへのハンドル。このパラメーターは NULL を指定できます。詳細については「解説」を参照してください。
lpFilenameLPCWSTRinoptional作成する拡張メタファイルのファイル名へのポインター。このパラメーターが NULL の場合、拡張メタファイルはメモリベースとなり、DeleteEnhMetaFile 関数で削除されるとその内容は失われます。
lprcRECT*inoptional拡張メタファイルに格納する図の寸法(0.01 ミリメートル単位)を指定する RECT 構造体へのポインター。
lpDescLPCWSTRinoptional図を作成したアプリケーションの名前および図のタイトルを指定する文字列へのポインター。このパラメーターは NULL を指定できます。詳細については「解説」を参照してください。

戻り値の型: HDC

公式ドキュメント

CreateEnhMetaFile 関数は、拡張形式メタファイル用のデバイスコンテキストを作成します。このデバイスコンテキストは、デバイス非依存の図を格納するために使用できます。(Unicode)

戻り値

関数が成功すると、戻り値は拡張メタファイル用のデバイスコンテキストへのハンドルになります。

関数が失敗すると、戻り値は NULL になります。

解説(Remarks)

テキスト引数で Unicode 文字を使用する必要がある場合は、CreateEnhMetaFile 関数をワイド文字版として使用します。テキスト引数で Windows 文字セットの文字を使用する必要がある場合は、この関数を ANSI 版として使用します。

システムは、hdc パラメーターで識別される参照デバイスを使用して、図が元々表示されたデバイスの解像度と単位を記録します。hdc パラメーターが NULL の場合は、現在のディスプレイデバイスが参照として使用されます。

lprc パラメーターが指す RECT 構造体の left および top メンバーは、それぞれ right および bottom メンバーより小さくなければなりません。四角形の各辺上の点は図に含まれます。lprc が NULL の場合、グラフィックスデバイスインターフェイス (GDI) は、アプリケーションが描画した図を囲む最小の四角形の寸法を計算します。lprc パラメーターは可能な限り指定すべきです。

lpDesc パラメーターが指す文字列は、アプリケーション名と図名の間に null 文字を含み、2 つの null 文字で終わる必要があります。たとえば "XYZ Graphics Editor\0Bald Eagle\0\0" のようになります(\0 は null 文字を表します)。lpDesc が NULL の場合、拡張メタファイルヘッダーには対応するエントリがありません。

アプリケーションは、この関数で作成されたデバイスコンテキストを使用して、グラフィックスの図を拡張メタファイルに格納します。このデバイスコンテキストを識別するハンドルは、任意の GDI 関数に渡すことができます。

アプリケーションは、図を拡張メタファイルに格納した後、PlayEnhMetaFile 関数を呼び出すことで、任意の出力デバイスにその図を表示できます。図を表示する際、システムは lprc パラメーターが指す四角形と参照デバイスの解像度データを使用して、図の位置とスケールを決定します。

この関数が返すデバイスコンテキストには、新しいデバイスコンテキストに関連付けられるものと同じ既定の属性が含まれます。

拡張メタファイルを古い Windows メタファイル形式に変換するには、アプリケーションは GetWinMetaFileBits 関数を使用する必要があります。

拡張メタファイルのファイル名には .emf 拡張子を使用してください。

例

例については、Creating an Enhanced Metafile を参照してください。

メモ

wingdi.h ヘッダーは、CreateEnhMetaFile を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング中立なエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は 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>

HDC CreateEnhMetaFileW(
    HDC hdc,   // optional
    LPCWSTR lpFilename,   // optional
    const RECT* lprc,   // optional
    LPCWSTR lpDesc   // optional
);
[DllImport("GDI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr CreateEnhMetaFileW(
    IntPtr hdc,   // HDC optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpFilename,   // LPCWSTR optional
    IntPtr lprc,   // RECT* optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpDesc   // LPCWSTR optional
);
<DllImport("GDI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function CreateEnhMetaFileW(
    hdc As IntPtr,   ' HDC optional
    <MarshalAs(UnmanagedType.LPWStr)> lpFilename As String,   ' LPCWSTR optional
    lprc As IntPtr,   ' RECT* optional
    <MarshalAs(UnmanagedType.LPWStr)> lpDesc As String   ' LPCWSTR optional
) As IntPtr
End Function
' hdc : HDC optional
' lpFilename : LPCWSTR optional
' lprc : RECT* optional
' lpDesc : LPCWSTR optional
Declare PtrSafe Function CreateEnhMetaFileW Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal lpFilename As LongPtr, _
    ByVal lprc As LongPtr, _
    ByVal lpDesc As LongPtr) As LongPtr
' 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

CreateEnhMetaFileW = ctypes.windll.gdi32.CreateEnhMetaFileW
CreateEnhMetaFileW.restype = ctypes.c_void_p
CreateEnhMetaFileW.argtypes = [
    wintypes.HANDLE,  # hdc : HDC optional
    wintypes.LPCWSTR,  # lpFilename : LPCWSTR optional
    ctypes.c_void_p,  # lprc : RECT* optional
    wintypes.LPCWSTR,  # lpDesc : LPCWSTR optional
]
require 'fiddle'
require 'fiddle/import'

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

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procCreateEnhMetaFileW = gdi32.NewProc("CreateEnhMetaFileW")
)

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

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

typedef CreateEnhMetaFileWNative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Void>, Pointer<Utf16>);
typedef CreateEnhMetaFileWDart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Void>, Pointer<Utf16>);
final CreateEnhMetaFileW = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<CreateEnhMetaFileWNative, CreateEnhMetaFileWDart>('CreateEnhMetaFileW');
// hdc : HDC optional -> Pointer<Void>
// lpFilename : LPCWSTR optional -> Pointer<Utf16>
// lprc : RECT* optional -> Pointer<Void>
// lpDesc : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateEnhMetaFileW(
  hdc: THandle;   // HDC optional
  lpFilename: PWideChar;   // LPCWSTR optional
  lprc: Pointer;   // RECT* optional
  lpDesc: PWideChar   // LPCWSTR optional
): THandle; stdcall;
  external 'GDI32.dll' name 'CreateEnhMetaFileW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateEnhMetaFileW"
  c_CreateEnhMetaFileW :: Ptr () -> CWString -> Ptr () -> CWString -> IO (Ptr ())
-- hdc : HDC optional -> Ptr ()
-- lpFilename : LPCWSTR optional -> CWString
-- lprc : RECT* optional -> Ptr ()
-- lpDesc : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createenhmetafilew =
  foreign "CreateEnhMetaFileW"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr void) @-> (ptr uint16_t) @-> returning (ptr void))
(* hdc : HDC optional -> (ptr void) *)
(* lpFilename : LPCWSTR optional -> (ptr uint16_t) *)
(* lprc : RECT* optional -> (ptr void) *)
(* lpDesc : LPCWSTR optional -> (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 ("CreateEnhMetaFileW" create-enh-meta-file-w :convention :stdcall) :pointer
  (hdc :pointer)   ; HDC optional
  (lp-filename (:string :encoding :utf-16le))   ; LPCWSTR optional
  (lprc :pointer)   ; RECT* optional
  (lp-desc (:string :encoding :utf-16le)))   ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateEnhMetaFileW = Win32::API::More->new('GDI32',
    'HANDLE CreateEnhMetaFileW(HANDLE hdc, LPCWSTR lpFilename, LPVOID lprc, LPCWSTR lpDesc)');
# my $ret = $CreateEnhMetaFileW->Call($hdc, $lpFilename, $lprc, $lpDesc);
# hdc : HDC optional -> HANDLE
# lpFilename : LPCWSTR optional -> LPCWSTR
# lprc : RECT* optional -> LPVOID
# lpDesc : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目
使用する型