// This Source Code is partially based on reverse engineering of the Windows Operating System, // and is intended for use on Windows systems only. // This Source Code is partially based on the source code provided by the .NET Foundation. // // NOTE: // I split unmanaged code stuff into the NativeMethods library. // If you have suggestions for the code below, please submit your changes there. // https://github.com/lepoco/nativemethods // ReSharper disable IdentifierTypo // ReSharper disable InconsistentNaming #pragma warning disable SA1307 // Accessible fields should begin with upper-case letter #pragma warning disable SA1401 // Fields should be private #pragma warning disable CA1060 // Move pinvokes to native methods class using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; namespace WPFluent.Interop; /// /// The Windows UI provides users with access to a wide variety of objects necessary to run applications and manage the /// operating system. /// internal static class Shell32 { /// /// Retrieves the User Model AppID that has been explicitly set for the current process via /// SetCurrentProcessExplicitAppUserModelID /// /// The returned string ID of the Application User Model. /// An HRESULT indicating success or failure of the operation. [DllImport(Libraries.Shell32)] public static extern int GetCurrentProcessExplicitAppUserModelID( [Out, MarshalAs(UnmanagedType.LPWStr)] out string AppID); /// /// Sets the User Model AppID for the current process, enabling Windows to retrieve this ID /// /// The string ID [DllImport(Libraries.Shell32, PreserveSig = false)] public static extern void SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID); [DllImport(Libraries.Shell32)] public static extern int SHCreateItemFromParsingName( [MarshalAs(UnmanagedType.LPWStr)] string pszPath, IBindCtx pbc, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppv); [DllImport(Libraries.Shell32)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool Shell_NotifyIcon([In] NIM dwMessage, [In] NOTIFYICONDATA lpdata); [DllImport(Libraries.Shell32, PreserveSig = false)] public static extern void SHGetItemFromDataObject( System.Runtime.InteropServices.ComTypes.IDataObject pdtobj, DOGIF dwFlags, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppv); /// /// DATAOBJ_GET_ITEM_FLAGS. DOGIF_*. /// public enum DOGIF { DEFAULT = 0x0000, TRAVERSE_LINK = 0x0001, // if the item is a link get the target NO_HDROP = 0x0002, // don't fallback and use CF_HDROP clipboard format NO_URL = 0x0004, // don't fallback and use URL clipboard format ONLY_IF_ONE = 0x0008, // only return the item if there is one item in the array } /// /// Shell_NotifyIcon messages. NIM_* /// public enum NIM : uint { ADD = 0, MODIFY = 1, DELETE = 2, SETFOCUS = 3, SETVERSION = 4, } /// /// Shell_NotifyIcon flags. NIF_* /// [Flags] public enum NIF : uint { MESSAGE = 0x0001, ICON = 0x0002, TIP = 0x0004, STATE = 0x0008, INFO = 0x0010, GUID = 0x0020, /// /// Vista only. /// REALTIME = 0x0040, /// /// Vista only. /// SHOWTIP = 0x0080, XP_MASK = MESSAGE | ICON | STATE | INFO | GUID, VISTA_MASK = XP_MASK | REALTIME | SHOWTIP, } [StructLayout(LayoutKind.Sequential)] public class NOTIFYICONDATA { /// /// The size of this structure, in bytes. /// public int cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA)); /// /// A handle to the window that receives notifications associated with an icon in the notification area. /// public IntPtr hWnd; /// /// The application-defined identifier of the taskbar icon. The Shell uses either (hWnd plus uID) or guidItem to /// identify which icon to operate on when Shell_NotifyIcon is invoked. You can have multiple icons associated /// with a single hWnd by assigning each a different uID. If guidItem is specified, uID is ignored. /// public int uID; /// /// Flags that either indicate which of the other members of the structure contain valid data or provide /// additional information to the tooltip as to how it should display. /// public NIF uFlags; /// /// 0x00000001. The uCallbackMessage member is valid. /// public int uCallbackMessage; /// /// 0x00000002. The hIcon member is valid. /// public IntPtr hIcon; /// /// 0x00000004. The szTip member is valid. /// [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)] // 128 public string? szTip; /// /// The state of the icon. There are two flags that can be set independently. NIS_HIDDEN = 1. The icon is /// hidden. NIS_SHAREDICON = 2. The icon is shared. /// public uint dwState; public uint dwStateMask; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)] // 256 public string? szInfo; /// /// Prior to Vista this was a union of uTimeout and uVersion. As of Vista, uTimeout has been deprecated. /// public uint uVersion; // Used with Shell_NotifyIcon flag NIM_SETVERSION. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)] // 64 public string? szInfoTitle; public uint dwInfoFlags; public Guid guidItem; // Vista only public IntPtr hBalloonIcon; } } #pragma warning restore SA1307 // Accessible fields should begin with upper-case letter #pragma warning restore SA1401 // Fields should be private #pragma warning restore CA1060 // Move pinvokes to native methods class