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

InsertMenuW

関数
指定位置にメニュー項目を挿入する(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL InsertMenuW(
    HMENU hMenu,
    DWORD uPosition,
    MENU_ITEM_FLAGS uFlags,
    UINT_PTR uIDNewItem,
    LPCWSTR lpNewItem   // optional
);

パラメーター

名前方向
hMenuHMENUin
uPositionDWORDin
uFlagsMENU_ITEM_FLAGSin
uIDNewItemUINT_PTRin
lpNewItemLPCWSTRinoptional

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL InsertMenuW(
    HMENU hMenu,
    DWORD uPosition,
    MENU_ITEM_FLAGS uFlags,
    UINT_PTR uIDNewItem,
    LPCWSTR lpNewItem   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool InsertMenuW(
    IntPtr hMenu,   // HMENU
    uint uPosition,   // DWORD
    uint uFlags,   // MENU_ITEM_FLAGS
    UIntPtr uIDNewItem,   // UINT_PTR
    [MarshalAs(UnmanagedType.LPWStr)] string lpNewItem   // LPCWSTR optional
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function InsertMenuW(
    hMenu As IntPtr,   ' HMENU
    uPosition As UInteger,   ' DWORD
    uFlags As UInteger,   ' MENU_ITEM_FLAGS
    uIDNewItem As UIntPtr,   ' UINT_PTR
    <MarshalAs(UnmanagedType.LPWStr)> lpNewItem As String   ' LPCWSTR optional
) As Boolean
End Function
' hMenu : HMENU
' uPosition : DWORD
' uFlags : MENU_ITEM_FLAGS
' uIDNewItem : UINT_PTR
' lpNewItem : LPCWSTR optional
Declare PtrSafe Function InsertMenuW Lib "user32" ( _
    ByVal hMenu As LongPtr, _
    ByVal uPosition As Long, _
    ByVal uFlags As Long, _
    ByVal uIDNewItem As LongPtr, _
    ByVal lpNewItem 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

InsertMenuW = ctypes.windll.user32.InsertMenuW
InsertMenuW.restype = wintypes.BOOL
InsertMenuW.argtypes = [
    wintypes.HANDLE,  # hMenu : HMENU
    wintypes.DWORD,  # uPosition : DWORD
    wintypes.DWORD,  # uFlags : MENU_ITEM_FLAGS
    ctypes.c_size_t,  # uIDNewItem : UINT_PTR
    wintypes.LPCWSTR,  # lpNewItem : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
InsertMenuW = Fiddle::Function.new(
  lib['InsertMenuW'],
  [
    Fiddle::TYPE_VOIDP,  # hMenu : HMENU
    -Fiddle::TYPE_INT,  # uPosition : DWORD
    -Fiddle::TYPE_INT,  # uFlags : MENU_ITEM_FLAGS
    Fiddle::TYPE_UINTPTR_T,  # uIDNewItem : UINT_PTR
    Fiddle::TYPE_VOIDP,  # lpNewItem : LPCWSTR optional
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn InsertMenuW(
        hMenu: *mut core::ffi::c_void,  // HMENU
        uPosition: u32,  // DWORD
        uFlags: u32,  // MENU_ITEM_FLAGS
        uIDNewItem: usize,  // UINT_PTR
        lpNewItem: *const u16  // LPCWSTR optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool InsertMenuW(IntPtr hMenu, uint uPosition, uint uFlags, UIntPtr uIDNewItem, [MarshalAs(UnmanagedType.LPWStr)] string lpNewItem);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_InsertMenuW' -Namespace Win32 -PassThru
# $api::InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
#uselib "USER32.dll"
#func global InsertMenuW "InsertMenuW" wptr, wptr, wptr, wptr, wptr
; InsertMenuW hMenu, uPosition, uFlags, uIDNewItem, lpNewItem   ; 戻り値は stat
; hMenu : HMENU -> "wptr"
; uPosition : DWORD -> "wptr"
; uFlags : MENU_ITEM_FLAGS -> "wptr"
; uIDNewItem : UINT_PTR -> "wptr"
; lpNewItem : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global InsertMenuW "InsertMenuW" sptr, int, int, sptr, wstr
; res = InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
; hMenu : HMENU -> "sptr"
; uPosition : DWORD -> "int"
; uFlags : MENU_ITEM_FLAGS -> "int"
; uIDNewItem : UINT_PTR -> "sptr"
; lpNewItem : LPCWSTR optional -> "wstr"
; BOOL InsertMenuW(HMENU hMenu, DWORD uPosition, MENU_ITEM_FLAGS uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem)
#uselib "USER32.dll"
#cfunc global InsertMenuW "InsertMenuW" intptr, int, int, intptr, wstr
; res = InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
; hMenu : HMENU -> "intptr"
; uPosition : DWORD -> "int"
; uFlags : MENU_ITEM_FLAGS -> "int"
; uIDNewItem : UINT_PTR -> "intptr"
; lpNewItem : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procInsertMenuW = user32.NewProc("InsertMenuW")
)

// hMenu (HMENU), uPosition (DWORD), uFlags (MENU_ITEM_FLAGS), uIDNewItem (UINT_PTR), lpNewItem (LPCWSTR optional)
r1, _, err := procInsertMenuW.Call(
	uintptr(hMenu),
	uintptr(uPosition),
	uintptr(uFlags),
	uintptr(uIDNewItem),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpNewItem))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function InsertMenuW(
  hMenu: THandle;   // HMENU
  uPosition: DWORD;   // DWORD
  uFlags: DWORD;   // MENU_ITEM_FLAGS
  uIDNewItem: NativeUInt;   // UINT_PTR
  lpNewItem: PWideChar   // LPCWSTR optional
): BOOL; stdcall;
  external 'USER32.dll' name 'InsertMenuW';
result := DllCall("USER32\InsertMenuW"
    , "Ptr", hMenu   ; HMENU
    , "UInt", uPosition   ; DWORD
    , "UInt", uFlags   ; MENU_ITEM_FLAGS
    , "UPtr", uIDNewItem   ; UINT_PTR
    , "WStr", lpNewItem   ; LPCWSTR optional
    , "Int")   ; return: BOOL
●InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem) = DLL("USER32.dll", "bool InsertMenuW(void*, dword, dword, int, char*)")
# 呼び出し: InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
# hMenu : HMENU -> "void*"
# uPosition : DWORD -> "dword"
# uFlags : MENU_ITEM_FLAGS -> "dword"
# uIDNewItem : UINT_PTR -> "int"
# lpNewItem : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。