Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

CreatePopupMenu (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern IntPtr CreatePopupMenu();

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    [DllImport("user32")]
        internal static extern uint CreatePopupMenu();

int IContextMenu.QueryContextMenu(uint hMenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags)

        {
            // Create the popup to insert
            uint hmnuPopup = Helpers.CreatePopupMenu();

            int id = 1;
            if ( (uFlags & 0xf) == 0 || (uFlags & (uint)CMF.CMF_EXPLORE) != 0)
            {
                uint nselected = Helpers.DragQueryFile(m_hDrop, 0xffffffff, null, 0);
                if (nselected == 1)
                {
                    StringBuilder sb = new StringBuilder(1024);
                    Helpers.DragQueryFile(m_hDrop, 0, sb, sb.Capacity + 1);
                    m_fileName = sb.ToString();

                    // Populate the popup menu with file-specific items
                    id = PopulateMenu(hmnuPopup, idCmdFirst+ id);
                }

                // Add the popup to the context menu
                MENUITEMINFO mii = new MENUITEMINFO();
                mii.cbSize = 48;
                mii.fMask = (uint) MIIM.TYPE | (uint)MIIM.STATE | (uint) MIIM.SUBMENU;
                mii.hSubMenu = (int) hmnuPopup;
                mii.fType = (uint) MF.STRING;
                mii.dwTypeData = "Actions";
                mii.fState = (uint) MF.ENABLED;
                Helpers.InsertMenuItem(hMenu, (uint)iMenu, 1, ref mii);

                // Add a separator
                MENUITEMINFO sep = new MENUITEMINFO();
                sep.cbSize = 48;
                sep.fMask = (uint )MIIM.TYPE;
                sep.fType = (uint) MF.SEPARATOR;
                Helpers.InsertMenuItem(hMenu, iMenu+1, 1, ref sep);

            }
            return id;
        }

void AddMenuItem(uint hMenu, string text, int id, uint position)

        {
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;
            mii.wID    = id;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData    = text;
            mii.fState = (uint)MF.ENABLED;
            Helpers.InsertMenuItem(hMenu, position, 1, ref mii);
        }

int PopulateMenu(uint hMenu, int id)

        {
            // Grab some info about the file
            bool done = ProcessBatchResults();

            if (done)
            {
                AddMenuItem(hMenu, "Add", id, 0);
                AddMenuItem(hMenu, "Test", ++id, 1);
                AddMenuItem(hMenu, "Do", ++id, 2);
            }
            else
            {
                // Uses 100-baed values to distinguish on the "done" status
                AddMenuItem(hMenu, "Commit", 100 + id, 0);
                AddMenuItem(hMenu, "Rollback", 100 + (++id), 1);
            }

            return id++;
        }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions