migrate Pilz.Win32 & optimize Pilz & Pilz.IO

This commit is contained in:
2024-09-01 08:54:57 +02:00
parent f370642354
commit 7a94fcf360
55 changed files with 700 additions and 1427 deletions

View File

@@ -0,0 +1,20 @@
namespace Pilz.Win32.Native;
[Flags]
public enum FileInfoFlags : int
{
/// <summary>
/// Retrieve the handle to the icon that represents the file and the index
/// of the icon within the system image list. The handle is copied to the
/// hIcon member of the structure specified by psfi, and the index is copied
/// to the iIcon member.
/// </summary>
SHGFI_ICON = 0x100,
/// <summary>
/// Indicates that the function should not attempt to access the file
/// specified by pszPath. Rather, it should act as if the file specified by
/// pszPath exists with the file attributes passed in dwFileAttributes.
/// </summary>
SHGFI_USEFILEATTRIBUTES = 0x10
}

View File

@@ -1,20 +0,0 @@
Namespace Native
<Flags>
Public Enum FileInfoFlags As Integer
''' <summary>
''' Retrieve the handle to the icon that represents the file and the index
''' of the icon within the system image list. The handle is copied to the
''' hIcon member of the structure specified by psfi, and the index is copied
''' to the iIcon member.
''' </summary>
SHGFI_ICON = &H100
''' <summary>
''' Indicates that the function should not attempt to access the file
''' specified by pszPath. Rather, it should act as if the file specified by
''' pszPath exists with the file attributes passed in dwFileAttributes.
''' </summary>
SHGFI_USEFILEATTRIBUTES = &H10
End Enum
End Namespace

View File

@@ -0,0 +1,15 @@
using System.Runtime.InteropServices;
using System.Text;
namespace Pilz.Win32.Native;
public class Kernel32
{
private const string LIB_KERNEL32 = "kernel32.dll";
[DllImport(LIB_KERNEL32)]
public static extern uint GetModuleFileName(nint hModule, StringBuilder lpFilename, int nSize);
}

View File

@@ -1,16 +0,0 @@
Imports System.Runtime.InteropServices
Imports System.Text
Namespace Native
Public Class Kernel32
Private Const LIB_KERNEL32 As String = "kernel32.dll"
<DllImport(LIB_KERNEL32)>
Public Shared Function GetModuleFileName(hModule As IntPtr, lpFilename As StringBuilder, nSize As Integer) As UInteger
End Function
End Class
End Namespace

View File

@@ -0,0 +1,23 @@
using System.Runtime.InteropServices;
namespace Pilz.Win32.Native;
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public POINT(System.Drawing.Point p)
{
x = p.X;
y = p.Y;
}
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
public int x;
public int y;
}

View File

@@ -1,21 +0,0 @@
Imports System.Runtime.InteropServices
Namespace Native
<StructLayout(LayoutKind.Sequential)>
Public Structure POINT
Public Sub New(ByVal p As System.Drawing.Point)
Me.x = p.X
Me.y = p.Y
End Sub
Public Sub New(ByVal x As Integer, ByVal y As Integer)
Me.x = x
Me.y = y
End Sub
Public x As Integer
Public y As Integer
End Structure
End Namespace

98
Pilz.Win32/Native/RECT.cs Normal file
View File

@@ -0,0 +1,98 @@
using System.Runtime.InteropServices;
namespace Pilz.Win32.Native;
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(int left_, int top_, int right_, int bottom_)
{
Left = left_;
Top = top_;
Right = right_;
Bottom = bottom_;
}
public RECT(Rectangle r)
{
Left = r.Left;
Top = r.Top;
Right = r.Right;
Bottom = r.Bottom;
}
public int Height
{
get
{
return Bottom - Top;
}
}
public int Width
{
get
{
return Right - Left;
}
}
public Size Size
{
get
{
return new Size(Width, Height);
}
}
public Point Location
{
get
{
return new Point(Left, Top);
}
}
public Rectangle ToRectangle()
{
return Rectangle.FromLTRB(Left, Top, Right, Bottom);
}
public static RECT FromRectangle(Rectangle rectangle)
{
return new RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
}
public override int GetHashCode()
{
return (int)Math.Round(Math.Pow(Math.Pow(Math.Pow(Left, Top << 13 | Top >> 0x13), Width << 0x1A | Width >> 6), Height << 7 | Height >> 0x19));
}
public static RECT FromXYWH(int x, int y, int width, int height)
{
return new RECT(x, y, x + width, y + height);
}
public static implicit operator Rectangle(RECT rect)
{
return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom);
}
public static implicit operator RECT(Rectangle rect)
{
return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
}
public override string ToString()
{
return "Left=" + Left + ", Top=" + Top + ", Right=" + Right + ", Bottom=" + Bottom;
}
}

View File

@@ -1,82 +0,0 @@
Imports System.Drawing
Imports System.Runtime.InteropServices
Namespace Native
<Serializable, StructLayout(LayoutKind.Sequential)>
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
Public Sub New(ByVal left_ As Integer, ByVal top_ As Integer, ByVal right_ As Integer, ByVal bottom_ As Integer)
Left = left_
Top = top_
Right = right_
Bottom = bottom_
End Sub
Public Sub New(ByVal r As Rectangle)
Left = r.Left
Top = r.Top
Right = r.Right
Bottom = r.Bottom
End Sub
Public ReadOnly Property Height As Integer
Get
Return Bottom - Top
End Get
End Property
Public ReadOnly Property Width As Integer
Get
Return Right - Left
End Get
End Property
Public ReadOnly Property Size As System.Drawing.Size
Get
Return New System.Drawing.Size(Width, Height)
End Get
End Property
Public ReadOnly Property Location As System.Drawing.Point
Get
Return New System.Drawing.Point(Left, Top)
End Get
End Property
Public Function ToRectangle() As Rectangle
Return Rectangle.FromLTRB(Left, Top, Right, Bottom)
End Function
Public Shared Function FromRectangle(ByVal rectangle As Rectangle) As RECT
Return New RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom)
End Function
Public Overrides Function GetHashCode() As Integer
Return Left ^ ((Top << 13) Or (Top >> &H13)) _
^ ((Width << &H1A) Or (Width >> 6)) _
^ ((Height << 7) Or (Height >> &H19))
End Function
Public Shared Function FromXYWH(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As RECT
Return New RECT(x, y, x + width, y + height)
End Function
Public Shared Widening Operator CType(ByVal rect As RECT) As Rectangle
Return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom)
End Operator
Public Shared Widening Operator CType(ByVal rect As Rectangle) As RECT
Return New RECT(rect.Left, rect.Top, rect.Right, rect.Bottom)
End Operator
Public Overrides Function ToString() As String
Return "Left=" & Me.Left & ", Top=" & Me.Top & ", Right=" & Me.Right & ", Bottom=" & Me.Bottom
End Function
End Structure
End Namespace

View File

@@ -0,0 +1,46 @@
using System.Runtime.InteropServices;
namespace Pilz.Win32.Native;
/// <summary>
/// Contains information about a file object.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public const uint SHGFI_ICON = 0x100U;
public const uint SHGFI_LARGEICON = 0x0U;
public const uint SHGFI_SMALLICON = 0x1U;
/// <summary>
/// Handle to the icon that represents the file. You are responsible for
/// destroying this handle with DestroyIcon when you no longer need it.
/// </summary>
public nint hIcon;
/// <summary>
/// Index of the icon image within the system image list.
/// </summary>
public nint iIcon;
/// <summary>
/// Array of values that indicates the attributes of the file object.
/// For information about these values, see the IShellFolder::GetAttributesOf
/// method.
/// </summary>
public uint dwAttributes;
/// <summary>
/// String that contains the name of the file as it appears in the Microsoft
/// Windows Shell, or the path and file name of the file that contains the
/// icon representing the file.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
/// <summary>
/// String that describes the type of file.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}

View File

@@ -1,46 +0,0 @@
Imports System.Runtime.InteropServices
Namespace Native
''' <summary>
''' Contains information about a file object.
''' </summary>
<StructLayout(LayoutKind.Sequential)>
Public Structure SHFILEINFO
Public Const SHGFI_ICON As UInteger = &H100
Public Const SHGFI_LARGEICON As UInteger = &H0
Public Const SHGFI_SMALLICON As UInteger = &H1
''' <summary>
''' Handle to the icon that represents the file. You are responsible for
''' destroying this handle with DestroyIcon when you no longer need it.
''' </summary>
Public hIcon As IntPtr
''' <summary>
''' Index of the icon image within the system image list.
''' </summary>
Public iIcon As IntPtr
''' <summary>
''' Array of values that indicates the attributes of the file object.
''' For information about these values, see the IShellFolder::GetAttributesOf
''' method.
''' </summary>
Public dwAttributes As UInteger
''' <summary>
''' String that contains the name of the file as it appears in the Microsoft
''' Windows Shell, or the path and file name of the file that contains the
''' icon representing the file.
''' </summary>
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
Public szDisplayName As String
''' <summary>
''' String that describes the type of file.
''' </summary>
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)>
Public szTypeName As String
End Structure
End Namespace

View File

@@ -0,0 +1,68 @@
using System.Runtime.InteropServices;
namespace Pilz.Win32.Native;
public class Shell32
{
/// <summary>
/// Creates an array of handles to large or small icons extracted from
/// the specified executable file, dynamic-link library (DLL), or icon
/// file.
/// </summary>
/// <param name="lpszFile">
/// Name of an executable file, DLL, or icon file from which icons will
/// be extracted.
/// </param>
/// <param name="nIconIndex">
/// <para>
/// Specifies the zero-based index of the first icon to extract. For
/// example, if this value is zero, the function extracts the first
/// icon in the specified file.
/// </para>
/// <para>
/// If this value is <20>1 and <paramrefname="phiconLarge"/> and
/// <paramrefname="phiconSmall"/> are both NULL, the function returns
/// the total number of icons in the specified file. If the file is an
/// executable file or DLL, the return value is the number of
/// RT_GROUP_ICON resources. If the file is an .ico file, the return
/// value is 1.
/// </para>
/// <para>
/// Windows 95/98/Me, Windows NT 4.0 and later: If this value is a
/// negative number and either <paramrefname="phiconLarge"/> or
/// <paramrefname="phiconSmall"/> is not NULL, the function begins by
/// extracting the icon whose resource identifier is equal to the
/// absolute value of <paramrefname="nIconIndex"/>. For example, use -3
/// to extract the icon whose resource identifier is 3.
/// </para>
/// </param>
/// <param name="phIconLarge">
/// An array of icon handles that receives handles to the large icons
/// extracted from the file. If this parameter is NULL, no large icons
/// are extracted from the file.
/// </param>
/// <param name="phIconSmall">
/// An array of icon handles that receives handles to the small icons
/// extracted from the file. If this parameter is NULL, no small icons
/// are extracted from the file.
/// </param>
/// <param name="nIcons">
/// Specifies the number of icons to extract from the file.
/// </param>
/// <returns>
/// If the <paramrefname="nIconIndex"/> parameter is -1, the
/// <paramrefname="phIconLarge"/> parameter is NULL, and the
/// <paramrefname="phiconSmall"/> parameter is NULL, then the return
/// value is the number of icons contained in the specified file.
/// Otherwise, the return value is the number of icons successfully
/// extracted from the file.
/// </returns>
[DllImport("Shell32", CharSet = CharSet.Auto)]
public static extern int ExtractIconEx([MarshalAs(UnmanagedType.LPTStr)] string lpszFile, int nIconIndex, nint[] phIconLarge, nint[] phIconSmall, int nIcons);
[DllImport("shell32.dll")]
public static extern nint SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}

View File

@@ -1,68 +0,0 @@
Imports System.Runtime.InteropServices
Namespace Native
Public Class Shell32
''' <summary>
''' Creates an array of handles to large or small icons extracted from
''' the specified executable file, dynamic-link library (DLL), or icon
''' file.
''' </summary>
''' <param name="lpszFile">
''' Name of an executable file, DLL, or icon file from which icons will
''' be extracted.
''' </param>
''' <param name="nIconIndex">
''' <para>
''' Specifies the zero-based index of the first icon to extract. For
''' example, if this value is zero, the function extracts the first
''' icon in the specified file.
''' </para>
''' <para>
''' If this value is <20>1 and <paramrefname="phiconLarge"/> and
''' <paramrefname="phiconSmall"/> are both NULL, the function returns
''' the total number of icons in the specified file. If the file is an
''' executable file or DLL, the return value is the number of
''' RT_GROUP_ICON resources. If the file is an .ico file, the return
''' value is 1.
''' </para>
''' <para>
''' Windows 95/98/Me, Windows NT 4.0 and later: If this value is a
''' negative number and either <paramrefname="phiconLarge"/> or
''' <paramrefname="phiconSmall"/> is not NULL, the function begins by
''' extracting the icon whose resource identifier is equal to the
''' absolute value of <paramrefname="nIconIndex"/>. For example, use -3
''' to extract the icon whose resource identifier is 3.
''' </para>
''' </param>
''' <param name="phIconLarge">
''' An array of icon handles that receives handles to the large icons
''' extracted from the file. If this parameter is NULL, no large icons
''' are extracted from the file.
''' </param>
''' <param name="phIconSmall">
''' An array of icon handles that receives handles to the small icons
''' extracted from the file. If this parameter is NULL, no small icons
''' are extracted from the file.
''' </param>
''' <param name="nIcons">
''' Specifies the number of icons to extract from the file.
''' </param>
''' <returns>
''' If the <paramrefname="nIconIndex"/> parameter is -1, the
''' <paramrefname="phIconLarge"/> parameter is NULL, and the
''' <paramrefname="phiconSmall"/> parameter is NULL, then the return
''' value is the number of icons contained in the specified file.
''' Otherwise, the return value is the number of icons successfully
''' extracted from the file.
''' </returns>
<DllImport("Shell32", CharSet:=CharSet.Auto)>
Public Shared Function ExtractIconEx(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszFile As String, ByVal nIconIndex As Integer, ByVal phIconLarge As IntPtr(), ByVal phIconSmall As IntPtr(), ByVal nIcons As Integer) As Integer
End Function
Public Declare Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, ByVal dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, ByVal cbSizeFileInfo As UInteger, ByVal uFlags As UInteger) As IntPtr
End Class
End Namespace

View File

@@ -0,0 +1,20 @@
using System.Runtime.InteropServices;
namespace Pilz.Win32.Native;
public class User32
{
private const string LIB_USER32 = "user32.dll";
[DllImport(LIB_USER32)]
public static extern bool GetWindowRect(nint hWnd, ref RECT r);
[DllImport(LIB_USER32)]
public static extern nint ChildWindowFromPointEx(nint hWndParent, POINT pt, uint uFlags);
[DllImport(LIB_USER32)]
public static extern short GetKeyState(int keyCode);
}

View File

@@ -1,23 +0,0 @@
Imports System.Runtime.InteropServices
Namespace Native
Public Class User32
Private Const LIB_USER32 As String = "user32.dll"
<DllImport(LIB_USER32)>
Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef r As RECT) As Boolean
End Function
<DllImport(LIB_USER32)>
Public Shared Function ChildWindowFromPointEx(ByVal hWndParent As IntPtr, ByVal pt As POINT, ByVal uFlags As UInteger) As IntPtr
End Function
<DllImport(LIB_USER32)>
Public Shared Function GetKeyState(keyCode As Integer) As Short
End Function
End Class
End Namespace

View File

@@ -0,0 +1,11 @@
namespace Pilz.Win32.Native;
[Flags]
public enum WindowFromPointFlags
{
CWP_ALL = 0x0,
CWP_SKIPINVISIBLE = 0x1,
CWP_SKIPDISABLED = 0x2,
CWP_SKIPTRANSPARENT = 0x4
}

View File

@@ -1,11 +0,0 @@
Namespace Native
<Flags>
Public Enum WindowFromPointFlags
CWP_ALL = &H0
CWP_SKIPINVISIBLE = &H1
CWP_SKIPDISABLED = &H2
CWP_SKIPTRANSPARENT = &H4
End Enum
End Namespace