TrackPopupMenuEx (coredll)
Last changed: -109.78.197.28

.
Summary
This function displays a floating pop-up menu at the specified location and tracks the selection of items on the pop-up menu. The floating pop-up menu can appear anywhere on the screen.

C# Signature:

[DllImport("user32.dll", SetLastError=true)]
static extern uint TrackPopupMenuEx(IntPtr hMenu, uint uFlags, int x, int y, IntPtr hWnd, IntPtr tpmParams);

User-Defined Types:

uFlags:

    uint TPM_LEFTALIGN = 0x0000;
    uint TPM_CENTERALIGN = 0x0004;
    uint TPM_RIGHTALIGN = 0x0008;
    uint TPM_TOPALIGN = 0x0000;
    uint TPM_VCENTERALIGN = 0x0010;
    uint TPM_BOTTOMALIGN = 0x0020;
    uint TPM_HORIZONTAL = 0x0000;
    uint TPM_VERTICAL = 0x0040;
    uint TPM_RETURNCMD = 0x0100;

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Tips & Tricks:

Use TrackPopupMenuEx with the TPM_RETURNCMD flag to return the ID of the selected Menu Item.

Sample Code:

    MessageWindow eventSink = new MessageWindow();
    IntPtr hMenu = CreatePopUpMenu();

    // Add some items...
    AppendMenu(hMenu, MF_STRING, 1001, "Item 1");

    // Display a Pop Up menu at the bottom right corner of the working area of the screen
    // growing from bottom to top
    uint menuItemID = TrackPopupMenuEx(hMenu, TPM_BOTTOMALIGN | TPM_RETURNCMD, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height, eventSink.Hwnd, IntPtr.Zero);

    // Use the menuItemID to fire some event...

Documentation