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
dwResSizeDWORDin
fIconBOOLin
dwVerDWORDin

戻り値の型: HICON

各言語での呼び出し定義

// 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)。