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 / 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 / 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
@emoacht
emoacht / MainWindow1.xaml
Created April 29, 2023 21:31
Sample Styles of Round Button
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="400" Height="200">
<Window.Resources>
<Style x:Key="RoundStyle" TargetType="{x:Type Button}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Foreground">
@emoacht
emoacht / DependencyObjectExtensions.cs
Created April 15, 2023 02:30
Scroll ScrollViewer of ListBox by mouse wheel.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Media;
public static class DependencyObjectExtensions
{
public static bool TryFindDescendant<T>(this DependencyObject reference, [NotNullWhen(true)] out T? descendant) where T : DependencyObject
{
var queue = new Queue<DependencyObject>();