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

InsertMenuA

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

シグネチャ

// USER32.dll  (ANSI / -A)
#include <windows.h>

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

パラメーター

名前方向
hMenuHMENUin
uPositionDWORDin
uFlagsMENU_ITEM_FLAGSin
uIDNewItemUINT_PTRin
lpNewItemLPCSTRinoptional

戻り値の型: BOOL

各言語での呼び出し定義

// USER32.dll  (ANSI / -A)
#include <windows.h>

BOOL InsertMenuA(
    HMENU hMenu,
    DWORD uPosition,
    MENU_ITEM_FLAGS uFlags,
    UINT_PTR uIDNewItem,
    LPCSTR lpNewItem   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool InsertMenuA(
    IntPtr hMenu,   // HMENU
    uint uPosition,   // DWORD
    uint uFlags,   // MENU_ITEM_FLAGS
    UIntPtr uIDNewItem,   // UINT_PTR
    [MarshalAs(UnmanagedType.LPStr)] string lpNewItem   // LPCSTR optional
);
<DllImport("USER32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function InsertMenuA(
    hMenu As IntPtr,   ' HMENU
    uPosition As UInteger,   ' DWORD
    uFlags As UInteger,   ' MENU_ITEM_FLAGS
    uIDNewItem As UIntPtr,   ' UINT_PTR
    <MarshalAs(UnmanagedType.LPStr)> lpNewItem As String   ' LPCSTR optional
) As Boolean
End Function
' hMenu : HMENU
' uPosition : DWORD
' uFlags : MENU_ITEM_FLAGS
' uIDNewItem : UINT_PTR
' lpNewItem : LPCSTR optional
Declare PtrSafe Function InsertMenuA Lib "user32" ( _
    ByVal hMenu As LongPtr, _
    ByVal uPosition As Long, _
    ByVal uFlags As Long, _
    ByVal uIDNewItem As LongPtr, _
    ByVal lpNewItem As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procInsertMenuA = user32.NewProc("InsertMenuA")
)

// hMenu (HMENU), uPosition (DWORD), uFlags (MENU_ITEM_FLAGS), uIDNewItem (UINT_PTR), lpNewItem (LPCSTR optional)
r1, _, err := procInsertMenuA.Call(
	uintptr(hMenu),
	uintptr(uPosition),
	uintptr(uFlags),
	uintptr(uIDNewItem),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpNewItem))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function InsertMenuA(
  hMenu: THandle;   // HMENU
  uPosition: DWORD;   // DWORD
  uFlags: DWORD;   // MENU_ITEM_FLAGS
  uIDNewItem: NativeUInt;   // UINT_PTR
  lpNewItem: PAnsiChar   // LPCSTR optional
): BOOL; stdcall;
  external 'USER32.dll' name 'InsertMenuA';
result := DllCall("USER32\InsertMenuA"
    , "Ptr", hMenu   ; HMENU
    , "UInt", uPosition   ; DWORD
    , "UInt", uFlags   ; MENU_ITEM_FLAGS
    , "UPtr", uIDNewItem   ; UINT_PTR
    , "AStr", lpNewItem   ; LPCSTR optional
    , "Int")   ; return: BOOL
●InsertMenuA(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem) = DLL("USER32.dll", "bool InsertMenuA(void*, dword, dword, int, char*)")
# 呼び出し: InsertMenuA(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
# hMenu : HMENU -> "void*"
# uPosition : DWORD -> "dword"
# uFlags : MENU_ITEM_FLAGS -> "dword"
# uIDNewItem : UINT_PTR -> "int"
# lpNewItem : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。