From c5225b57081a7649bb4596bcf9447d11325fc53b Mon Sep 17 00:00:00 2001 From: Peter Kirmeier Date: Tue, 9 May 2023 22:06:15 +0200 Subject: [PATCH] Simplified shell context menu creation --- DataClasses/RowData.cs | 17 -------- UserInterface/Menu.xaml.cs | 14 ++++++- .../ShellContextMenu/ShellContextMenu.cs | 37 +++++++++++++++-- .../ShellContextMenuException.cs | 37 ----------------- UserInterface/ShellContextMenu/ShellHelper.cs | 40 ------------------- 5 files changed, 47 insertions(+), 98 deletions(-) delete mode 100644 UserInterface/ShellContextMenu/ShellContextMenuException.cs delete mode 100644 UserInterface/ShellContextMenu/ShellHelper.cs diff --git a/DataClasses/RowData.cs b/DataClasses/RowData.cs index bd2717e..ad5e81e 100644 --- a/DataClasses/RowData.cs +++ b/DataClasses/RowData.cs @@ -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; diff --git a/UserInterface/Menu.xaml.cs b/UserInterface/Menu.xaml.cs index ac71735..0f1b8d3 100644 --- a/UserInterface/Menu.xaml.cs +++ b/UserInterface/Menu.xaml.cs @@ -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) diff --git a/UserInterface/ShellContextMenu/ShellContextMenu.cs b/UserInterface/ShellContextMenu/ShellContextMenu.cs index 8b78a59..097cf47 100644 --- a/UserInterface/ShellContextMenu/ShellContextMenu.cs +++ b/UserInterface/ShellContextMenu/ShellContextMenu.cs @@ -2,7 +2,7 @@ // Copyright (c) PlaceholderCompany. All rights reserved. // -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.")] + /// /// 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 + + /// + /// Creates and shows a shell context menu. + /// + /// Context of given directory. + /// Position where the menu show be shown. + public static void OpenShellContextMenu(DirectoryInfo directoryInfo, Point position) + { + ShellContextMenu ctxMnu = new(); + DirectoryInfo[] dir = new DirectoryInfo[1]; + dir[0] = directoryInfo; + ctxMnu.ShowContextMenu(dir, position); + } + + /// + /// Creates and shows a shell context menu. + /// + /// Context of given file. + /// Position where the menu show be shown. + public static void OpenShellContextMenu(FileInfo fileInfo, Point position) + { + ShellContextMenu ctxMnu = new(); + FileInfo[] arrFI = new FileInfo[1]; + arrFI[0] = fileInfo; + ctxMnu.ShowContextMenu(arrFI, position); + } + + #endregion + /// /// Shows the context menu. /// @@ -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)); diff --git a/UserInterface/ShellContextMenu/ShellContextMenuException.cs b/UserInterface/ShellContextMenu/ShellContextMenuException.cs deleted file mode 100644 index a942bb5..0000000 --- a/UserInterface/ShellContextMenu/ShellContextMenuException.cs +++ /dev/null @@ -1,37 +0,0 @@ -// -// Copyright (c) PlaceholderCompany. All rights reserved. -// - -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. - } - } -} diff --git a/UserInterface/ShellContextMenu/ShellHelper.cs b/UserInterface/ShellContextMenu/ShellHelper.cs deleted file mode 100644 index 3e9ed76..0000000 --- a/UserInterface/ShellContextMenu/ShellHelper.cs +++ /dev/null @@ -1,40 +0,0 @@ -// -// Copyright (c) PlaceholderCompany. All rights reserved. -// - -namespace SystemTrayMenu.Helpers -{ - using System; - - internal static class ShellHelper - { - /// - /// Retrieves the High Word of a WParam of a WindowMessage. - /// - /// The pointer to the WParam. - /// The unsigned integer for the High Word. - public static uint HiWord(IntPtr ptr) - { - uint param32 = (uint)(ptr.ToInt64() | 0xffffffffL); - if ((param32 & 0x80000000) == 0x80000000) - { - return param32 >> 16; - } - else - { - return (param32 >> 16) & 0xffff; - } - } - - /// - /// Retrieves the Low Word of a WParam of a WindowMessage. - /// - /// The pointer to the WParam. - /// The unsigned integer for the Low Word. - public static uint LoWord(IntPtr ptr) - { - uint param32 = (uint)(ptr.ToInt64() | 0xffffffffL); - return param32 & 0xffff; - } - } -}