2023-05-01 09:46:07 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Vanara.Extensions;
|
|
|
|
|
using Vanara.Extensions.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace Vanara.PInvoke;
|
|
|
|
|
|
2023-05-09 13:56:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Use this attribute to specify the types of the wParam and lParam values of a message. If not specified, the default is IntPtr for both.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <seealso cref="System.Attribute"/>
|
2023-05-01 09:46:07 -04:00
|
|
|
|
public class MsgParamsAttribute : Attribute
|
|
|
|
|
{
|
2023-05-09 13:56:16 -04:00
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="MsgParamsAttribute"/> class.</summary>
|
|
|
|
|
/// <param name="wParamType">Type of the wParam.</param>
|
|
|
|
|
/// <param name="lParamType">Type of the lParam.</param>
|
2023-05-01 09:46:07 -04:00
|
|
|
|
public MsgParamsAttribute(Type? wParamType, Type? lParamType)
|
|
|
|
|
{
|
|
|
|
|
WParamType = wParamType;
|
|
|
|
|
LParamType = lParamType;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 13:56:16 -04:00
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="MsgParamsAttribute"/> class.</summary>
|
|
|
|
|
public MsgParamsAttribute() => WParamType = LParamType = null;
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets the type of the lParam.</summary>
|
|
|
|
|
/// <value>The type of the lParam.</value>
|
|
|
|
|
public Type? LParamType { get; set; }
|
2023-05-01 09:46:07 -04:00
|
|
|
|
|
2023-05-09 13:56:16 -04:00
|
|
|
|
/// <summary>Gets or sets the type of the LRESULT.</summary>
|
|
|
|
|
/// <value>The type of the LRESULT.</value>
|
|
|
|
|
public Type? LResultType { get; set; } = typeof(int);
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets the type of the wParam.</summary>
|
|
|
|
|
/// <value>The type of the wParam.</value>
|
|
|
|
|
public Type? WParamType { get; set; }
|
2023-05-01 09:46:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 13:56:16 -04:00
|
|
|
|
/// <summary></summary>
|
2023-05-01 09:46:07 -04:00
|
|
|
|
public static class MsgExtensions
|
|
|
|
|
{
|
2023-05-09 13:56:16 -04:00
|
|
|
|
/// <summary>Gets the parameters for a message using the <see cref="MsgParamsAttribute"/> associated with the message.</summary>
|
|
|
|
|
/// <typeparam name="TEnum">The type of the message enum.</typeparam>
|
|
|
|
|
/// <param name="msg">The MSG value.</param>
|
|
|
|
|
/// <returns>The wParam and lParam in a tuple tied to their assigned value types.</returns>
|
2023-05-01 09:46:07 -04:00
|
|
|
|
public static (object? wParam, object? lParam) GetParams<TEnum>(this MSG msg) where TEnum : Enum
|
|
|
|
|
{
|
|
|
|
|
MsgParamsAttribute? attr = typeof(TEnum).GetCustomAttribute<MsgParamsAttribute>();
|
2023-05-09 13:56:16 -04:00
|
|
|
|
if (attr is null || attr.WParamType == typeof(IntPtr) && attr.LParamType == typeof(IntPtr))
|
|
|
|
|
{
|
2023-05-01 09:46:07 -04:00
|
|
|
|
return (msg.wParam, msg.lParam);
|
2023-05-09 13:56:16 -04:00
|
|
|
|
}
|
2023-05-01 09:46:07 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
2023-05-13 22:16:32 -04:00
|
|
|
|
object? wParam = attr.WParamType is null ? null : attr.WParamType.IsNullable() ? msg.wParam.ToStructure(attr.WParamType) : msg.wParam.ToInt32().CastTo(attr.WParamType);
|
|
|
|
|
object? lParam = attr.LParamType is null ? null : attr.LParamType.IsNullable() ? msg.lParam.ToStructure(attr.LParamType) : msg.lParam.ToInt32().CastTo(attr.LParamType);
|
2023-05-01 09:46:07 -04:00
|
|
|
|
return (wParam, lParam);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|