root / trunk / Pithos.ShellExtensions / Menus / FileContextMenu.cs @ 42800be8
History | View | Annotate | Download (24.6 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel.Composition; |
4 |
using System.Diagnostics; |
5 |
using System.Diagnostics.Contracts; |
6 |
using System.Drawing; |
7 |
using System.Linq; |
8 |
using System.Runtime.InteropServices; |
9 |
using System.Runtime.InteropServices.ComTypes; |
10 |
using System.Text; |
11 |
using Pithos.ShellExtensions.Properties; |
12 |
|
13 |
namespace Pithos.ShellExtensions.Menus |
14 |
{ |
15 |
[ClassInterface(ClassInterfaceType.None)] |
16 |
[Guid("B1F1405D-94A1-4692-B72F-FC8CAF8B8700"), ComVisible(true)] |
17 |
public class FileContextMenu : IShellExtInit, IContextMenu |
18 |
{ |
19 |
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos.FileContextMenu"); |
20 |
|
21 |
private const string MenuHandlername = "Pithos.FileContextMenu"; |
22 |
|
23 |
|
24 |
private readonly Dictionary<string, MenuItem> _items; |
25 |
|
26 |
|
27 |
[Import] |
28 |
public FileContext Context { get; set; } |
29 |
|
30 |
private IntPtr _gotoBitmap=IntPtr.Zero; |
31 |
private IntPtr _versionBitmap = IntPtr.Zero; |
32 |
private IntPtr _propertiesBitmap = IntPtr.Zero; |
33 |
|
34 |
public FileContextMenu() |
35 |
{ |
36 |
_gotoBitmap = GetBitmapPtr(Resources.MenuGoToPithos); |
37 |
_versionBitmap = GetBitmapPtr(Resources.MenuHistory); |
38 |
_propertiesBitmap = GetBitmapPtr(Resources.MenuProperties); |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
_items = new Dictionary<string, MenuItem>{ |
44 |
{"gotoPithos",new MenuItem{ |
45 |
MenuText = "&Go to Pithos", |
46 |
Verb = "gotoPithos", |
47 |
VerbCanonicalName = "PITHOSGoTo", |
48 |
VerbHelpText = "Go to Pithos", |
49 |
MenuDisplayId = 1, |
50 |
MenuCommand=OnGotoPithos, |
51 |
DisplayFlags=DisplayFlags.All, |
52 |
MenuBitmap = _gotoBitmap |
53 |
}}, |
54 |
{"showProperties",new MenuItem{ |
55 |
MenuText = "&Pithos Properties", |
56 |
Verb = "showProperties", |
57 |
VerbCanonicalName = "PITHOSProperties", |
58 |
VerbHelpText = "Pithos Properties", |
59 |
MenuDisplayId = 2, |
60 |
MenuCommand=OnShowProperties, |
61 |
DisplayFlags=DisplayFlags.All, |
62 |
MenuBitmap = _propertiesBitmap |
63 |
}}/*, |
64 |
{"prevVersions",new MenuItem{ |
65 |
MenuText = "&Show Previous Versions", |
66 |
Verb = "prevVersions", |
67 |
VerbCanonicalName = "PITHOSPrevVersions", |
68 |
VerbHelpText = "Go to Pithos and display previous versions", |
69 |
MenuDisplayId = 3, |
70 |
MenuCommand=OnVerbDisplayFileName, |
71 |
DisplayFlags=DisplayFlags.File, |
72 |
MenuBitmap=_versionBitmap |
73 |
}}*/ |
74 |
}; |
75 |
|
76 |
IoC.Current.Compose(this); |
77 |
|
78 |
|
79 |
} |
80 |
|
81 |
|
82 |
~FileContextMenu() |
83 |
{ |
84 |
if (_gotoBitmap != IntPtr.Zero) |
85 |
{ |
86 |
NativeMethods.DeleteObject(_gotoBitmap); |
87 |
_gotoBitmap= IntPtr.Zero; |
88 |
} |
89 |
if (_versionBitmap != IntPtr.Zero) |
90 |
{ |
91 |
NativeMethods.DeleteObject(_versionBitmap); |
92 |
_versionBitmap = IntPtr.Zero; |
93 |
} |
94 |
if (_propertiesBitmap != IntPtr.Zero) |
95 |
{ |
96 |
NativeMethods.DeleteObject(_propertiesBitmap); |
97 |
_propertiesBitmap = IntPtr.Zero; |
98 |
} |
99 |
|
100 |
} |
101 |
private static IntPtr GetBitmapPtr(Bitmap gotoBitmap) |
102 |
{ |
103 |
gotoBitmap.MakeTransparent(gotoBitmap.GetPixel(0, 0)); |
104 |
var hbitmap = gotoBitmap.GetHbitmap(); |
105 |
return hbitmap; |
106 |
} |
107 |
|
108 |
void OnShowProperties(IntPtr hWnd) |
109 |
{ |
110 |
var filePath = Context.CurrentFile ?? Context.CurrentFolder; |
111 |
if (String.IsNullOrWhiteSpace(filePath)) |
112 |
{ |
113 |
Debug.WriteLine("No current file or folder"); |
114 |
return; |
115 |
} |
116 |
else |
117 |
{ |
118 |
Debug.WriteLine("Will display properties for {0}",filePath); |
119 |
} |
120 |
|
121 |
using (var client = PithosHost.GetCommandsClient()) |
122 |
{ |
123 |
client.ShowProperties(Context.CurrentFile); |
124 |
} |
125 |
} |
126 |
|
127 |
void OnVerbDisplayFileName(IntPtr hWnd) |
128 |
{ |
129 |
string message = String.Format("The selected file is {0}\r\n\r\nThe selected Path is {1}", |
130 |
Context.CurrentFile, |
131 |
Context.CurrentFolder); |
132 |
|
133 |
System.Windows.Forms.MessageBox.Show( |
134 |
message, |
135 |
"Pithos Shell Extensions"); |
136 |
NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, |
137 |
IntPtr.Zero, IntPtr.Zero); |
138 |
} |
139 |
|
140 |
void OnGotoPithos(IntPtr hWnd) |
141 |
{ |
142 |
var settings = Context.Settings; |
143 |
var activeAccount = settings.Accounts.FirstOrDefault(acc => Context.CurrentFile.StartsWith(acc.RootPath,StringComparison.InvariantCultureIgnoreCase)); |
144 |
var address = String.Format("{0}/ui/?token={1}&user={2}", |
145 |
settings.PithosSite, |
146 |
activeAccount.ApiKey, |
147 |
Uri.EscapeUriString(activeAccount.AccountName)); |
148 |
|
149 |
settings.Reload(); |
150 |
Process.Start(address); |
151 |
} |
152 |
|
153 |
|
154 |
#region Shell Extension Registration |
155 |
|
156 |
[ComRegisterFunction] |
157 |
public static void Register(Type t) |
158 |
{ |
159 |
try |
160 |
{ |
161 |
ShellExtReg.RegisterShellExtContextMenuHandler(t.GUID, ".cs", |
162 |
MenuHandlername); |
163 |
ShellExtReg.RegisterShellExtContextMenuHandler(t.GUID, "Directory", |
164 |
MenuHandlername); |
165 |
ShellExtReg.RegisterShellExtContextMenuHandler(t.GUID, @"Directory\Background", |
166 |
MenuHandlername); |
167 |
ShellExtReg.RegisterShellExtContextMenuHandler(t.GUID, "*", |
168 |
MenuHandlername); |
169 |
|
170 |
//ShellExtReg.MarkApproved(t.GUID, MenuHandlername); |
171 |
} |
172 |
catch (Exception ex) |
173 |
{ |
174 |
Console.WriteLine(ex.Message); // Log the error |
175 |
throw; // Re-throw the exception |
176 |
} |
177 |
} |
178 |
|
179 |
[ComUnregisterFunction] |
180 |
public static void Unregister(Type t) |
181 |
{ |
182 |
try |
183 |
{ |
184 |
ShellExtReg.UnregisterShellExtContextMenuHandler(t.GUID, ".cs", MenuHandlername); |
185 |
ShellExtReg.UnregisterShellExtContextMenuHandler(t.GUID, "Directory", MenuHandlername); |
186 |
ShellExtReg.UnregisterShellExtContextMenuHandler(t.GUID, @"Directory\Background", MenuHandlername); |
187 |
ShellExtReg.UnregisterShellExtContextMenuHandler(t.GUID, "*", MenuHandlername); |
188 |
|
189 |
//ShellExtReg.RemoveApproved(t.GUID, MenuHandlername); |
190 |
} |
191 |
catch (Exception ex) |
192 |
{ |
193 |
Console.WriteLine(ex.Message); // Log the error |
194 |
throw; // Re-throw the exception |
195 |
} |
196 |
} |
197 |
|
198 |
#endregion |
199 |
|
200 |
|
201 |
#region IShellExtInit Members |
202 |
|
203 |
/// <summary> |
204 |
/// Initialize the context menu handler. |
205 |
/// </summary> |
206 |
/// <param name="pidlFolder"> |
207 |
/// A pointer to an ITEMIDLIST structure that uniquely identifies a folder. |
208 |
/// </param> |
209 |
/// <param name="pDataObj"> |
210 |
/// A pointer to an IDataObject interface object that can be used to retrieve |
211 |
/// the objects being acted upon. |
212 |
/// </param> |
213 |
/// <param name="hKeyProgID"> |
214 |
/// The registry key for the file object or folder type. |
215 |
/// </param> |
216 |
public void Initialize(IntPtr pidlFolder, IntPtr pDataObj, IntPtr hKeyProgID) |
217 |
{ |
218 |
|
219 |
if(pDataObj == IntPtr.Zero && pidlFolder == IntPtr.Zero) |
220 |
{ |
221 |
throw new ArgumentException("pidlFolder and pDataObj shouldn't be null at the same time"); |
222 |
} |
223 |
|
224 |
|
225 |
Debug.WriteLine("Initializing", LogCategories.ShellMenu); |
226 |
|
227 |
if (pDataObj != IntPtr.Zero) |
228 |
{ |
229 |
Debug.WriteLine("Got a data object", LogCategories.ShellMenu); |
230 |
|
231 |
FORMATETC fe = new FORMATETC(); |
232 |
fe.cfFormat = (short)CLIPFORMAT.CF_HDROP; |
233 |
fe.ptd = IntPtr.Zero; |
234 |
fe.dwAspect = DVASPECT.DVASPECT_CONTENT; |
235 |
fe.lindex = -1; |
236 |
fe.tymed = TYMED.TYMED_HGLOBAL; |
237 |
STGMEDIUM stm = new STGMEDIUM(); |
238 |
|
239 |
// The pDataObj pointer contains the objects being acted upon. In this |
240 |
// example, we get an HDROP handle for enumerating the selected files |
241 |
// and folders. |
242 |
IDataObject dataObject = (IDataObject)Marshal.GetObjectForIUnknown(pDataObj); |
243 |
dataObject.GetData(ref fe, out stm); |
244 |
|
245 |
try |
246 |
{ |
247 |
// Get an HDROP handle. |
248 |
IntPtr hDrop = stm.unionmember; |
249 |
if (hDrop == IntPtr.Zero) |
250 |
{ |
251 |
throw new ArgumentException(); |
252 |
} |
253 |
|
254 |
// Determine how many files are involved in this operation. |
255 |
uint nFiles = NativeMethods.DragQueryFile(hDrop, UInt32.MaxValue, null, 0); |
256 |
|
257 |
Debug.WriteLine(String.Format("Got {0} files", nFiles), LogCategories.ShellMenu); |
258 |
// This code sample displays the custom context menu item when only |
259 |
// one file is selected. |
260 |
if (nFiles == 1) |
261 |
{ |
262 |
// Get the path of the file. |
263 |
var fileName = new StringBuilder(260); |
264 |
if (0 == NativeMethods.DragQueryFile(hDrop, 0, fileName, |
265 |
fileName.Capacity)) |
266 |
{ |
267 |
Marshal.ThrowExceptionForHR(WinError.E_FAIL); |
268 |
} |
269 |
Context.CurrentFile = fileName.ToString(); |
270 |
} |
271 |
/* else |
272 |
{ |
273 |
Marshal.ThrowExceptionForHR(WinError.E_FAIL); |
274 |
}*/ |
275 |
|
276 |
// [-or-] |
277 |
|
278 |
// Enumerate the selected files and folders. |
279 |
//if (nFiles > 0) |
280 |
//{ |
281 |
// StringCollection selectedFiles = new StringCollection(); |
282 |
// StringBuilder fileName = new StringBuilder(260); |
283 |
// for (uint i = 0; i < nFiles; i++) |
284 |
// { |
285 |
// // Get the next file name. |
286 |
// if (0 != NativeMethods.DragQueryFile(hDrop, i, fileName, |
287 |
// fileName.Capacity)) |
288 |
// { |
289 |
// // Add the file name to the list. |
290 |
// selectedFiles.Add(fileName.ToString()); |
291 |
// } |
292 |
// } |
293 |
// |
294 |
// // If we did not find any files we can work with, throw |
295 |
// // exception. |
296 |
// if (selectedFiles.Count == 0) |
297 |
// { |
298 |
// Marshal.ThrowExceptionForHR(WinError.E_FAIL); |
299 |
// } |
300 |
//} |
301 |
//else |
302 |
//{ |
303 |
// Marshal.ThrowExceptionForHR(WinError.E_FAIL); |
304 |
//} |
305 |
} |
306 |
finally |
307 |
{ |
308 |
NativeMethods.ReleaseStgMedium(ref stm); |
309 |
} |
310 |
} |
311 |
|
312 |
if (pidlFolder != IntPtr.Zero) |
313 |
{ |
314 |
Debug.WriteLine("Got a folder", LogCategories.ShellMenu); |
315 |
StringBuilder path = new StringBuilder(); |
316 |
if (!NativeMethods.SHGetPathFromIDList(pidlFolder, path)) |
317 |
{ |
318 |
int error = Marshal.GetHRForLastWin32Error(); |
319 |
Marshal.ThrowExceptionForHR(error); |
320 |
} |
321 |
Context.CurrentFolder = path.ToString(); |
322 |
Debug.WriteLine(String.Format("Folder is {0}", Context.CurrentFolder), LogCategories.ShellMenu); |
323 |
} |
324 |
} |
325 |
|
326 |
#endregion |
327 |
|
328 |
|
329 |
#region IContextMenu Members |
330 |
|
331 |
/// <summary> |
332 |
/// Add commands to a shortcut menu. |
333 |
/// </summary> |
334 |
/// <param name="hMenu">A handle to the shortcut menu.</param> |
335 |
/// <param name="iMenu"> |
336 |
/// The zero-based position at which to insert the first new menu item. |
337 |
/// </param> |
338 |
/// <param name="idCmdFirst"> |
339 |
/// The minimum value that the handler can specify for a menu item ID. |
340 |
/// </param> |
341 |
/// <param name="idCmdLast"> |
342 |
/// The maximum value that the handler can specify for a menu item ID. |
343 |
/// </param> |
344 |
/// <param name="uFlags"> |
345 |
/// Optional flags that specify how the shortcut menu can be changed. |
346 |
/// </param> |
347 |
/// <returns> |
348 |
/// If successful, returns an HRESULT value that has its severity value set |
349 |
/// to SEVERITY_SUCCESS and its code value set to the offset of the largest |
350 |
/// command identifier that was assigned, plus one. |
351 |
/// </returns> |
352 |
public int QueryContextMenu( |
353 |
IntPtr hMenu, |
354 |
uint iMenu, |
355 |
uint idCmdFirst, |
356 |
uint idCmdLast, |
357 |
uint uFlags) |
358 |
{ |
359 |
Debug.WriteLine("Start qcm", LogCategories.ShellMenu); |
360 |
// If uFlags include CMF_DEFAULTONLY then we should not do anything. |
361 |
Debug.WriteLine(String.Format("Flags {0}", uFlags), LogCategories.ShellMenu); |
362 |
|
363 |
if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) |
364 |
{ |
365 |
Debug.WriteLine("Default only flag, returning", LogCategories.ShellMenu); |
366 |
return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0); |
367 |
} |
368 |
|
369 |
if (!Context.IsManaged) |
370 |
{ |
371 |
Debug.WriteLine("Not a PITHOS folder",LogCategories.ShellMenu); |
372 |
return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0); |
373 |
} |
374 |
|
375 |
/* |
376 |
if (!selectedFolder.ToLower().Contains("pithos")) |
377 |
return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0); |
378 |
*/ |
379 |
|
380 |
// Use either InsertMenu or InsertMenuItem to add menu items. |
381 |
|
382 |
uint largestID = 0; |
383 |
|
384 |
DisplayFlags itemType = (Context.IsFolder) ? DisplayFlags.Folder : DisplayFlags.File; |
385 |
|
386 |
Debug.WriteLine(String.Format("Item Flags {0}", itemType), LogCategories.ShellMenu); |
387 |
|
388 |
if (!NativeMethods.InsertMenu(hMenu, idCmdFirst, MF.MF_SEPARATOR | MF.MF_BYPOSITION, 0, String.Empty)) |
389 |
{ |
390 |
Log.ErrorFormat("Error adding separator 1\r\n{0}", Marshal.GetLastWin32Error()); |
391 |
return Marshal.GetHRForLastWin32Error(); |
392 |
} |
393 |
|
394 |
foreach (var menuItem in _items.Values) |
395 |
{ |
396 |
Debug.WriteLine(String.Format("Menu Flags {0}", menuItem.DisplayFlags), LogCategories.ShellMenu); |
397 |
if ((itemType & menuItem.DisplayFlags) != DisplayFlags.None) |
398 |
{ |
399 |
Debug.WriteLine("Adding Menu", LogCategories.ShellMenu); |
400 |
|
401 |
MENUITEMINFO mii = menuItem.CreateInfo(idCmdFirst); |
402 |
if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) |
403 |
{ |
404 |
var lastError = Marshal.GetLastWin32Error(); |
405 |
var lastErrorHR = Marshal.GetHRForLastWin32Error(); |
406 |
return lastErrorHR; |
407 |
} |
408 |
if (largestID < menuItem.MenuDisplayId) |
409 |
largestID = menuItem.MenuDisplayId; |
410 |
} |
411 |
} |
412 |
|
413 |
Debug.Write("Adding Separator 1", LogCategories.ShellMenu); |
414 |
// Add a separator. |
415 |
/* MENUITEMINFO sep = new MENUITEMINFO(); |
416 |
sep.cbSize = (uint)Marshal.SizeOf(sep); |
417 |
sep.fMask = MIIM.MIIM_TYPE; |
418 |
sep.fType = MFT.MFT_SEPARATOR;*/ |
419 |
if (!NativeMethods.InsertMenu(hMenu, (uint)_items.Values.Count + idCmdFirst+1,MF.MF_SEPARATOR|MF.MF_BYPOSITION, 0, String.Empty)) |
420 |
{ |
421 |
Log.ErrorFormat("Error adding separator 1\r\n{0}", Marshal.GetLastWin32Error()); |
422 |
return Marshal.GetHRForLastWin32Error(); |
423 |
} |
424 |
|
425 |
|
426 |
|
427 |
|
428 |
Debug.WriteLine("Menus added", LogCategories.ShellOverlays); |
429 |
// Return an HRESULT value with the severity set to SEVERITY_SUCCESS. |
430 |
// Set the code value to the offset of the largest command identifier |
431 |
// that was assigned, plus one (1). |
432 |
return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, |
433 |
largestID + 1); |
434 |
} |
435 |
|
436 |
/// <summary> |
437 |
/// Carry out the command associated with a shortcut menu item. |
438 |
/// </summary> |
439 |
/// <param name="pici"> |
440 |
/// A pointer to a CMINVOKECOMMANDINFO or CMINVOKECOMMANDINFOEX structure |
441 |
/// containing information about the command. |
442 |
/// </param> |
443 |
public void InvokeCommand(IntPtr pici) |
444 |
{ |
445 |
bool isUnicode = false; |
446 |
|
447 |
// Determine which structure is being passed in, CMINVOKECOMMANDINFO or |
448 |
// CMINVOKECOMMANDINFOEX based on the cbSize member of lpcmi. Although |
449 |
// the lpcmi parameter is declared in Shlobj.h as a CMINVOKECOMMANDINFO |
450 |
// structure, in practice it often points to a CMINVOKECOMMANDINFOEX |
451 |
// structure. This struct is an extended version of CMINVOKECOMMANDINFO |
452 |
// and has additional members that allow Unicode strings to be passed. |
453 |
CMINVOKECOMMANDINFO ici = (CMINVOKECOMMANDINFO)Marshal.PtrToStructure( |
454 |
pici, typeof(CMINVOKECOMMANDINFO)); |
455 |
CMINVOKECOMMANDINFOEX iciex = new CMINVOKECOMMANDINFOEX(); |
456 |
if (ici.cbSize == Marshal.SizeOf(typeof(CMINVOKECOMMANDINFOEX))) |
457 |
{ |
458 |
if ((ici.fMask & CMIC.CMIC_MASK_UNICODE) != 0) |
459 |
{ |
460 |
isUnicode = true; |
461 |
iciex = (CMINVOKECOMMANDINFOEX)Marshal.PtrToStructure(pici, |
462 |
typeof(CMINVOKECOMMANDINFOEX)); |
463 |
} |
464 |
} |
465 |
|
466 |
// Determines whether the command is identified by its offset or verb. |
467 |
// There are two ways to identify commands: |
468 |
// |
469 |
// 1) The command's verb string |
470 |
// 2) The command's identifier offset |
471 |
// |
472 |
// If the high-order word of lpcmi->lpVerb (for the ANSI case) or |
473 |
// lpcmi->lpVerbW (for the Unicode case) is nonzero, lpVerb or lpVerbW |
474 |
// holds a verb string. If the high-order word is zero, the command |
475 |
// offset is in the low-order word of lpcmi->lpVerb. |
476 |
|
477 |
// For the ANSI case, if the high-order word is not zero, the command's |
478 |
// verb string is in lpcmi->lpVerb. |
479 |
if (!isUnicode && NativeMethods.HighWord(ici.verb.ToInt32()) != 0) |
480 |
{ |
481 |
// Is the verb supported by this context menu extension? |
482 |
string verbAnsi = Marshal.PtrToStringAnsi(ici.verb); |
483 |
if (_items.ContainsKey(verbAnsi)) |
484 |
{ |
485 |
_items[verbAnsi].MenuCommand(ici.hwnd); |
486 |
} |
487 |
else |
488 |
{ |
489 |
// If the verb is not recognized by the context menu handler, it |
490 |
// must return E_FAIL to allow it to be passed on to the other |
491 |
// context menu handlers that might implement that verb. |
492 |
Marshal.ThrowExceptionForHR(WinError.E_FAIL); |
493 |
} |
494 |
} |
495 |
|
496 |
// For the Unicode case, if the high-order word is not zero, the |
497 |
// command's verb string is in lpcmi->lpVerbW. |
498 |
else if (isUnicode && NativeMethods.HighWord(iciex.verbW.ToInt32()) != 0) |
499 |
{ |
500 |
// Is the verb supported by this context menu extension? |
501 |
string verbUTF = Marshal.PtrToStringUni(iciex.verbW); |
502 |
if (_items.ContainsKey(verbUTF)) |
503 |
{ |
504 |
_items[verbUTF].MenuCommand(ici.hwnd); |
505 |
} |
506 |
else |
507 |
{ |
508 |
// If the verb is not recognized by the context menu handler, it |
509 |
// must return E_FAIL to allow it to be passed on to the other |
510 |
// context menu handlers that might implement that verb. |
511 |
Marshal.ThrowExceptionForHR(WinError.E_FAIL); |
512 |
} |
513 |
} |
514 |
|
515 |
// If the command cannot be identified through the verb string, then |
516 |
// check the identifier offset. |
517 |
else |
518 |
{ |
519 |
// Is the command identifier offset supported by this context menu |
520 |
// extension? |
521 |
int menuID = NativeMethods.LowWord(ici.verb.ToInt32()); |
522 |
var menuItem = _items.FirstOrDefault(item => item.Value.MenuDisplayId == menuID).Value; |
523 |
if (menuItem != null) |
524 |
{ |
525 |
menuItem.MenuCommand(ici.hwnd); |
526 |
} |
527 |
else |
528 |
{ |
529 |
// If the verb is not recognized by the context menu handler, it |
530 |
// must return E_FAIL to allow it to be passed on to the other |
531 |
// context menu handlers that might implement that verb. |
532 |
Marshal.ThrowExceptionForHR(WinError.E_FAIL); |
533 |
} |
534 |
} |
535 |
} |
536 |
|
537 |
/// <summary> |
538 |
/// Get information about a shortcut menu command, including the help string |
539 |
/// and the language-independent, or canonical, name for the command. |
540 |
/// </summary> |
541 |
/// <param name="idCmd">Menu command identifier offset.</param> |
542 |
/// <param name="uFlags"> |
543 |
/// Flags specifying the information to return. This parameter can have one |
544 |
/// of the following values: GCS_HELPTEXTA, GCS_HELPTEXTW, GCS_VALIDATEA, |
545 |
/// GCS_VALIDATEW, GCS_VERBA, GCS_VERBW. |
546 |
/// </param> |
547 |
/// <param name="pReserved">Reserved. Must be IntPtr.Zero</param> |
548 |
/// <param name="pszName"> |
549 |
/// The address of the buffer to receive the null-terminated string being |
550 |
/// retrieved. |
551 |
/// </param> |
552 |
/// <param name="cchMax"> |
553 |
/// Size of the buffer, in characters, to receive the null-terminated string. |
554 |
/// </param> |
555 |
public void GetCommandString( |
556 |
UIntPtr idCmd, |
557 |
uint uFlags, |
558 |
IntPtr pReserved, |
559 |
StringBuilder pszName, |
560 |
uint cchMax) |
561 |
{ |
562 |
uint menuID = idCmd.ToUInt32(); |
563 |
var menuItem = _items.FirstOrDefault(item => item.Value.MenuDisplayId == menuID).Value; |
564 |
if (menuItem != null) |
565 |
{ |
566 |
switch ((GCS)uFlags) |
567 |
{ |
568 |
case GCS.GCS_VERBW: |
569 |
if (menuItem.VerbCanonicalName.Length > cchMax - 1) |
570 |
{ |
571 |
Marshal.ThrowExceptionForHR(WinError.STRSAFE_E_INSUFFICIENT_BUFFER); |
572 |
} |
573 |
else |
574 |
{ |
575 |
pszName.Clear(); |
576 |
pszName.Append(menuItem.VerbCanonicalName); |
577 |
} |
578 |
break; |
579 |
|
580 |
case GCS.GCS_HELPTEXTW: |
581 |
if (menuItem.VerbHelpText.Length > cchMax - 1) |
582 |
{ |
583 |
Marshal.ThrowExceptionForHR(WinError.STRSAFE_E_INSUFFICIENT_BUFFER); |
584 |
} |
585 |
else |
586 |
{ |
587 |
pszName.Clear(); |
588 |
pszName.Append(menuItem.VerbHelpText); |
589 |
} |
590 |
break; |
591 |
} |
592 |
} |
593 |
} |
594 |
|
595 |
#endregion |
596 |
} |
597 |
} |