2021-12-28 16:53:45 +08:00

281 lines
9.1 KiB
C#

#if PREVIEW_HANDLER
using System;
using System.Runtime.InteropServices;
namespace ShellExtensions.Interop.Common
{
/// <summary>
/// Wraps the native Windows MSG structure.
/// </summary>
public struct Message
{
/// <summary>
/// Gets the window handle
/// </summary>
public IntPtr WindowHandle { get; }
/// <summary>
/// Gets the window message
/// </summary>
public uint Msg { get; }
/// <summary>
/// Gets the WParam
/// </summary>
public IntPtr WParam { get; }
/// <summary>
/// Gets the LParam
/// </summary>
public IntPtr LParam { get; }
/// <summary>
/// Gets the time
/// </summary>
public int Time { get; }
/// <summary>
/// Gets the point
/// </summary>
public NativePoint Point { get; }
/// <summary>
/// Creates a new instance of the Message struct
/// </summary>
/// <param name="windowHandle">Window handle</param>
/// <param name="msg">Message</param>
/// <param name="wparam">WParam</param>
/// <param name="lparam">LParam</param>
/// <param name="time">Time</param>
/// <param name="point">Point</param>
internal Message(IntPtr windowHandle, uint msg, IntPtr wparam, IntPtr lparam, int time, NativePoint point)
: this()
{
WindowHandle = windowHandle;
Msg = msg;
WParam = wparam;
LParam = lparam;
Time = time;
Point = point;
}
/// <summary>
/// Determines if two messages are equal.
/// </summary>
/// <param name="first">First message</param>
/// <param name="second">Second message</param>
/// <returns>True if first and second message are equal; false otherwise.</returns>
public static bool operator ==(Message first, Message second)
{
return first.WindowHandle == second.WindowHandle
&& first.Msg == second.Msg
&& first.WParam == second.WParam
&& first.LParam == second.LParam
&& first.Time == second.Time
&& first.Point == second.Point;
}
/// <summary>
/// Determines if two messages are not equal.
/// </summary>
/// <param name="first">First message</param>
/// <param name="second">Second message</param>
/// <returns>True if first and second message are not equal; false otherwise.</returns>
public static bool operator !=(Message first, Message second)
{
return !(first == second);
}
/// <summary>
/// Determines if this message is equal to another.
/// </summary>
/// <param name="obj">Another message</param>
/// <returns>True if this message is equal argument; false otherwise.</returns>
public override bool Equals(object obj)
{
return obj != null && obj is Message message && this == message;
}
/// <summary>
/// Gets a hash code for the message.
/// </summary>
/// <returns>Hash code for this message.</returns>
public override int GetHashCode()
{
int hash = WindowHandle.GetHashCode();
hash = hash * 31 + Msg.GetHashCode();
hash = hash * 31 + WParam.GetHashCode();
hash = hash * 31 + LParam.GetHashCode();
hash = hash * 31 + Time.GetHashCode();
hash = hash * 31 + Point.GetHashCode();
return hash;
}
}
/// <summary>
/// A wrapper for the native POINT structure.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct NativePoint
{
/// <summary>
/// Initialize the NativePoint
/// </summary>
/// <param name="x">The x coordinate of the point.</param>
/// <param name="y">The y coordinate of the point.</param>
public NativePoint(int x, int y)
: this()
{
X = x;
Y = y;
}
/// <summary>
/// The X coordinate of the point
/// </summary>
public int X { get; set; }
/// <summary>
/// The Y coordinate of the point
/// </summary>
public int Y { get; set; }
/// <summary>
/// Determines if two NativePoints are equal.
/// </summary>
/// <param name="first">First NativePoint</param>
/// <param name="second">Second NativePoint</param>
/// <returns>True if first NativePoint is equal to the second; false otherwise.</returns>
public static bool operator ==(NativePoint first, NativePoint second)
{
return first.X == second.X
&& first.Y == second.Y;
}
/// <summary>
/// Determines if two NativePoints are not equal.
/// </summary>
/// <param name="first">First NativePoint</param>
/// <param name="second">Second NativePoint</param>
/// <returns>True if first NativePoint is not equal to the second; false otherwise.</returns>
public static bool operator !=(NativePoint first, NativePoint second)
{
return !(first == second);
}
/// <summary>
/// Determines if this NativePoint is equal to another.
/// </summary>
/// <param name="obj">Another NativePoint to compare</param>
/// <returns>True if this NativePoint is equal obj; false otherwise.</returns>
public override bool Equals(object obj)
{
return obj != null && obj is NativePoint point && this == point;
}
/// <summary>
/// Gets a hash code for the NativePoint.
/// </summary>
/// <returns>Hash code for the NativePoint</returns>
public override int GetHashCode()
{
int hash = X.GetHashCode();
hash = hash * 31 + Y.GetHashCode();
return hash;
}
}
/// <summary>
/// A wrapper for a RECT struct
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct NativeRect
{
/// <summary>
/// Position of left edge
/// </summary>
public int Left { get; set; }
/// <summary>
/// Position of top edge
/// </summary>
public int Top { get; set; }
/// <summary>
/// Position of right edge
/// </summary>
public int Right { get; set; }
/// <summary>
/// Position of bottom edge
/// </summary>
public int Bottom { get; set; }
/// <summary>
/// Creates a new NativeRect initialized with supplied values.
/// </summary>
/// <param name="left">Position of left edge</param>
/// <param name="top">Position of top edge</param>
/// <param name="right">Position of right edge</param>
/// <param name="bottom">Position of bottom edge</param>
public NativeRect(int left, int top, int right, int bottom)
: this()
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
/// <summary>
/// Determines if two NativeRects are equal.
/// </summary>
/// <param name="first">First NativeRect</param>
/// <param name="second">Second NativeRect</param>
/// <returns>True if first NativeRect is equal to second; false otherwise.</returns>
public static bool operator ==(NativeRect first, NativeRect second)
{
return first.Left == second.Left
&& first.Top == second.Top
&& first.Right == second.Right
&& first.Bottom == second.Bottom;
}
/// <summary>
/// Determines if two NativeRects are not equal
/// </summary>
/// <param name="first">First NativeRect</param>
/// <param name="second">Second NativeRect</param>
/// <returns>True if first is not equal to second; false otherwise.</returns>
public static bool operator !=(NativeRect first, NativeRect second)
{
return !(first == second);
}
/// <summary>
/// Determines if the NativeRect is equal to another Rect.
/// </summary>
/// <param name="obj">Another NativeRect to compare</param>
/// <returns>True if this NativeRect is equal to the one provided; false otherwise.</returns>
public override bool Equals(object obj)
{
return obj != null && obj is NativeRect rect && this == rect;
}
/// <summary>
/// Creates a hash code for the NativeRect
/// </summary>
/// <returns>Returns hash code for this NativeRect</returns>
public override int GetHashCode()
{
int hash = Left.GetHashCode();
hash = hash * 31 + Top.GetHashCode();
hash = hash * 31 + Right.GetHashCode();
hash = hash * 31 + Bottom.GetHashCode();
return hash;
}
}
}
#endif