Skip to content

Instantly share code, notes, and snippets.

@xv
xv / Win32Macros.cs
Last active January 7, 2024 01:23
Common Win32 API macros.
public static byte LOBYTE(ushort a) =>
(byte)(a & 0xFF);
public static byte HIBYTE(ushort a) =>
(byte)((a >> 8) & 0xFF);
public static ushort HIWORD(uint a) =>
(ushort)((a >> 16) & 0xFFFF);
public static ushort LOWORD(uint a) =>
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define CLAMP(N, LO, HI) (MIN(MAX((LO), (N)), (HI)))
using System.ComponentModel;
using System.Windows.Interop;
using System.Windows.Input;
using System.Runtime.InteropServices;
using System.IO;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
using Windows.Win32;
@xv
xv / CycleEnum.cs
Last active October 28, 2025 03:32
Cycles through an enum upward or downward and clamps the index.
private static T CycleEnum<T>(T type, int direction) where T : Enum
{
var values = (T[])Enum.GetValues(typeof(T));
var index = Array.IndexOf(values, type);
var newIndex = Math.Clamp(index + direction, 0, values.Length - 1);
return values[newIndex];
}