ホーム › UI.WindowsAndMessaging › InsertMenuW
InsertMenuW
関数指定位置にメニュー項目を挿入する(Unicode版)。
シグネチャ
// USER32.dll (Unicode / -W)
#include <windows.h>
BOOL InsertMenuW(
HMENU hMenu,
DWORD uPosition,
MENU_ITEM_FLAGS uFlags,
UINT_PTR uIDNewItem,
LPCWSTR lpNewItem // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hMenu | HMENU | in | 変更するメニューへのハンドルです。 | ||||||||||||||||||||||||||||||||
| uPosition | DWORD | in | uFlags パラメーターによって決定される、新しいメニュー項目を挿入する位置の直前のメニュー項目です。 | ||||||||||||||||||||||||||||||||
| uFlags | MENU_ITEM_FLAGS | in | uPosition パラメーターの解釈、および新しいメニュー項目の内容・外観・動作を制御します。このパラメーターには、次の必須の値のいずれか 1 つを含める必要があります。
また、このパラメーターには次の値のうち少なくとも 1 つを含める必要があります。
| ||||||||||||||||||||||||||||||||
| uIDNewItem | UINT_PTR | in | 新しいメニュー項目の識別子です。uFlags パラメーターに MF_POPUP フラグが設定されている場合は、ドロップダウンメニューまたはサブメニューへのハンドルになります。 | ||||||||||||||||||||||||||||||||
| lpNewItem | LPCWSTR | inoptional | 新しいメニュー項目の内容です。lpNewItem の解釈は、uFlags パラメーターに MF_BITMAP、MF_OWNERDRAW、または MF_STRING フラグが含まれるかどうかによって、次のように異なります。
|
戻り値の型: BOOL
公式ドキュメント
新しいメニュー項目をメニューに挿入し、他の項目をメニューの下方へ移動します。(Unicode)
戻り値
解説(Remarks)
メニューが表示中のウィンドウにあるかどうかにかかわらず、メニューが変更されるたびにアプリケーションは DrawMenuBar 関数を呼び出す必要があります。
次のフラグのグループは、同時に使用できません。
- MF_BYCOMMAND と MF_BYPOSITION
- MF_DISABLED、MF_ENABLED、MF_GRAYED
- MF_BITMAP、MF_STRING、MF_OWNERDRAW、MF_SEPARATOR
- MF_MENUBARBREAK と MF_MENUBREAK
- MF_CHECKED と MF_UNCHECKED
メモ
winuser.h ヘッダーは InsertMenu をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング非依存のエイリアスの使用と、エンコーディング非依存でないコードを混在させると、不一致が生じてコンパイルエラーまたは実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 <MarshalAs(UnmanagedType.Bool)> 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 // BOOLfunction 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 版の利用を推奨。const std = @import("std");
extern "user32" fn InsertMenuW(
hMenu: ?*anyopaque, // HMENU
uPosition: u32, // DWORD
uFlags: u32, // MENU_ITEM_FLAGS
uIDNewItem: usize, // UINT_PTR
lpNewItem: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc InsertMenuW(
hMenu: pointer, # HMENU
uPosition: uint32, # DWORD
uFlags: uint32, # MENU_ITEM_FLAGS
uIDNewItem: uint, # UINT_PTR
lpNewItem: WideCString # LPCWSTR optional
): int32 {.importc: "InsertMenuW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "user32");
extern(Windows)
int InsertMenuW(
void* hMenu, // HMENU
uint uPosition, // DWORD
uint uFlags, // MENU_ITEM_FLAGS
size_t uIDNewItem, // UINT_PTR
const(wchar)* lpNewItem // LPCWSTR optional
);ccall((:InsertMenuW, "USER32.dll"), stdcall, Int32,
(Ptr{Cvoid}, UInt32, UInt32, Csize_t, Cwstring),
hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
# hMenu : HMENU -> Ptr{Cvoid}
# uPosition : DWORD -> UInt32
# uFlags : MENU_ITEM_FLAGS -> UInt32
# uIDNewItem : UINT_PTR -> Csize_t
# lpNewItem : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t InsertMenuW(
void* hMenu,
uint32_t uPosition,
uint32_t uFlags,
uintptr_t uIDNewItem,
const uint16_t* lpNewItem);
]]
local user32 = ffi.load("user32")
-- user32.InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
-- hMenu : HMENU
-- uPosition : DWORD
-- uFlags : MENU_ITEM_FLAGS
-- uIDNewItem : UINT_PTR
-- lpNewItem : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const InsertMenuW = lib.func('__stdcall', 'InsertMenuW', 'int32_t', ['void *', 'uint32_t', 'uint32_t', 'uintptr_t', 'str16']);
// InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
// hMenu : HMENU -> 'void *'
// uPosition : DWORD -> 'uint32_t'
// uFlags : MENU_ITEM_FLAGS -> 'uint32_t'
// uIDNewItem : UINT_PTR -> 'uintptr_t'
// lpNewItem : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
InsertMenuW: { parameters: ["pointer", "u32", "u32", "usize", "buffer"], result: "i32" },
});
// lib.symbols.InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem)
// hMenu : HMENU -> "pointer"
// uPosition : DWORD -> "u32"
// uFlags : MENU_ITEM_FLAGS -> "u32"
// uIDNewItem : UINT_PTR -> "usize"
// lpNewItem : LPCWSTR optional -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t InsertMenuW(
void* hMenu,
uint32_t uPosition,
uint32_t uFlags,
size_t uIDNewItem,
const uint16_t* lpNewItem);
C, "USER32.dll");
// $ffi->InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, lpNewItem);
// hMenu : HMENU
// uPosition : DWORD
// uFlags : MENU_ITEM_FLAGS
// uIDNewItem : UINT_PTR
// lpNewItem : LPCWSTR optional
// 構造体/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.UNICODE_OPTIONS);
boolean InsertMenuW(
Pointer hMenu, // HMENU
int uPosition, // DWORD
int uFlags, // MENU_ITEM_FLAGS
long uIDNewItem, // UINT_PTR
WString lpNewItem // LPCWSTR optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("user32")]
lib LibUSER32
fun InsertMenuW = InsertMenuW(
hMenu : Void*, # HMENU
uPosition : UInt32, # DWORD
uFlags : UInt32, # MENU_ITEM_FLAGS
uIDNewItem : LibC::SizeT, # UINT_PTR
lpNewItem : UInt16* # LPCWSTR optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef InsertMenuWNative = Int32 Function(Pointer<Void>, Uint32, Uint32, UintPtr, Pointer<Utf16>);
typedef InsertMenuWDart = int Function(Pointer<Void>, int, int, int, Pointer<Utf16>);
final InsertMenuW = DynamicLibrary.open('USER32.dll')
.lookupFunction<InsertMenuWNative, InsertMenuWDart>('InsertMenuW');
// hMenu : HMENU -> Pointer<Void>
// uPosition : DWORD -> Uint32
// uFlags : MENU_ITEM_FLAGS -> Uint32
// uIDNewItem : UINT_PTR -> UintPtr
// lpNewItem : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
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';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "InsertMenuW"
c_InsertMenuW :: Ptr () -> Word32 -> Word32 -> CUIntPtr -> CWString -> IO CInt
-- hMenu : HMENU -> Ptr ()
-- uPosition : DWORD -> Word32
-- uFlags : MENU_ITEM_FLAGS -> Word32
-- uIDNewItem : UINT_PTR -> CUIntPtr
-- lpNewItem : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let insertmenuw =
foreign "InsertMenuW"
((ptr void) @-> uint32_t @-> uint32_t @-> size_t @-> (ptr uint16_t) @-> returning int32_t)
(* hMenu : HMENU -> (ptr void) *)
(* uPosition : DWORD -> uint32_t *)
(* uFlags : MENU_ITEM_FLAGS -> uint32_t *)
(* uIDNewItem : UINT_PTR -> size_t *)
(* lpNewItem : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("InsertMenuW" insert-menu-w :convention :stdcall) :int32
(h-menu :pointer) ; HMENU
(u-position :uint32) ; DWORD
(u-flags :uint32) ; MENU_ITEM_FLAGS
(u-idnew-item :uint64) ; UINT_PTR
(lp-new-item (:string :encoding :utf-16le))) ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $InsertMenuW = Win32::API::More->new('USER32',
'BOOL InsertMenuW(HANDLE hMenu, DWORD uPosition, DWORD uFlags, WPARAM uIDNewItem, LPCWSTR lpNewItem)');
# my $ret = $InsertMenuW->Call($hMenu, $uPosition, $uFlags, $uIDNewItem, $lpNewItem);
# hMenu : HMENU -> HANDLE
# uPosition : DWORD -> DWORD
# uFlags : MENU_ITEM_FLAGS -> DWORD
# uIDNewItem : UINT_PTR -> WPARAM
# lpNewItem : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f InsertMenuA (ANSI版) — 指定位置にメニュー項目を挿入する(ANSI版)。
公式の関連項目
- f DeleteMenu — メニューから項目を削除しサブメニューも破棄する。
- f DrawMenuBar — ウィンドウのメニューバーを再描画する。
- f InsertMenuItemW — 情報構造体を用いてメニュー項目を挿入する(Unicode版)。
- f ModifyMenuW — 既存のメニュー項目を変更する(Unicode版)。
- f RemoveMenu — メニューから項目を削除するがサブメニューは破棄しない。
- f SetMenuItemBitmaps — メニュー項目に表示するチェック用ビットマップを設定する。
使用する型