要在C#应用程序中显示Windows资源管理器上下文菜单,您可以使用Windows API(Win32 API)中的IContextMenu
接口。以下是一个简单的示例,说明如何实现此功能:
using System.Runtime.InteropServices;
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214e4-0000-0000-c000-000000000046")]
internal interface IContextMenu
{
[PreserveSig]
int QueryContextMenu(IntPtr hMenu, uint indexMenu, int idCmdFirst, int idCmdLast, uint uFlags);
[PreserveSig]
int InvokeCommand(ref CMINVOKECOMMANDINFOEX info);
[PreserveSig]
int GetCommandString(int idCmd, uint uType, IntPtr pReserved, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMax);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214f4-0000-0000-c000-000000000046")]
internal interface IShellFolder
{
void ParseDisplayName(IntPtr hwnd, IntPtr pbc, [MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName, ref int pchEaten, out IntPtr ppidl, ref int pdwAttributes);
void EnumObjects(IntPtr hwnd, uint grfFlags, out IEnumIDList ppenumIDList);
void BindToObject(IntPtr pidl, IntPtr pbc, [In] ref Guid riid, out IntPtr ppv);
void BindToStorage(IntPtr pidl, IntPtr pbc, [In] ref Guid riid, out IntPtr ppv);
void CompareIDs(IntPtr lParam, IntPtr pidl1, IntPtr pidl2);
void CreateViewObject(IntPtr hwndOwner, [In] ref Guid riid, out IntPtr ppv);
void GetAttributesOf(uint cidl, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, ref uint rgfInOut);
void GetUIObjectOf(IntPtr hwndOwner, uint cidl, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, [In] ref Guid riid, IntPtr rgfReserved, out IntPtr ppv);
void GetDisplayNameOf(IntPtr pidl, uint uFlags, out IntPtr pName);
void SetNameOf(IntPtr hwnd, IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] string pszName, uint uFlags, out IntPtr ppidlOut);
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct CMINVOKECOMMANDINFOEX
{
public int cbSize;
public int fMask;
public IntPtr hwnd;
public IntPtr lpVerb;
public string lpParameters;
public string lpDirectory;
public int nShow;
public int dwHotKey;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpTitle;
public IntPtr lpVerbW;
public string lpParametersW;
public string lpDirectoryW;
public string lpTitleW;
public int ptInvoke;
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F2-0000-0000-C000-000000000046")]
internal interface IEnumIDList
{
[PreserveSig]
int Next(uint celt, IntPtr rgelt, out uint pceltFetched);
[PreserveSig]
int Skip(uint celt);
[PreserveSig]
int Reset();
[PreserveSig]
int Clone(out IEnumIDList ppenum);
}
private static class NativeMethods
{
public const int CMF_NORMAL = 0x00000000;
public const int CMF_DEFAULTONLY = 0x00000001;
public const int CMF_VERBSONLY = 0x00000002;
public const int CMF_EXPLORE = 0x00000004;
public const int CMF_NOVERBS = 0x00000008;
public const int CMF_CANRENAME = 0x00000010;
public const int CMF_NODEFAULT = 0x00000020;
public const int CMF_INCLUDESTATIC = 0x00000040;
public const int CMF_EXTENDEDVERBS = 0x00000100;
public const int CMF_RESERVED = 0xffff0000;
public const int CMD_FIRST = 1;
public const int CMD_LAST = 30000;
public const int MFS_DISABLED = 0x00000003;
public const int MFS_ENABLED = 0x00000000;
public const int MFS_UNCHECKED = 0x00000001;
public const int MFS_UNHILITE = 0x00000000;
public const int MFS_CHECKED = 0x00000008;
public const int MFS_HILITE = 0x00000080;
public const int MFS_DEFAULT = 0x00001000;
public const int S_OK = 0;
public const int S_FALSE = 1;
public const
领取专属 10元无门槛券
手把手带您无忧上云