Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / InputDeviceHelper.cs
Created February 19, 2024 10:34
Find input devices.
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class InputDeviceHelper
{
[DllImport("User32", SetLastError = true)]
private static extern uint GetRawInputDeviceList(
[Out] RAWINPUTDEVICELIST[]? pRawInputDeviceList,
ref uint puiNumDevices,
uint cbSize);
@emoacht
emoacht / StorageManagement.cs
Last active November 19, 2023 21:49
C# console app to utilize MSFT_PhysicalDisk
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
class Program
{
static void Main(string[] args)
{
Check();
@emoacht
emoacht / PropertyPatternTest.cs
Created November 10, 2023 05:26
Test null check of property of Property pattern
public static class PropertyPatternTest
{
public static void Test()
{
var t = new TestClass();
// Case 1: False
TestBase(() => t is { Data: { Length: > 0 } });
// Case 2: False
@emoacht
emoacht / BytesConverter.cs
Created November 8, 2023 00:47
Convert 4 data bytes to unsigned integer.
public static class BytesConverter
{
/// <summary>
/// Converts 4 data bytes to unsigned integer.
/// </summary>
/// <param name="mh">High order bytes when using four data bytes</param>
/// <param name="ml">Low order bytes when using four data bytes</param>
/// <param name="sh">High order bytes when using two data bytes</param>
/// <param name="sl">Low order bytes when using two data bytes</param>
/// <returns>Unsigned integer</returns>
@emoacht
emoacht / WindowHelper.cs
Last active October 1, 2023 00:29
Utility method to set Window's location and size
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public static class WindowHelper
{
public static void SetWindowRect(Window window, int x, int y, int width, int height)
{
IntPtr windowHandle = new WindowInteropHelper(window).EnsureHandle();
@emoacht
emoacht / Program.cs
Created October 25, 2015 04:52
Sample of Task.Factory.FromAsync method
using System;
using System.IO;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
if (0 == args.Length)
return;
@emoacht
emoacht / SvgImageHelper.cs
Created August 5, 2023 05:39
Helper method for Windows.UI.Xaml.Media.Imaging.SvgImageSource
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
@emoacht
emoacht / SKHelper.cs
Last active July 25, 2023 23:54
Get BitmapImage from SKImage of SkiaSharp
using System.IO;
using System.Windows.Media.Imaging;
using SkiaSharp;
public static class SKHelper
{
public static BitmapImage Convert(string filePath)
{
using SKImage image = SKImage.FromEncodedData(filePath);
using SKData encoded = image.Encode(); // PNG format
@emoacht
emoacht / ItemsControlBehavior.cs
Created July 7, 2023 00:01
Behavior to capture the change of source collection.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.Xaml.Behaviors;
public class ItemsControlBehavior : Behavior<ItemsControl>
{
@emoacht
emoacht / DialogHelper.cs
Created May 20, 2023 09:47
Use WinRT's FileOpenPicker and FolderPicker from WPF.
using System;
using System.Threading.Tasks;
using System.Windows.Interop;
using Windows.Storage.Pickers;
public static async Task<string?> OpenFileDialog(System.Windows.Window window)
{
var picker = new FileOpenPicker()
{
ViewMode = PickerViewMode.Thumbnail