Simplified shell context menu creation

This commit is contained in:
Peter Kirmeier 2023-05-09 22:06:15 +02:00
parent bb198a32db
commit c5225b5708
5 changed files with 47 additions and 98 deletions

View file

@ -142,23 +142,6 @@ namespace SystemTrayMenu.DataClasses
}
}
internal void OpenShellContextMenu(Point position)
{
ShellContextMenu ctxMnu = new();
if (IsPointingToFolder)
{
DirectoryInfo[] dir = new DirectoryInfo[1];
dir[0] = new DirectoryInfo(Path);
ctxMnu.ShowContextMenu(dir, position);
}
else
{
FileInfo[] arrFI = new FileInfo[1];
arrFI[0] = FileInfo;
ctxMnu.ShowContextMenu(arrFI, position);
}
}
internal void OpenItem(out bool doCloseAfterOpen, int clickCount = -1)
{
doCloseAfterOpen = false;

View file

@ -1238,7 +1238,7 @@ namespace SystemTrayMenu.UserInterface
{
var position = Mouse.GetPosition(this);
position.Offset(Left, Top);
itemData.data.OpenShellContextMenu(position);
itemData.OpenShellContextMenu(position);
}
}
@ -1343,6 +1343,18 @@ namespace SystemTrayMenu.UserInterface
data.OpenItem(out doCloseAfterOpen, clickCount);
}
internal void OpenShellContextMenu(Point position)
{
if (data.IsPointingToFolder)
{
ShellContextMenu.OpenShellContextMenu(new DirectoryInfo(data.Path), position);
}
else
{
ShellContextMenu.OpenShellContextMenu(data.FileInfo, position);
}
}
internal void UpdateColors()
{
if (IsClicking)

View file

@ -2,7 +2,7 @@
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Utilities
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Diagnostics.CodeAnalysis;
@ -11,9 +11,10 @@ namespace SystemTrayMenu.Utilities
using System.Text;
using System.Windows;
using System.Windows.Input;
using SystemTrayMenu.Helpers;
using static SystemTrayMenu.Utilities.FormsExtensions;
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1124:Do not use regions", Justification = "Mark SystemTrayMenu modifications made to original source.")]
/// <summary>
/// source: https://www.codeproject.com/Articles/22012/Explorer-Shell-Context-Menu
/// modified to fit SystemTrayMenu.
@ -697,6 +698,36 @@ namespace SystemTrayMenu.Utilities
IntPtr plResult);
}
#region System Tray Menu Helpers
/// <summary>
/// Creates and shows a shell context menu.
/// </summary>
/// <param name="directoryInfo">Context of given directory.</param>
/// <param name="position">Position where the menu show be shown.</param>
public static void OpenShellContextMenu(DirectoryInfo directoryInfo, Point position)
{
ShellContextMenu ctxMnu = new();
DirectoryInfo[] dir = new DirectoryInfo[1];
dir[0] = directoryInfo;
ctxMnu.ShowContextMenu(dir, position);
}
/// <summary>
/// Creates and shows a shell context menu.
/// </summary>
/// <param name="fileInfo">Context of given file.</param>
/// <param name="position">Position where the menu show be shown.</param>
public static void OpenShellContextMenu(FileInfo fileInfo, Point position)
{
ShellContextMenu ctxMnu = new();
FileInfo[] arrFI = new FileInfo[1];
arrFI[0] = fileInfo;
ctxMnu.ShowContextMenu(arrFI, position);
}
#endregion
/// <summary>
/// Shows the context menu.
/// </summary>
@ -1063,7 +1094,7 @@ namespace SystemTrayMenu.Utilities
int nResult = DllImports.NativeMethods.Shell32SHGetDesktopFolder(out IntPtr pUnkownDesktopFolder);
if (nResult != ResultOK)
{
throw new ShellContextMenuException("Failed to get the desktop shell folder");
throw new COMException("Failed to get the desktop shell folder", nResult);
}
oDesktopFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnkownDesktopFolder, typeof(IShellFolder));

View file

@ -1,37 +0,0 @@
// <copyright file="ShellContextMenuException.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Helpers
{
using System;
using System.Runtime.Serialization;
[Serializable]
public class ShellContextMenuException : Exception
{
public ShellContextMenuException()
{
// Add any type-specific logic, and supply the default message.
}
public ShellContextMenuException(string message)
: base(message)
{
// Add any type-specific logic.
}
public ShellContextMenuException(string message, Exception innerException)
: base(message, innerException)
{
// Add any type-specific logic for inner exceptions.
}
protected ShellContextMenuException(
SerializationInfo info, StreamingContext context)
: base(info, context)
{
// Implement type-specific serialization constructor logic.
}
}
}

View file

@ -1,40 +0,0 @@
// <copyright file="ShellHelper.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Helpers
{
using System;
internal static class ShellHelper
{
/// <summary>
/// Retrieves the High Word of a WParam of a WindowMessage.
/// </summary>
/// <param name="ptr">The pointer to the WParam.</param>
/// <returns>The unsigned integer for the High Word.</returns>
public static uint HiWord(IntPtr ptr)
{
uint param32 = (uint)(ptr.ToInt64() | 0xffffffffL);
if ((param32 & 0x80000000) == 0x80000000)
{
return param32 >> 16;
}
else
{
return (param32 >> 16) & 0xffff;
}
}
/// <summary>
/// Retrieves the Low Word of a WParam of a WindowMessage.
/// </summary>
/// <param name="ptr">The pointer to the WParam.</param>
/// <returns>The unsigned integer for the Low Word.</returns>
public static uint LoWord(IntPtr ptr)
{
uint param32 = (uint)(ptr.ToInt64() | 0xffffffffL);
return param32 & 0xffff;
}
}
}