Skip to content

Instantly share code, notes, and snippets.

@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>();
@emoacht
emoacht / TaskbarHelper.cs
Created April 14, 2023 02:25
Switch taskbar aute hide state.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class TaskbarHelper
{
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
extern static IntPtr FindWindow(
[MarshalAs(UnmanagedType.LPWStr), In] string lpClassName,
[MarshalAs(UnmanagedType.LPWStr), In] string? lpWindowName);
@emoacht
emoacht / MainWindow.xaml.cs
Last active March 25, 2023 03:51
Generate and consume IAsyncEnumerable<T>.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
@emoacht
emoacht / MonitorHelper.cs
Created January 17, 2023 12:44
Enumerate monitor connections by WMI.
using System.Collections.Generic;
using System.Linq;
using System.Management;
public static class MonitorHelper
{
public static IEnumerable<MonitorConnection> EnumerateMonitorConnections()
{
var @class = new ManagementClass(@"root\wmi:WmiMonitorConnectionParams");
@emoacht
emoacht / CollectionToBooleanConverter.cs
Created January 5, 2023 01:18
Converts the state whether the source object is a collection and it has any item to boolean.
using System;
using System.Collections;
using System.Globalization;
using System.Windows.Data;
public class CollectionToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is IEnumerable collection)
@emoacht
emoacht / WindowHelper.cs
Last active December 6, 2022 20:23
Test alternative PointToScreen and ScreenToPoint methods.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
public static class WindowHelper
{
public static Point PointToScreen(Visual visual, Point point)
{
@emoacht
emoacht / WindowHelper.cs
Created December 5, 2022 23:36
Moves a specified Window to the center of a monitor where the cursor locates.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public static class WindowHelper
{
/// <summary>
/// Moves a specified Window to the center of a monitor where the cursor locates.
/// </summary>
@emoacht
emoacht / EnumerableExtension.cs
Created September 4, 2022 04:38
Extension method to group the elements by key in source sequence
public static class EnumerableExtension
{
public static IEnumerable<IGrouping<TSource, TSource>> GroupBy<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> keyFinder)
{
if (source is null)
throw new ArgumentNullException(nameof(source));
if (keyFinder is null)
throw new ArgumentNullException(nameof(keyFinder));
TSource key = default;
@emoacht
emoacht / TaskHelper.cs
Last active August 28, 2022 04:26
Aynchronously numerate Task results with timeout and cancellation.
public static class TaskHelper
{
public static async Task<IEnumerable<TResult>> EnumerateTaskResults<TResult>(IEnumerable<Func<TResult>> source, TimeSpan timeout, CancellationToken cancellationToken)
{
var enumerateTasks = source
.Select(x => Task.Run(() => x.Invoke()))
.ToArray();
var timeoutTask = Task.Delay(timeout, cancellationToken);
@emoacht
emoacht / TupleTest.cs
Created August 5, 2022 23:50
Store ValueTuple or Tuple on the Clipboard and retrieve it.
// using System.Windows;
public class TupleTest
{
public void TupleClipboardTest1()
{
(int, string) original = (11, "Eleven");
Clipboard.SetData(DataFormats.Serializable, original);
object retrieved = Clipboard.GetData(DataFormats.Serializable);