Refactor native DLL imports

Adjust namespace
Enforce calling conventions
Mark as Windows specific API
Remove empty wrapper calls
Remove obsolete calls
Combine/group logically in files instead of "by function"
This commit is contained in:
Peter Kirmeier 2023-05-09 23:33:56 +02:00
parent c5225b5708
commit ea02181408
28 changed files with 255 additions and 503 deletions

View file

@ -204,7 +204,7 @@ namespace SystemTrayMenu.Helpers
menuData.RowDatas.Add(new RowData(true, false, menuData.Level, directory));
}
foreach (string file in DirectoryBySearchPattern.GetFiles(path, Config.SearchPattern))
foreach (string file in GetFilesBySearchPattern(path, Config.SearchPattern))
{
if (worker?.CancellationPending == true)
{
@ -223,7 +223,7 @@ namespace SystemTrayMenu.Helpers
{
try
{
foreach (string file in DirectoryBySearchPattern.GetFiles(path, Config.SearchPattern))
foreach (string file in GetFilesBySearchPattern(path, Config.SearchPattern))
{
menuData.RowDatas.Add(new RowData(false, true, menuData.Level, file));
}
@ -246,5 +246,17 @@ namespace SystemTrayMenu.Helpers
Log.Warn($"GetDirectoriesAndFilesRecursive path:'{path}'", ex);
}
}
private static List<string> GetFilesBySearchPattern(string path, string searchPatternCombined)
{
string[] searchPatterns = searchPatternCombined.Split('|');
List<string> files = new();
foreach (string searchPattern in searchPatterns)
{
files.AddRange(Directory.GetFiles(path, searchPattern));
}
return files;
}
}
}
}

View file

@ -1,25 +0,0 @@
// <copyright file="CreatePopupMenu.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static IntPtr User32CreatePopupMenu()
{
return CreatePopupMenu();
}
// The CreatePopupMenu function creates a drop-down menu, submenu, or shortcut menu. The menu is initially empty. You can insert or append menu items by using the InsertMenuItem function. You can also use the InsertMenu function to insert menu items and the AppendMenu function to append menu items.
[DllImport("user32", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr CreatePopupMenu();
}
}

View file

@ -1,19 +0,0 @@
// <copyright file="DeleteObject.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
[DllImport("gdi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int DeleteObject(IntPtr hIcon);
}
}

View file

@ -1,24 +0,0 @@
// <copyright file="DestroyIcon.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static void User32DestroyIcon(IntPtr hIcon)
{
_ = DestroyIcon(hIcon);
}
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int DestroyIcon(IntPtr hIcon);
}
}

View file

@ -1,25 +0,0 @@
// <copyright file="DestroyMenu.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static bool User32DestroyMenu(IntPtr hMenu)
{
return DestroyMenu(hMenu);
}
// The DestroyMenu function destroys the specified menu and frees any memory that the menu occupies.
[DllImport("user32", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern bool DestroyMenu(IntPtr hMenu);
}
}

View file

@ -1,24 +0,0 @@
// <copyright file="FindExecuteable.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static void Shell32FindExecutable(string lpFile, string lpDirectory, [Out] StringBuilder lpResult)
{
_ = FindExecutable(lpFile, lpDirectory, lpResult);
}
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int FindExecutable(string lpFile, string lpDirectory, [Out] StringBuilder lpResult);
}
}

View file

@ -1,29 +0,0 @@
// <copyright file="FindWindow.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static IntPtr User32FindWindow(string? lpClassName, string? lpWindowName)
{
return FindWindow(lpClassName, lpWindowName);
}
/// <summary>
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindoww .
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr FindWindow(string? lpClassName, string? lpWindowName);
}
}

39
NativeDllImport/HotKey.cs Normal file
View file

@ -0,0 +1,39 @@
// <copyright file="HotKey.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool User32RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode);
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern bool User32UnregisterHotKey(IntPtr hWnd, int id);
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "MapVirtualKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern uint User32MapVirtualKey(uint uCode, uint uMapType);
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "GetKeyNameText", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize);
}
}

View file

@ -1,12 +1,13 @@
// <copyright file="GetIcon.cs" company="PlaceholderCompany">
// <copyright file="Icon.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
@ -29,11 +30,17 @@ namespace SystemTrayMenu.DllImports
/// <param name="i">i.</param>
/// <param name="flags">flags.</param>
/// <returns>IntPtr.</returns>
[DllImport("comctl32", SetLastError = true, CharSet = CharSet.Unicode)]
[SupportedOSPlatform("windows")]
[DllImport("comctl32", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern IntPtr ImageList_GetIcon(
IntPtr himl,
int i,
int flags);
int flags);
[SupportedOSPlatform("windows")]
[DllImport("User32.dll", EntryPoint = "DestroyIcon", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern int User32DestroyIcon(IntPtr hIcon);
}
}

View file

@ -1,26 +0,0 @@
// <copyright file="IsTouchEnabled.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static bool IsTouchEnabled()
{
const int MAXTOUCHES_INDEX = 95;
int maxTouches = GetSystemMetrics(MAXTOUCHES_INDEX);
return maxTouches > 0;
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int GetSystemMetrics(int nIndex);
}
}

69
NativeDllImport/Menu.cs Normal file
View file

@ -0,0 +1,69 @@
// <copyright file="Menu.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
/// <summary>
/// Specifies how TrackPopupMenuEx positions the shortcut menu horizontally.
/// </summary>
[Flags]
internal enum TPM : uint
{
LEFTBUTTON = 0x0000, // LEFTALIGN = 0x0000, // TOPALIGN = 0x0000, // HORIZONTAL = 0x0000,
RIGHTBUTTON = 0x0002,
CENTERALIGN = 0x0004,
RIGHTALIGN = 0x0008,
VCENTERALIGN = 0x0010,
BOTTOMALIGN = 0x0020,
VERTICAL = 0x0040,
NONOTIFY = 0x0080,
RETURNCMD = 0x0100,
RECURSE = 0x0001,
HORPOSANIMATION = 0x0400,
HORNEGANIMATION = 0x0800,
VERPOSANIMATION = 0x1000,
VERNEGANIMATION = 0x2000,
NOANIMATION = 0x4000,
LAYOUTRTL = 0x8000,
}
// The CreatePopupMenu function creates a drop-down menu, submenu, or shortcut menu. The menu is initially empty. You can insert or append menu items by using the InsertMenuItem function. You can also use the InsertMenu function to insert menu items and the AppendMenu function to append menu items.
[SupportedOSPlatform("windows")]
[DllImport("user32", EntryPoint = "CreatePopupMenu", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern IntPtr User32CreatePopupMenu();
// The DestroyMenu function destroys the specified menu and frees any memory that the menu occupies.
[SupportedOSPlatform("windows")]
[DllImport("user32", EntryPoint = "DestroyMenu", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern bool User32DestroyMenu(IntPtr hMenu);
/// <summary>
/// user32 TrackPopupMenuEx.
/// The TrackPopupMenuEx function displays a shortcut menu at the specified location and
/// tracks the selection of items on the shortcut menu. The shortcut menu can appear anywhere on the screen.
/// </summary>
/// <param name="hmenu">hmenu.</param>
/// <param name="flags">flags.</param>
/// <param name="x">x.</param>
/// <param name="y">y.</param>
/// <param name="hwnd">hwnd.</param>
/// <param name="lptpm">lptpm.</param>
/// <returns>uint.</returns>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "TrackPopupMenuEx", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern uint User32TrackPopupMenuEx(IntPtr hmenu, TPM flags, int x, int y, IntPtr hwnd, IntPtr lptpm);
}
}

View file

@ -1,54 +0,0 @@
// <copyright file="RegisterHotKey.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static bool User32RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk)
{
return RegisterHotKey(hWnd, id, fsModifiers, vk);
}
public static bool User32UnregisterHotKey(IntPtr hWnd, int id)
{
return UnregisterHotKey(hWnd, id);
}
public static uint User32MapVirtualKey(uint uCode, uint uMapType)
{
return MapVirtualKey(uCode, uMapType);
}
public static int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize)
{
return GetKeyNameText(lParam, lpString, nSize);
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize);
}
}

View file

@ -1,25 +0,0 @@
// <copyright file="SHGetDesktopFolder.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static int Shell32SHGetDesktopFolder(out IntPtr ppshf)
{
return SHGetDesktopFolder(out ppshf);
}
// Retrieves the IShellFolder interface for the desktop folder, which is the root of the Shell's namespace.
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int SHGetDesktopFolder(out IntPtr ppshf);
}
}

View file

@ -1,55 +0,0 @@
// <copyright file="SHGetFileInfo.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
private const int maxPath = 256;
internal static IntPtr Shell32SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags)
{
return SHGetFileInfo(
pszPath,
dwFileAttributes,
ref psfi,
cbFileInfo,
uFlags);
}
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct SHFILEINFO
{
public const int NAMESIZE = 80;
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = maxPath)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)]
public string szTypeName;
}
}
}

View file

@ -1,25 +0,0 @@
// <copyright file="SHGetFolderPath.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static int ShfolderSHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath)
{
return SHGetFolderPath(hwndOwner, nFolder, hToken, dwFlags, lpszPath);
}
[DllImport("shfolder.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
}
}

View file

@ -2,7 +2,7 @@
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
//
// Copyright (c) 2022-2022 Peter Kirmeier
// Copyright (c) 2022-2023 Peter Kirmeier
namespace SystemTrayMenu.DllImports
{
@ -17,6 +17,19 @@ namespace SystemTrayMenu.DllImports
/// </summary>
public static partial class NativeMethods
{
public static bool IsTouchEnabled()
{
const int MAXTOUCHES_INDEX = 95;
int maxTouches = GetSystemMetrics(MAXTOUCHES_INDEX);
return maxTouches > 0;
}
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int GetSystemMetrics(int nIndex);
public static class Screen
{
private static Point LastCursorPosition = new Point(0, 0);
@ -112,14 +125,16 @@ namespace SystemTrayMenu.DllImports
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaymonitors .
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
[DllImport("user32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
/// <summary>
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos .
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
public static extern bool GetCursorPos(out POINT lpPoint);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

View file

@ -1,14 +1,15 @@
// <copyright file="NativeMethods.cs" company="PlaceholderCompany">
// <copyright file="ShellInterfacesCOM.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
internal static class NativeMethods
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
public static partial class NativeMethods
{
public const uint FOS_PICKFOLDERS = 0x00000020;
public const uint FOS_FORCEFILESYSTEM = 0x00000040;
@ -20,7 +21,8 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
public const uint SIGDN_FILESYSPATH = 0x80058000;
[ComImport]
[ComImport]
[SupportedOSPlatform("windows")]
[Guid("42F85136-DB7E-439C-85F1-E4075D135FC8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IFileDialog
@ -102,7 +104,8 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
uint SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
}
[ComImport]
[ComImport]
[SupportedOSPlatform("windows")]
[Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IShellItem
@ -125,9 +128,10 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
uint Compare([In, MarshalAs(UnmanagedType.Interface)] IShellItem psi, [In] uint hint, out int piOrder);
}
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
}
[SupportedOSPlatform("windows")]
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern int SHCreateItemFromParsingName(
[MarshalAs(UnmanagedType.LPWStr)] string pszPath,
@ -135,7 +139,8 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
ref Guid riid,
[MarshalAs(UnmanagedType.Interface)] out IShellItem ppv);
[ComImport]
[ComImport]
[SupportedOSPlatform("windows")]
[ClassInterface(ClassInterfaceType.None)]
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
[Guid("DC1C5A9C-E88A-4DDE-A5A1-60F82A20AEF7")]

View file

@ -0,0 +1,59 @@
// <copyright file="ShellWindowsApi.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
[SupportedOSPlatform("windows")]
[DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern IntPtr Shell32SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags);
// Retrieves the IShellFolder interface for the desktop folder, which is the root of the Shell's namespace.
[SupportedOSPlatform("windows")]
[DllImport("shell32.dll", EntryPoint = "SHGetDesktopFolder", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern int Shell32SHGetDesktopFolder(out IntPtr ppshf);
[SupportedOSPlatform("windows")]
[DllImport("shlwapi.dll", EntryPoint = "StrCmpLogicalW", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern int ShlwapiStrCmpLogicalW(string x, string y);
// Takes a STRRET structure returned by IShellFolder::GetDisplayNameOf, converts it to a string, and places the result in a buffer.
[SupportedOSPlatform("windows")]
[DllImport("shlwapi.dll", EntryPoint = "StrRetToBuf", ExactSpelling = false, SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern int ShlwapiStrRetToBuf(IntPtr pstr, IntPtr pidl, StringBuilder pszBuf, int cchBuf);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)]
public string szTypeName;
private const int NAMESIZE = 80;
private const int MAX_PATH = 256;
}
}
}

View file

@ -1,25 +0,0 @@
// <copyright file="StrCmpLogicalW.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static int ShlwapiStrCmpLogicalW(string x, string y)
{
return StrCmpLogicalW(x, y);
}
[SupportedOSPlatform("windows")]
[DllImport("shlwapi.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int StrCmpLogicalW(string x, string y);
}
}

View file

@ -1,26 +0,0 @@
// <copyright file="StrRetToBuf.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
public static int ShlwapiStrRetToBuf(IntPtr pstr, IntPtr pidl, StringBuilder pszBuf, int cchBuf)
{
return StrRetToBuf(pstr, pidl, pszBuf, cchBuf);
}
// Takes a STRRET structure returned by IShellFolder::GetDisplayNameOf, converts it to a string, and places the result in a buffer.
[DllImport("shlwapi.dll", EntryPoint = "StrRetToBuf", ExactSpelling = false, SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern int StrRetToBuf(IntPtr pstr, IntPtr pidl, StringBuilder pszBuf, int cchBuf);
}
}

View file

@ -1,12 +1,13 @@
// <copyright file="SHAppBarMessage.cs" company="PlaceholderCompany">
// <copyright file="TaskBar.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
@ -34,15 +35,11 @@ namespace SystemTrayMenu.DllImports
Right = 2,
Bottom = 3,
}
internal static IntPtr Shell32SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData)
{
return SHAppBarMessage(dwMessage, ref pData);
}
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[SupportedOSPlatform("windows")]
[DllImport("shell32.dll", EntryPoint = "SHAppBarMessage", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData);
internal static extern IntPtr Shell32SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct APPBARDATA

View file

@ -1,59 +0,0 @@
// <copyright file="TrackPopupMenuEx.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.DllImports
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// wraps the methodcalls to native windows dll's.
/// </summary>
public static partial class NativeMethods
{
/// <summary>
/// Specifies how TrackPopupMenuEx positions the shortcut menu horizontally.
/// </summary>
[Flags]
internal enum TPM : uint
{
LEFTBUTTON = 0x0000, // LEFTALIGN = 0x0000, // TOPALIGN = 0x0000, // HORIZONTAL = 0x0000,
RIGHTBUTTON = 0x0002,
CENTERALIGN = 0x0004,
RIGHTALIGN = 0x0008,
VCENTERALIGN = 0x0010,
BOTTOMALIGN = 0x0020,
VERTICAL = 0x0040,
NONOTIFY = 0x0080,
RETURNCMD = 0x0100,
RECURSE = 0x0001,
HORPOSANIMATION = 0x0400,
HORNEGANIMATION = 0x0800,
VERPOSANIMATION = 0x1000,
VERNEGANIMATION = 0x2000,
NOANIMATION = 0x4000,
LAYOUTRTL = 0x8000,
}
/// <summary>
/// user32 TrackPopupMenuEx.
/// </summary>
/// <param name="hmenu">hmenu.</param>
/// <param name="flags">flags.</param>
/// <param name="x">x.</param>
/// <param name="y">y.</param>
/// <param name="hwnd">hwnd.</param>
/// <param name="lptpm">lptpm.</param>
/// <returns>uint.</returns>
internal static uint User32TrackPopupMenuEx(IntPtr hmenu, TPM flags, int x, int y, IntPtr hwnd, IntPtr lptpm)
{
return TrackPopupMenuEx(hmenu, flags, x, y, hwnd, lptpm);
}
// The TrackPopupMenuEx function displays a shortcut menu at the specified location and tracks the selection of items on the shortcut menu. The shortcut menu can appear anywhere on the screen.
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern uint TrackPopupMenuEx(IntPtr hmenu, TPM flags, int x, int y, IntPtr hwnd, IntPtr lptpm);
}
}

View file

@ -1,8 +1,8 @@
// <copyright file="ShowWindow.cs" company="PlaceholderCompany">
// <copyright file="Window.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
//
// Copyright (c) 2022-2022 Peter Kirmeier
// Copyright (c) 2022-2023 Peter Kirmeier
namespace SystemTrayMenu.DllImports
{
@ -38,11 +38,23 @@ namespace SystemTrayMenu.DllImports
}
}
/// <summary>
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindoww .
/// </summary>
/// <param name="lpClassName">The class name or a class atom.</param>
/// <param name="lpWindowName">The window name.</param>
/// <returns>Handle to the window or NULL on failure.</returns>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern IntPtr User32FindWindow(string? lpClassName, string? lpWindowName);
/// <summary>
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongw .
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
/// <summary>
@ -50,6 +62,7 @@ namespace SystemTrayMenu.DllImports
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
/// <summary>
@ -57,6 +70,7 @@ namespace SystemTrayMenu.DllImports
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
/// <summary>
@ -64,6 +78,7 @@ namespace SystemTrayMenu.DllImports
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
}
}

View file

@ -2,7 +2,7 @@
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Helpers
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Diagnostics;
@ -11,7 +11,6 @@ namespace SystemTrayMenu.Helpers
using System.Windows.Threading;
using H.NotifyIcon.Core;
using SystemTrayMenu.Helpers.Updater;
using SystemTrayMenu.UserInterface;
using SystemTrayMenu.Utilities;
internal class AppContextMenu

View file

@ -7,7 +7,6 @@ namespace SystemTrayMenu.UserInterface
using System;
using System.Windows.Threading;
using H.NotifyIcon.Core;
using SystemTrayMenu.Helpers;
internal class AppNotifyIcon : IDisposable
{

View file

@ -9,6 +9,7 @@ namespace SystemTrayMenu.UserInterface.FolderBrowseDialog
using System.Runtime.Versioning;
using System.Windows;
using System.Windows.Interop;
using SystemTrayMenu.DllImports;
using SystemTrayMenu.Utilities;
public class FolderDialog : IFolderDialog, IDisposable

View file

@ -1,24 +0,0 @@
// <copyright file="DirectoryBySearchPattern.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
// see also: https://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using.
namespace SystemTrayMenu.Utilities
{
using System.Collections.Generic;
public static class DirectoryBySearchPattern
{
public static List<string> GetFiles(string path, string searchPatternCombined)
{
string[] searchPatterns = searchPatternCombined.Split('|');
List<string> files = new();
foreach (string searchPattern in searchPatterns)
{
files.AddRange(System.IO.Directory.GetFiles(path, searchPattern));
}
return files;
}
}
}