Skip to content

Instantly share code, notes, and snippets.

View xv's full-sized avatar
🇷🇺

Jad xv

🇷🇺
View GitHub Profile
@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 April 2, 2023 17:46
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>
///
@xv
xv / InvertWindowBorder.cs
Created March 4, 2020 09:14
Flashes a screen-inverted rectangle around a window like in Microsoft Spy++.
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
static extern int SaveDC(IntPtr hWnd);
@xv
xv / cpp-cli-form-set-up.md
Created September 8, 2019 03:17
A really short guide on how to set up a UI form in C++/CLI.

If you are playing with C++/CLI you are probably already familiar with the C# or VB.NET way of doing WinForms, so there's nothing to be afraid of in CLI -- it is just a bit of pain in the ass at first.

Setting up the project

Assuming that you have C++/CLI installed, fire up Visual Studio and create an empty CLR project. Once the project is created, we need to let Visual Studio know what type of project (WinForms in this case) this is, as well as set an entry point for the application.

Open the project's Properties dialog (Alt+Enter) and select All Platforms from the Platform combo box. After that, you need to:

Define the project type

  1. Navigate to Configuration Properties &gt;&gt; Linker &gt;&gt; System in the tree-view control on the left side.
@xv
xv / del_file_content.bat
Created May 18, 2019 18:56
Iterates through files of a matching extension and deletes their content while keeping the files themselves.
@echo off
rem change .txt to whatever extension you want
type nul > content
for %%f in (*.txt) do copy /y content %%f
del content