Win32 API 日本語リファレンス
ホームUI.WindowsAndMessaging › CreateIconFromResource

CreateIconFromResource

関数
リソースビットからアイコンまたはカーソルを作成する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

HICON CreateIconFromResource(
    BYTE* presbits,
    DWORD dwResSize,
    BOOL fIcon,
    DWORD dwVer
);

パラメーター

名前方向説明
presbitsBYTE*in

アイコンまたはカーソルのリソースビットを格納する、DWORD 境界に整列したバッファへのポインターです。これらのビットは通常、LookupIconIdFromDirectoryLookupIconIdFromDirectoryExLoadResource の各関数を呼び出すことで読み込まれます。

アイコンおよびカーソルのリソース形式の詳細については、Cursor and Icon Resources を参照してください。

dwResSizeDWORDinpresbits パラメーターが指すビット列のサイズ(バイト単位)です。
fIconBOOLin

アイコンとカーソルのどちらを作成するかを指定します。このパラメーターが TRUE の場合はアイコンが作成されます。FALSE の場合はカーソルが作成されます。

LOCALHEADER 構造体はカーソルのホットスポットを定義しており、カーソルのリソースビットから最初に読み取られるデータです。

dwVerDWORDinpresbits パラメーターが指すリソースビットのアイコンまたはカーソル形式のバージョン番号です。値は 0x00020000 以上 0x00030000 以下でなければなりません。このパラメーターには通常 0x00030000 を設定します。

戻り値の型: HICON

公式ドキュメント

アイコンを記述するリソースビットからアイコンまたはカーソルを作成します。(CreateIconFromResource)

戻り値

型: HICON

関数が成功した場合、戻り値はアイコンまたはカーソルへのハンドルです。

関数が失敗した場合、戻り値は NULL です。拡張エラー情報を取得するには、GetLastError を呼び出してください。

解説(Remarks)

CreateIconFromResourceCreateIconFromResourceExCreateIconIndirectGetIconInfoLookupIconIdFromDirectoryLookupIconIdFromDirectoryEx の各関数を使用すると、シェルアプリケーションやアイコンブラウザーがシステム全体のリソースを調査して利用できます。

CreateIconFromResource 関数は、フラグとして LR_DEFAULTSIZE|LR_SHARED を渡して CreateIconFromResourceEx を呼び出します。

CreateIconFromResource で作成したアイコンには DestroyIcon を、カーソルには DestroyCursor を呼び出してください。

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

各言語での呼び出し定義

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

HICON CreateIconFromResource(
    BYTE* presbits,
    DWORD dwResSize,
    BOOL fIcon,
    DWORD dwVer
);
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateIconFromResource(
    IntPtr presbits,   // BYTE*
    uint dwResSize,   // DWORD
    bool fIcon,   // BOOL
    uint dwVer   // DWORD
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateIconFromResource(
    presbits As IntPtr,   ' BYTE*
    dwResSize As UInteger,   ' DWORD
    fIcon As Boolean,   ' BOOL
    dwVer As UInteger   ' DWORD
) As IntPtr
End Function
' presbits : BYTE*
' dwResSize : DWORD
' fIcon : BOOL
' dwVer : DWORD
Declare PtrSafe Function CreateIconFromResource Lib "user32" ( _
    ByVal presbits As LongPtr, _
    ByVal dwResSize As Long, _
    ByVal fIcon As Long, _
    ByVal dwVer As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateIconFromResource = ctypes.windll.user32.CreateIconFromResource
CreateIconFromResource.restype = ctypes.c_void_p
CreateIconFromResource.argtypes = [
    ctypes.POINTER(ctypes.c_ubyte),  # presbits : BYTE*
    wintypes.DWORD,  # dwResSize : DWORD
    wintypes.BOOL,  # fIcon : BOOL
    wintypes.DWORD,  # dwVer : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
CreateIconFromResource = Fiddle::Function.new(
  lib['CreateIconFromResource'],
  [
    Fiddle::TYPE_VOIDP,  # presbits : BYTE*
    -Fiddle::TYPE_INT,  # dwResSize : DWORD
    Fiddle::TYPE_INT,  # fIcon : BOOL
    -Fiddle::TYPE_INT,  # dwVer : DWORD
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "user32")]
extern "system" {
    fn CreateIconFromResource(
        presbits: *mut u8,  // BYTE*
        dwResSize: u32,  // DWORD
        fIcon: i32,  // BOOL
        dwVer: u32  // DWORD
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", SetLastError = true)]
public static extern IntPtr CreateIconFromResource(IntPtr presbits, uint dwResSize, bool fIcon, uint dwVer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_CreateIconFromResource' -Namespace Win32 -PassThru
# $api::CreateIconFromResource(presbits, dwResSize, fIcon, dwVer)
#uselib "USER32.dll"
#func global CreateIconFromResource "CreateIconFromResource" sptr, sptr, sptr, sptr
; CreateIconFromResource varptr(presbits), dwResSize, fIcon, dwVer   ; 戻り値は stat
; presbits : BYTE* -> "sptr"
; dwResSize : DWORD -> "sptr"
; fIcon : BOOL -> "sptr"
; dwVer : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global CreateIconFromResource "CreateIconFromResource" var, int, int, int
; res = CreateIconFromResource(presbits, dwResSize, fIcon, dwVer)
; presbits : BYTE* -> "var"
; dwResSize : DWORD -> "int"
; fIcon : BOOL -> "int"
; dwVer : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HICON CreateIconFromResource(BYTE* presbits, DWORD dwResSize, BOOL fIcon, DWORD dwVer)
#uselib "USER32.dll"
#cfunc global CreateIconFromResource "CreateIconFromResource" var, int, int, int
; res = CreateIconFromResource(presbits, dwResSize, fIcon, dwVer)
; presbits : BYTE* -> "var"
; dwResSize : DWORD -> "int"
; fIcon : BOOL -> "int"
; dwVer : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procCreateIconFromResource = user32.NewProc("CreateIconFromResource")
)

// presbits (BYTE*), dwResSize (DWORD), fIcon (BOOL), dwVer (DWORD)
r1, _, err := procCreateIconFromResource.Call(
	uintptr(presbits),
	uintptr(dwResSize),
	uintptr(fIcon),
	uintptr(dwVer),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HICON
function CreateIconFromResource(
  presbits: Pointer;   // BYTE*
  dwResSize: DWORD;   // DWORD
  fIcon: BOOL;   // BOOL
  dwVer: DWORD   // DWORD
): THandle; stdcall;
  external 'USER32.dll' name 'CreateIconFromResource';
result := DllCall("USER32\CreateIconFromResource"
    , "Ptr", presbits   ; BYTE*
    , "UInt", dwResSize   ; DWORD
    , "Int", fIcon   ; BOOL
    , "UInt", dwVer   ; DWORD
    , "Ptr")   ; return: HICON
●CreateIconFromResource(presbits, dwResSize, fIcon, dwVer) = DLL("USER32.dll", "void* CreateIconFromResource(void*, dword, bool, dword)")
# 呼び出し: CreateIconFromResource(presbits, dwResSize, fIcon, dwVer)
# presbits : BYTE* -> "void*"
# dwResSize : DWORD -> "dword"
# fIcon : BOOL -> "bool"
# dwVer : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef CreateIconFromResourceNative = Pointer<Void> Function(Pointer<Uint8>, Uint32, Int32, Uint32);
typedef CreateIconFromResourceDart = Pointer<Void> Function(Pointer<Uint8>, int, int, int);
final CreateIconFromResource = DynamicLibrary.open('USER32.dll')
    .lookupFunction<CreateIconFromResourceNative, CreateIconFromResourceDart>('CreateIconFromResource');
// presbits : BYTE* -> Pointer<Uint8>
// dwResSize : DWORD -> Uint32
// fIcon : BOOL -> Int32
// dwVer : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateIconFromResource(
  presbits: Pointer;   // BYTE*
  dwResSize: DWORD;   // DWORD
  fIcon: BOOL;   // BOOL
  dwVer: DWORD   // DWORD
): THandle; stdcall;
  external 'USER32.dll' name 'CreateIconFromResource';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateIconFromResource"
  c_CreateIconFromResource :: Ptr Word8 -> Word32 -> CInt -> Word32 -> IO (Ptr ())
-- presbits : BYTE* -> Ptr Word8
-- dwResSize : DWORD -> Word32
-- fIcon : BOOL -> CInt
-- dwVer : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createiconfromresource =
  foreign "CreateIconFromResource"
    ((ptr uint8_t) @-> uint32_t @-> int32_t @-> uint32_t @-> returning (ptr void))
(* presbits : BYTE* -> (ptr uint8_t) *)
(* dwResSize : DWORD -> uint32_t *)
(* fIcon : BOOL -> int32_t *)
(* dwVer : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("CreateIconFromResource" create-icon-from-resource :convention :stdcall) :pointer
  (presbits :pointer)   ; BYTE*
  (dw-res-size :uint32)   ; DWORD
  (f-icon :int32)   ; BOOL
  (dw-ver :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateIconFromResource = Win32::API::More->new('USER32',
    'HANDLE CreateIconFromResource(LPVOID presbits, DWORD dwResSize, BOOL fIcon, DWORD dwVer)');
# my $ret = $CreateIconFromResource->Call($presbits, $dwResSize, $fIcon, $dwVer);
# presbits : BYTE* -> LPVOID
# dwResSize : DWORD -> DWORD
# fIcon : BOOL -> BOOL
# dwVer : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目