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

TrackPopupMenu

関数
指定位置にポップアップメニューを表示し選択を追跡する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// USER32.dll
#include <windows.h>

BOOL TrackPopupMenu(
    HMENU hMenu,
    TRACK_POPUP_MENU_FLAGS uFlags,
    INT x,
    INT y,
    INT nReserved,   // optional
    HWND hWnd,
    const RECT* prcRect   // optional
);

パラメーター

名前方向説明
hMenuHMENUin表示するショートカット(ポップアップ)メニューのハンドル。
uFlagsTRACK_POPUP_MENU_FLAGSin表示位置や選択通知方法を制御するフラグ。TPM_LEFTALIGN/TPM_RETURNCMD等を指定する。
xINTinメニューの水平位置(スクリーン座標)。
yINTinメニューの垂直位置(スクリーン座標)。
nReservedINToptional予約済み。必ず0を指定する。
hWndHWNDinメニューを所有しコマンド通知を受け取るウィンドウのハンドル。
prcRectRECT*inoptionalタップしてもメニューが消えない矩形へのポインター。不要ならNULL可。

戻り値の型: BOOL

各言語での呼び出し定義

// USER32.dll
#include <windows.h>

BOOL TrackPopupMenu(
    HMENU hMenu,
    TRACK_POPUP_MENU_FLAGS uFlags,
    INT x,
    INT y,
    INT nReserved,   // optional
    HWND hWnd,
    const RECT* prcRect   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool TrackPopupMenu(
    IntPtr hMenu,   // HMENU
    uint uFlags,   // TRACK_POPUP_MENU_FLAGS
    int x,   // INT
    int y,   // INT
    int nReserved,   // INT optional
    IntPtr hWnd,   // HWND
    IntPtr prcRect   // RECT* optional
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function TrackPopupMenu(
    hMenu As IntPtr,   ' HMENU
    uFlags As UInteger,   ' TRACK_POPUP_MENU_FLAGS
    x As Integer,   ' INT
    y As Integer,   ' INT
    nReserved As Integer,   ' INT optional
    hWnd As IntPtr,   ' HWND
    prcRect As IntPtr   ' RECT* optional
) As Boolean
End Function
' hMenu : HMENU
' uFlags : TRACK_POPUP_MENU_FLAGS
' x : INT
' y : INT
' nReserved : INT optional
' hWnd : HWND
' prcRect : RECT* optional
Declare PtrSafe Function TrackPopupMenu Lib "user32" ( _
    ByVal hMenu As LongPtr, _
    ByVal uFlags As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nReserved As Long, _
    ByVal hWnd As LongPtr, _
    ByVal prcRect As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

TrackPopupMenu = ctypes.windll.user32.TrackPopupMenu
TrackPopupMenu.restype = wintypes.BOOL
TrackPopupMenu.argtypes = [
    wintypes.HANDLE,  # hMenu : HMENU
    wintypes.DWORD,  # uFlags : TRACK_POPUP_MENU_FLAGS
    ctypes.c_int,  # x : INT
    ctypes.c_int,  # y : INT
    ctypes.c_int,  # nReserved : INT optional
    wintypes.HANDLE,  # hWnd : HWND
    ctypes.c_void_p,  # prcRect : RECT* optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
TrackPopupMenu = Fiddle::Function.new(
  lib['TrackPopupMenu'],
  [
    Fiddle::TYPE_VOIDP,  # hMenu : HMENU
    -Fiddle::TYPE_INT,  # uFlags : TRACK_POPUP_MENU_FLAGS
    Fiddle::TYPE_INT,  # x : INT
    Fiddle::TYPE_INT,  # y : INT
    Fiddle::TYPE_INT,  # nReserved : INT optional
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    Fiddle::TYPE_VOIDP,  # prcRect : RECT* optional
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn TrackPopupMenu(
        hMenu: *mut core::ffi::c_void,  // HMENU
        uFlags: u32,  // TRACK_POPUP_MENU_FLAGS
        x: i32,  // INT
        y: i32,  // INT
        nReserved: i32,  // INT optional
        hWnd: *mut core::ffi::c_void,  // HWND
        prcRect: *const RECT  // RECT* optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true)]
public static extern bool TrackPopupMenu(IntPtr hMenu, uint uFlags, int x, int y, int nReserved, IntPtr hWnd, IntPtr prcRect);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_TrackPopupMenu' -Namespace Win32 -PassThru
# $api::TrackPopupMenu(hMenu, uFlags, x, y, nReserved, hWnd, prcRect)
#uselib "USER32.dll"
#func global TrackPopupMenu "TrackPopupMenu" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; TrackPopupMenu hMenu, uFlags, x, y, nReserved, hWnd, varptr(prcRect)   ; 戻り値は stat
; hMenu : HMENU -> "sptr"
; uFlags : TRACK_POPUP_MENU_FLAGS -> "sptr"
; x : INT -> "sptr"
; y : INT -> "sptr"
; nReserved : INT optional -> "sptr"
; hWnd : HWND -> "sptr"
; prcRect : RECT* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global TrackPopupMenu "TrackPopupMenu" sptr, int, int, int, int, sptr, var
; res = TrackPopupMenu(hMenu, uFlags, x, y, nReserved, hWnd, prcRect)
; hMenu : HMENU -> "sptr"
; uFlags : TRACK_POPUP_MENU_FLAGS -> "int"
; x : INT -> "int"
; y : INT -> "int"
; nReserved : INT optional -> "int"
; hWnd : HWND -> "sptr"
; prcRect : RECT* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL TrackPopupMenu(HMENU hMenu, TRACK_POPUP_MENU_FLAGS uFlags, INT x, INT y, INT nReserved, HWND hWnd, RECT* prcRect)
#uselib "USER32.dll"
#cfunc global TrackPopupMenu "TrackPopupMenu" intptr, int, int, int, int, intptr, var
; res = TrackPopupMenu(hMenu, uFlags, x, y, nReserved, hWnd, prcRect)
; hMenu : HMENU -> "intptr"
; uFlags : TRACK_POPUP_MENU_FLAGS -> "int"
; x : INT -> "int"
; y : INT -> "int"
; nReserved : INT optional -> "int"
; hWnd : HWND -> "intptr"
; prcRect : RECT* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procTrackPopupMenu = user32.NewProc("TrackPopupMenu")
)

// hMenu (HMENU), uFlags (TRACK_POPUP_MENU_FLAGS), x (INT), y (INT), nReserved (INT optional), hWnd (HWND), prcRect (RECT* optional)
r1, _, err := procTrackPopupMenu.Call(
	uintptr(hMenu),
	uintptr(uFlags),
	uintptr(x),
	uintptr(y),
	uintptr(nReserved),
	uintptr(hWnd),
	uintptr(prcRect),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function TrackPopupMenu(
  hMenu: THandle;   // HMENU
  uFlags: DWORD;   // TRACK_POPUP_MENU_FLAGS
  x: Integer;   // INT
  y: Integer;   // INT
  nReserved: Integer;   // INT optional
  hWnd: THandle;   // HWND
  prcRect: Pointer   // RECT* optional
): BOOL; stdcall;
  external 'USER32.dll' name 'TrackPopupMenu';
result := DllCall("USER32\TrackPopupMenu"
    , "Ptr", hMenu   ; HMENU
    , "UInt", uFlags   ; TRACK_POPUP_MENU_FLAGS
    , "Int", x   ; INT
    , "Int", y   ; INT
    , "Int", nReserved   ; INT optional
    , "Ptr", hWnd   ; HWND
    , "Ptr", prcRect   ; RECT* optional
    , "Int")   ; return: BOOL
●TrackPopupMenu(hMenu, uFlags, x, y, nReserved, hWnd, prcRect) = DLL("USER32.dll", "bool TrackPopupMenu(void*, dword, int, int, int, void*, void*)")
# 呼び出し: TrackPopupMenu(hMenu, uFlags, x, y, nReserved, hWnd, prcRect)
# hMenu : HMENU -> "void*"
# uFlags : TRACK_POPUP_MENU_FLAGS -> "dword"
# x : INT -> "int"
# y : INT -> "int"
# nReserved : INT optional -> "int"
# hWnd : HWND -> "void*"
# prcRect : RECT* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。