Skip to content

Instantly share code, notes, and snippets.

@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];
}
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;
#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)))
@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) =>
@xv
xv / calc_check_digit.py
Last active June 15, 2025 01:45
Python function to calculate the check digit of a UPC-12 barcode.
def calc_check_digit(digits):
digits = [int(i) for i in str(digits)][:-1]
odd_pos_list = digits[0::2] # 1st, 3rd, 5th, etc
even_pos_list = digits[1::2] # 2nd, 4th, 6th, etc
n = (sum(odd_pos_list) * 3) + sum(even_pos_list)
rmndr = n % 10
return (10 - rmndr) if rmndr > 0 else 0
@xv
xv / clock.ps1
Last active June 15, 2022 19:57
PowerShell script utilizing .NET WinForms to create a GUI with a live clock label.
Add-Type -AssemblyName System.Windows.Forms
$Win32_ShowWindow = @'
[DllImport("user32.dll")]
public static extern bool ShowWindow(int handle, int nCmdShow);
'@
$User32 = Add-Type -MemberDefinition $Win32_ShowWindow `
-Name 'User32' -Namespace 'Win32' -PassThru
@xv
xv / ShellFileIcon.cs
Created November 6, 2020 10:52
Simple method to retrieve the 16x16 shell file icon using SHFILEINFO structure.
internal const uint FILE_ATTRIBUTE_NORMAL = 0x80;
[Flags]
internal enum SHGetFileInfoFlags
{
SHGFI_LARGEICON = 0x0,
SHGFI_SMALLICON = 0x1,
SHGFI_OPENICON = 0x2,
SHGFI_SHELLICONSIZE = 0x4,
SHGFI_PIDL = 0x8,
@xv
xv / UndocumentedAcrylic.cs
Created November 5, 2020 20:05
Undocumented API for Windows 10 Acrylic style.
internal enum WindowCompositionAttribute
{
WCA_UNDEFINED = 0,
WCA_NCRENDERING_ENABLED = 1,
WCA_NCRENDERING_POLICY = 2,
WCA_TRANSITIONS_FORCEDISABLED = 3,
WCA_ALLOW_NCPAINT = 4,
WCA_CAPTION_BUTTON_BOUNDS = 5,
WCA_NONCLIENT_RTL_LAYOUT = 6,
WCA_FORCE_ICONIC_REPRESENTATION = 7,
@xv
xv / toggle_disk_space_warning.bat
Last active October 17, 2020 11:56
Tiny script to enbale/disable the "Low Disk Space" warning.
@echo off
rem Run this script as administrator
rem You may need to restart your computer to take effect
set hKey=HKLM
for /f "tokens=2 delims==." %%a in ('wmic os get version /value') do (
if %%a neq 10 set hKey=HKCU
)
@xv
xv / ColorFromHTML.cs
Created May 13, 2020 10:43
A much faster alternative to Microsoft's ColorTranslator.FromHtml() function.
/// <summary>
/// Translates an HTML-format color value to a GDI+
/// <see cref="System.Drawing.Color"/> structure.
/// </summary>
///
/// <param name="htmlVal">
/// A 3 or 6 digit hexadecimal value to translate. Do not include the
/// leading "#" sign.
/// </param>
///