SysCntxtMenu.cs

Description du code

SysCntxtMenu.cs est un fichier du projet BrolDev.
Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.

Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.

Code source ou contenu du fichier

  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace be.gaudry.model.win32
  7. {
  8. public class SysCntxtMenu
  9. {
  10. private SysCntxtMenu()
  11. {
  12. //
  13. // All static, no instances
  14. //
  15. }
  16.  
  17. // CreatePopupMenu
  18. [DllImport("user32.dll", EntryPoint = "CreatePopupMenu", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
  19. public static extern IntPtr CreatePopupMenu();
  20.  
  21. // MessageBeep
  22. [DllImport("User32", EntryPoint = "MessageBeep", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
  23. public static extern bool MessageBeep(int SoundType);
  24.  
  25. // TrackPopupMenu
  26. [DllImport("user32.dll", EntryPoint = "TrackPopupMenu", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
  27. public static extern int TrackPopupMenu(IntPtr hMenu, int wFlags, int x, int y, int nReserved, IntPtr hwnd, out Rect lprc);
  28.  
  29. // SHGetDesktopFolder
  30. [DllImport("shell32.dll", EntryPoint = "SHGetDesktopFolder", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
  31. static extern void SHGetDesktopFolder(ref IShellFolder sf);
  32.  
  33. public const int MIN_SHELL_ID = 1;
  34. public const int MAX_SHELL_ID = 0x7FFF;
  35. //
  36. public const int TPM_RETURNCMD = 0x100;
  37. public const int TPM_LEFTALIGN = 0x0;
  38. //
  39. public const int SW_SHOWNORMAL = 1;
  40.  
  41. static int m_pidlCount = 0;
  42. static IntPtr[] m_ipList;
  43.  
  44. public static int MAKEINTRESOURCE(int res)
  45. {
  46. return 0x0000FFFF & res;
  47. }
  48.  
  49. public static int ShowContextMenu(IntPtr hwnd, string[] strFiles, int intX, int intY, bool blnErrorBeep)
  50. {
  51. CMINVOKECOMMANDINFO CI = new CMINVOKECOMMANDINFO();
  52.  
  53. DirectoryInfo dInfo;
  54.  
  55. FileInfo fInfo;
  56.  
  57. IContextMenu CM; // LPCONTEXTMENU
  58.  
  59. int cmdID; // LongBool
  60. int pchEaten = 0; // DWORD pchEaten;
  61. int dwAttributes = new int(); // ULONG
  62. int intZero = 0;
  63. int intCount;
  64.  
  65. IntPtr ipParent = new IntPtr(); // LPITEMIDLIST
  66. IntPtr ipChild = new IntPtr(); // LPITEMIDLIST
  67. IntPtr hMenu = new IntPtr(); // HMENU
  68. IntPtr ipPath = new IntPtr();
  69. IntPtr ipFileName = new IntPtr();
  70.  
  71. IShellFolder DesktopFolder = null; // IShellFolder
  72. IShellFolder ParentFolder = null; // IShellfolder ParentFolder
  73.  
  74. Rect mRect;
  75.  
  76. REFIID IID_IShellFolder = new REFIID("000214E6-0000-0000-c000-000000000046");
  77. REFIID IID_IContextMenu = new REFIID("000214E4-0000-0000-c000-000000000046");
  78.  
  79. string strFullName = "";
  80. string strFilePath = "";
  81. string strFileName = "";
  82. string strFQName = "";
  83.  
  84. m_pidlCount = 0;
  85.  
  86. for (intCount = 0; intCount <= strFiles.GetUpperBound(0); intCount++)
  87. {
  88. strFullName = strFiles[intCount];
  89.  
  90. fInfo = new FileInfo(strFullName);
  91.  
  92. if (fInfo.Exists)
  93. {
  94. strFilePath = fInfo.DirectoryName;
  95. strFileName = fInfo.Name;
  96. strFQName = fInfo.FullName;
  97. }
  98. else
  99. {
  100. dInfo = new DirectoryInfo(strFullName);
  101. if (dInfo.Exists)
  102. {
  103. try
  104. {
  105. strFileName = dInfo.Name;
  106. strFilePath = dInfo.Parent.FullName;
  107.  
  108. }
  109. catch
  110. {
  111. strFilePath = "";
  112. }
  113. }
  114. }
  115.  
  116. SHGetDesktopFolder(ref DesktopFolder);
  117.  
  118. // ParseDisplayName - parent
  119. // Translates a file object's or folder's display name into an item identifier list
  120. pchEaten = 1;
  121. dwAttributes = 0;
  122. ipParent = new IntPtr();
  123. DesktopFolder.ParseDisplayName(hwnd, IntPtr.Zero, strFilePath, ref pchEaten, ref ipParent, ref dwAttributes);
  124.  
  125. // BindToObject
  126. // Retrieves an IShellFolder object for a subfolder
  127. ParentFolder = null;
  128. DesktopFolder.BindToObject(ipParent, IntPtr.Zero, ref IID_IShellFolder, ref ParentFolder);
  129.  
  130. // ParseDisplayName - child
  131. // Translates a file object's or folder's display name into an item identifier list
  132. pchEaten = 1;
  133. dwAttributes = 0;
  134. ipChild = new IntPtr();
  135. ParentFolder.ParseDisplayName(hwnd, IntPtr.Zero, strFileName, ref pchEaten, ref ipChild, ref dwAttributes);
  136.  
  137. JAddItemToIDList(ipChild);
  138. }
  139.  
  140. CM = null;
  141.  
  142. int intReturn = ParentFolder.GetUIObjectOf(hwnd, m_pidlCount, ref m_ipList[0], ref IID_IContextMenu, out intZero, ref CM);
  143.  
  144. if (CM != null)
  145. {
  146. hMenu = CreatePopupMenu();
  147.  
  148. CM.QueryContextMenu(hMenu, 0, MIN_SHELL_ID, MAX_SHELL_ID, QueryContextMenuFlags.CMF_EXPLORE);
  149.  
  150. cmdID = TrackPopupMenu(hMenu, (TPM_RETURNCMD | TPM_LEFTALIGN), intX, intY, 0, hwnd, out mRect);
  151.  
  152. if (cmdID != 0)
  153. {
  154. CI.cbSize = Marshal.SizeOf(CI);
  155. CI.hwnd = hwnd;
  156. CI.lpVerb = (IntPtr)MAKEINTRESOURCE(cmdID - 1);
  157. CI.lpParameters = IntPtr.Zero;
  158. CI.lpDirectory = IntPtr.Zero;
  159. CI.nShow = SW_SHOWNORMAL;
  160. CM.InvokeCommand(ref CI);
  161. }
  162. }
  163. else
  164. {
  165. if (blnErrorBeep)
  166. MessageBeep(-1);
  167. }
  168. return 0;
  169. }
  170.  
  171. private static void JAddItemToIDList(IntPtr ipNew)
  172. {
  173. if (m_pidlCount == 0)
  174. {
  175. m_ipList = new IntPtr[1];
  176. m_ipList[0] = ipNew;
  177. m_pidlCount++;
  178. return;
  179. }
  180.  
  181. int intCount;
  182. IntPtr[] ipTemp = new IntPtr[m_pidlCount];
  183.  
  184. for (intCount = 0; intCount < m_pidlCount; intCount++)
  185. ipTemp[intCount] = m_ipList[intCount];
  186.  
  187. m_ipList = new IntPtr[m_pidlCount + 1];
  188.  
  189. for (intCount = 0; intCount < m_pidlCount; intCount++)
  190. m_ipList[intCount] = ipTemp[intCount];
  191.  
  192. m_ipList[intCount] = ipNew;
  193. m_pidlCount++;
  194. }
  195. }
  196.  
  197. [ComImport, Guid("00000000-0000-0000-c000-000000000046")]
  198. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  199. public interface IUnknown
  200. {
  201. [PreserveSig]
  202. IntPtr QueryInterface(REFIID riid, out IntPtr pVoid);
  203.  
  204. [PreserveSig]
  205. IntPtr AddRef();
  206.  
  207. [PreserveSig]
  208. IntPtr Release();
  209. }
  210.  
  211. public struct Rect
  212. {
  213. public int left;
  214. public int top;
  215. public int right;
  216. public int bottom;
  217.  
  218. public int Width
  219. {
  220. get
  221. {
  222. return right - left;
  223. }
  224. }
  225.  
  226. public int Height
  227. {
  228. get
  229. {
  230. return bottom - top;
  231. }
  232. }
  233. }
  234.  
  235. [ComImport, Guid("000214E6-0000-0000-c000-000000000046")]
  236. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  237. public interface IShellFolder
  238. {
  239. [PreserveSig]
  240. int ParseDisplayName(IntPtr hwnd, IntPtr pbc, string pszDisplayName, ref int pchEaten, ref IntPtr ppidl, ref int pdwAttributes);
  241.  
  242. [PreserveSig]
  243. int EnumObjects(IntPtr hWnd, ShellEnumFlags flags, ref IEnumIDList enumList);
  244.  
  245. [PreserveSig]
  246. int BindToObject(IntPtr idList, IntPtr bindingContext, ref REFIID refiid, ref IShellFolder folder);
  247.  
  248. [PreserveSig]
  249. int BindToStorage(ref IntPtr idList, IntPtr bindingContext, ref REFIID riid, IntPtr pVoid);
  250.  
  251. [PreserveSig]
  252. int CompareIDs(int lparam, IntPtr idList1, IntPtr idList2);
  253.  
  254. [PreserveSig]
  255. int CreateViewObject(IntPtr hWnd, REFIID riid, IntPtr pVoid);
  256.  
  257. [PreserveSig]
  258. int GetAttributesOf(int count, ref IntPtr idList, out GetAttributeOfFlags properties);
  259.  
  260. [PreserveSig]
  261. int GetUIObjectOf(IntPtr hwnd, int cidl, ref IntPtr apidl, ref REFIID riid, out int rgfReserved, ref IContextMenu ppv);
  262.  
  263. [PreserveSig]
  264. int GetDisplayNameOf(IntPtr idList, ShellGetDisplayNameOfFlags flags, ref STRRET strRet);
  265.  
  266. [PreserveSig]
  267. int SetNameOf(IntPtr hWnd, ref IntPtr idList, IntPtr pOLEString, int flags, ref IntPtr pItemIDList);
  268.  
  269. }
  270.  
  271. [ComImport, Guid("000214f2-0000-0000-c000-000000000046")]
  272. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  273. public interface IEnumIDList
  274. {
  275. [PreserveSig]
  276. int Next(int count, ref IntPtr idList, out int fetched);
  277.  
  278. [PreserveSig]
  279. int Skip(int count);
  280.  
  281. [PreserveSig]
  282. int Reset();
  283.  
  284. [PreserveSig]
  285. int Clone(ref IEnumIDList list);
  286. }
  287.  
  288. [ComImport, Guid("000214e4-0000-0000-c000-000000000046")]
  289. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  290. public interface IContextMenu
  291. {
  292. [PreserveSig]
  293. int QueryContextMenu(IntPtr hMenu, int indexMenu, int idFirstCommand, int idLastCommand, QueryContextMenuFlags flags);
  294.  
  295. [PreserveSig]
  296. int InvokeCommand(ref CMINVOKECOMMANDINFO ici);
  297.  
  298. [PreserveSig]
  299. int GetCommandString(int idCommand, int type, int reserved, string commandName, int cchMax);
  300. }
  301.  
  302. [StructLayout(LayoutKind.Sequential)]
  303. public struct REFIID
  304. {
  305. public int x;
  306. public short s1;
  307. public short s2;
  308. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 8)]
  309. public byte[] chars;
  310.  
  311. public REFIID(string guid)
  312. {
  313. // Needs to be a string of the form:
  314. // "000214E6-0000-0000-c000-000000000046"
  315. string[] data = guid.Split('-');
  316. Debug.Assert(data.Length == 5);
  317. x = Convert.ToInt32(data[0], 16);
  318. s1 = Convert.ToInt16(data[1], 16);
  319. s2 = Convert.ToInt16(data[2], 16);
  320. string bytesData = data[3] + data[4];
  321. chars = new byte[] { Convert.ToByte(bytesData.Substring(0,2), 16), Convert.ToByte(bytesData.Substring(2,2), 16),
  322. Convert.ToByte(bytesData.Substring(4,2), 16), Convert.ToByte(bytesData.Substring(6,2), 16),
  323. Convert.ToByte(bytesData.Substring(8,2), 16), Convert.ToByte(bytesData.Substring(10,2), 16),
  324. Convert.ToByte(bytesData.Substring(12,2), 16), Convert.ToByte(bytesData.Substring(14,2), 16) };
  325. }
  326.  
  327. }
  328.  
  329.  
  330. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  331. public struct STRRET
  332. {
  333. public STRRETFlags uType; // One of the STRRET values
  334. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 1024)]
  335. public byte[] cStr;
  336. }
  337.  
  338. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  339. public struct CMINVOKECOMMANDINFO
  340. {
  341. public int cbSize; // sizeof(CMINVOKECOMMANDINFO)
  342. public int fMask; // any combination of CMIC_MASK_*
  343. public IntPtr hwnd; // might be NULL (indicating no owner window)
  344. public IntPtr lpVerb; // either a string or MAKEINTRESOURCE(idOffset)
  345. public IntPtr lpParameters; // might be NULL (indicating no parameter)
  346. public IntPtr lpDirectory; // might be NULL (indicating no specific directory)
  347. public int nShow; // one of SW_ values for ShowWindow() API
  348. public int dwHotKey;
  349. public IntPtr hIcon;
  350. }
  351.  
  352.  
  353. [StructLayout(LayoutKind.Sequential)]
  354. public struct SHITEMID
  355. {
  356. public short cb;
  357. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 1)]
  358. public byte[] abID;
  359. }
  360.  
  361. [StructLayout(LayoutKind.Sequential)]
  362. public struct ITEMIDLIST
  363. {
  364. public SHITEMID mkid;
  365. }
  366.  
  367. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  368. public struct POINT
  369. {
  370. public int x;
  371. public int y;
  372. };
  373.  
  374. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  375. public struct RECT
  376. {
  377. public int Left;
  378. public int Top;
  379. public int Right;
  380. public int Bottom;
  381. }
  382.  
  383. // Enums
  384. [Flags]
  385. public enum ShellEnumFlags
  386. {
  387. SHCONTF_FOLDERS = 32, // for shell browser
  388. SHCONTF_NONFOLDERS = 64, // for default view
  389. SHCONTF_INCLUDEHIDDEN = 128, // for hidden/system objects
  390. }
  391.  
  392. [Flags]
  393. public enum ShellGetDisplayNameOfFlags
  394. {
  395. SHGDN_NORMAL = 0, // default (display purpose)
  396. SHGDN_INFOLDER = 1, // displayed under a folder (relative)
  397. SHGDN_INCLUDE_NONFILESYS = 0x2000, // if not set, display names for shell name space items that are not in the file system will fail.
  398. SHGDN_FORADDRESSBAR = 0x4000, // for displaying in the address (drives dropdown) bar
  399. SHGDN_FORPARSING = 0x8000, // for ParseDisplayName or path
  400. }
  401.  
  402. [Flags]
  403. public enum STRRETFlags
  404. {
  405. STRRET_WSTR = 0x0000, // Use STRRET.pOleStr
  406. STRRET_OFFSET = 0x0001, // Use STRRET.uOffset to Ansi
  407. STRRET_CSTR = 0x0002 // Use STRRET.cStr
  408. }
  409.  
  410. [Flags]
  411. public enum GetAttributeOfFlags : long
  412. {
  413. DROPEFFECT_NONE = 0,
  414. DROPEFFECT_COPY = 1,
  415. DROPEFFECT_MOVE = 2,
  416. DROPEFFECT_LINK = 4,
  417. DROPEFFECT_SCROLL = 0x80000000,
  418. SFGAO_CANCOPY = DROPEFFECT_COPY, // Objects can be copied
  419. SFGAO_CANMOVE = DROPEFFECT_MOVE, // Objects can be moved
  420. SFGAO_CANLINK = DROPEFFECT_LINK, // Objects can be linked
  421. SFGAO_CANRENAME = 0x00000010, // Objects can be renamed
  422. SFGAO_CANDELETE = 0x00000020, // Objects can be deleted
  423. SFGAO_HASPROPSHEET = 0x00000040, // Objects have property sheets
  424. SFGAO_DROPTARGET = 0x00000100, // Objects are drop target
  425. SFGAO_CAPABILITYMASK = 0x00000177,
  426. SFGAO_LINK = 0x00010000, // Shortcut (link)
  427. SFGAO_SHARE = 0x00020000, // shared
  428. SFGAO_READONLY = 0x00040000, // read-only
  429. SFGAO_GHOSTED = 0x00080000, // ghosted icon
  430. SFGAO_HIDDEN = 0x00080000, // hidden object
  431. SFGAO_DISPLAYATTRMASK = 0x000F0000,
  432. SFGAO_FILESYSANCESTOR = 0x10000000, // It contains file system folder
  433. SFGAO_FOLDER = 0x20000000, // It's a folder.
  434. SFGAO_FILESYSTEM = 0x40000000, // is a file system thing (file/folder/root)
  435. SFGAO_HASSUBFOLDER = 0x80000000, // Expandable in the map pane
  436. SFGAO_CONTENTSMASK = 0x80000000,
  437. SFGAO_VALIDATE = 0x01000000, // invalidate cached information
  438. SFGAO_REMOVABLE = 0x02000000, // is this removeable media?
  439. SFGAO_COMPRESSED = 0x04000000, // Object is compressed (use alt color)
  440. SFGAO_BROWSABLE = 0x08000000, // is in-place browsable
  441. SFGAO_NONENUMERATED = 0x00100000, // is a non-enumerated object
  442. SFGAO_NEWCONTENT = 0x00200000 // should show bold in explorer tree
  443. }
  444.  
  445. public enum QueryContextMenuFlags : long
  446. {
  447. CMF_NORMAL = 0x00000000,
  448. CMF_DEFAULTONLY = 0x00000001,
  449. CMF_VERBSONLY = 0x00000002,
  450. CMF_EXPLORE = 0x00000004,
  451. CMF_NOVERBS = 0x00000008,
  452. CMF_CANRENAME = 0x00000010,
  453. CMF_NODEFAULT = 0x00000020,
  454. CMF_INCLUDESTATIC = 0x00000040,
  455. CMF_RESERVED = 0xffff0000
  456. }
  457. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/model/win32/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1732295727 22/11/2024 18:15:27
Fichiers contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/model/win32/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .cs|.csSysCntxtMenu.cs15.84 Ko31/10/2018 18:33:10-refusé-
Afficher le fichier .cs|.csShellExecuteInfo.cs564 octets31/10/2018 18:33:10-refusé-
Afficher le fichier .cs|.csShell32Helper.cs1.05 Ko31/10/2018 18:33:10-refusé-
Afficher le fichier .cs|.csUser32Helper.cs930 octets31/10/2018 18:33:10-refusé-

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-model/win32//SysCntxtMenu.cs.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.