Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / Program.cs
Last active July 21, 2024 07:04
Enumerate all windows on the desktop. To produce correct geometries, app.manifest must be added.
using System.Runtime.InteropServices;
using System.Text;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Generation, Name, X,Y,Width,Height");
foreach (var w in GetAllWindows())
@emoacht
emoacht / Program.cs
Created April 30, 2024 20:48
Show disk drives. This is slightly modified version of https://gist.github.com/emoacht/0afa084cf30123f121492ab26fb8174d
using System.Runtime.InteropServices;
using System.Text;
class Program
{
static void Main(string[] args)
{
ShowDiskDrives();
}
@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