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

InsertMenuItemA

関数
情報構造体を用いてメニュー項目を挿入する(ANSI版)。
DLLUSER32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL InsertMenuItemA(
    HMENU hmenu,
    DWORD item,
    BOOL fByPosition,
    MENUITEMINFOA* lpmi
);

パラメーター

名前方向説明
hmenuHMENUin新しいメニュー項目を挿入するメニューへのハンドルです。
itemDWORDin新しい項目を挿入する位置の基準となるメニュー項目の識別子または位置です。このパラメーターの意味は fByPosition の値によって異なります。
fByPositionBOOLinitem の意味を制御します。このパラメーターが FALSE の場合、item はメニュー項目の識別子です。それ以外の場合は、メニュー項目の位置です。詳細については、Accessing Menu Items Programmatically を参照してください。
lpmiMENUITEMINFOA*in新しいメニュー項目に関する情報を格納する MENUITEMINFO 構造体へのポインターです。

戻り値の型: BOOL

公式ドキュメント

メニュー内の指定した位置に新しいメニュー項目を挿入します。(ANSI)

戻り値

型: BOOL

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、GetLastError 関数を使用します。

解説(Remarks)

アプリケーションは、メニューが表示されているウィンドウにあるかどうかにかかわらず、メニューが変更されるたびに DrawMenuBar 関数を呼び出す必要があります。

ビットマップ メニュー項目またはオーナー描画メニュー項目でキーボード アクセラレーターを機能させるには、メニューの所有者が WM_MENUCHAR メッセージを処理する必要があります。詳細については、Owner-Drawn Menus and the WM_MENUCHAR Message を参照してください。

例については、Example of Menu-Item Bitmaps を参照してください。

メモ

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

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

各言語での呼び出し定義

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

BOOL InsertMenuItemA(
    HMENU hmenu,
    DWORD item,
    BOOL fByPosition,
    MENUITEMINFOA* lpmi
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool InsertMenuItemA(
    IntPtr hmenu,   // HMENU
    uint item,   // DWORD
    bool fByPosition,   // BOOL
    IntPtr lpmi   // MENUITEMINFOA*
);
<DllImport("USER32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function InsertMenuItemA(
    hmenu As IntPtr,   ' HMENU
    item As UInteger,   ' DWORD
    fByPosition As Boolean,   ' BOOL
    lpmi As IntPtr   ' MENUITEMINFOA*
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hmenu : HMENU
' item : DWORD
' fByPosition : BOOL
' lpmi : MENUITEMINFOA*
Declare PtrSafe Function InsertMenuItemA Lib "user32" ( _
    ByVal hmenu As LongPtr, _
    ByVal item As Long, _
    ByVal fByPosition As Long, _
    ByVal lpmi As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

InsertMenuItemA = ctypes.windll.user32.InsertMenuItemA
InsertMenuItemA.restype = wintypes.BOOL
InsertMenuItemA.argtypes = [
    wintypes.HANDLE,  # hmenu : HMENU
    wintypes.DWORD,  # item : DWORD
    wintypes.BOOL,  # fByPosition : BOOL
    ctypes.c_void_p,  # lpmi : MENUITEMINFOA*
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
InsertMenuItemA = Fiddle::Function.new(
  lib['InsertMenuItemA'],
  [
    Fiddle::TYPE_VOIDP,  # hmenu : HMENU
    -Fiddle::TYPE_INT,  # item : DWORD
    Fiddle::TYPE_INT,  # fByPosition : BOOL
    Fiddle::TYPE_VOIDP,  # lpmi : MENUITEMINFOA*
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn InsertMenuItemA(
        hmenu: *mut core::ffi::c_void,  // HMENU
        item: u32,  // DWORD
        fByPosition: i32,  // BOOL
        lpmi: *mut MENUITEMINFOA  // MENUITEMINFOA*
    ) -> 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 InsertMenuItemA(IntPtr hmenu, uint item, bool fByPosition, IntPtr lpmi);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_InsertMenuItemA' -Namespace Win32 -PassThru
# $api::InsertMenuItemA(hmenu, item, fByPosition, lpmi)
#uselib "USER32.dll"
#func global InsertMenuItemA "InsertMenuItemA" sptr, sptr, sptr, sptr
; InsertMenuItemA hmenu, item, fByPosition, varptr(lpmi)   ; 戻り値は stat
; hmenu : HMENU -> "sptr"
; item : DWORD -> "sptr"
; fByPosition : BOOL -> "sptr"
; lpmi : MENUITEMINFOA* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global InsertMenuItemA "InsertMenuItemA" sptr, int, int, var
; res = InsertMenuItemA(hmenu, item, fByPosition, lpmi)
; hmenu : HMENU -> "sptr"
; item : DWORD -> "int"
; fByPosition : BOOL -> "int"
; lpmi : MENUITEMINFOA* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL InsertMenuItemA(HMENU hmenu, DWORD item, BOOL fByPosition, MENUITEMINFOA* lpmi)
#uselib "USER32.dll"
#cfunc global InsertMenuItemA "InsertMenuItemA" intptr, int, int, var
; res = InsertMenuItemA(hmenu, item, fByPosition, lpmi)
; hmenu : HMENU -> "intptr"
; item : DWORD -> "int"
; fByPosition : BOOL -> "int"
; lpmi : MENUITEMINFOA* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procInsertMenuItemA = user32.NewProc("InsertMenuItemA")
)

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

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

typedef InsertMenuItemANative = Int32 Function(Pointer<Void>, Uint32, Int32, Pointer<Void>);
typedef InsertMenuItemADart = int Function(Pointer<Void>, int, int, Pointer<Void>);
final InsertMenuItemA = DynamicLibrary.open('USER32.dll')
    .lookupFunction<InsertMenuItemANative, InsertMenuItemADart>('InsertMenuItemA');
// hmenu : HMENU -> Pointer<Void>
// item : DWORD -> Uint32
// fByPosition : BOOL -> Int32
// lpmi : MENUITEMINFOA* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function InsertMenuItemA(
  hmenu: THandle;   // HMENU
  item: DWORD;   // DWORD
  fByPosition: BOOL;   // BOOL
  lpmi: Pointer   // MENUITEMINFOA*
): BOOL; stdcall;
  external 'USER32.dll' name 'InsertMenuItemA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "InsertMenuItemA"
  c_InsertMenuItemA :: Ptr () -> Word32 -> CInt -> Ptr () -> IO CInt
-- hmenu : HMENU -> Ptr ()
-- item : DWORD -> Word32
-- fByPosition : BOOL -> CInt
-- lpmi : MENUITEMINFOA* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let insertmenuitema =
  foreign "InsertMenuItemA"
    ((ptr void) @-> uint32_t @-> int32_t @-> (ptr void) @-> returning int32_t)
(* hmenu : HMENU -> (ptr void) *)
(* item : DWORD -> uint32_t *)
(* fByPosition : BOOL -> int32_t *)
(* lpmi : MENUITEMINFOA* -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("InsertMenuItemA" insert-menu-item-a :convention :stdcall) :int32
  (hmenu :pointer)   ; HMENU
  (item :uint32)   ; DWORD
  (f-by-position :int32)   ; BOOL
  (lpmi :pointer))   ; MENUITEMINFOA*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $InsertMenuItemA = Win32::API::More->new('USER32',
    'BOOL InsertMenuItemA(HANDLE hmenu, DWORD item, BOOL fByPosition, LPVOID lpmi)');
# my $ret = $InsertMenuItemA->Call($hmenu, $item, $fByPosition, $lpmi);
# hmenu : HMENU -> HANDLE
# item : DWORD -> DWORD
# fByPosition : BOOL -> BOOL
# lpmi : MENUITEMINFOA* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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