Skip to content

Instantly share code, notes, and snippets.

@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);
@emoacht
emoacht / WindowHelper.cs
Created August 3, 2022 04:08
Drag window without title bar by mouse left button down.
public static class WindowHelper
{
public static bool GetIsDragEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsDragEnabledProperty);
}
public static void SetIsDragEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsDragEnabledProperty, value);
}
@emoacht
emoacht / WindowHelper.cs
Created July 30, 2022 12:45
Get image data of FrameworkElement in PNG format.
public static class WindowHelper
{
public static byte[] GetImageData(FrameworkElement element)
{
var dpi = VisualTreeHelper.GetDpi(element);
var rtb = new RenderTargetBitmap(
(int)(element.ActualWidth * dpi.DpiScaleX),
(int)(element.ActualHeight * dpi.DpiScaleY),
dpi.PixelsPerInchX,
@emoacht
emoacht / MonitorHelper.ps1
Created July 23, 2022 18:07
Get size of primary monitor by PowerShell. To get actual size under Per-Monitor DPI environment, run this method in PowerShell ISE.
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class MonitorHelper
{
[DllImport("User32.dll")]
public static extern IntPtr MonitorFromWindow(
IntPtr hwnd,
int dwFlags);
@emoacht
emoacht / Program.cs
Created July 6, 2022 14:59
Execute command in .NET 5.0 and newer.
public static async Task<string[]> ExecuteCommandAsync(string command)
{
using var p = new Process();
p.StartInfo = new ProcessStartInfo("cmd.exe")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
@emoacht
emoacht / VersionFormat.xaml
Last active June 29, 2022 00:14
Fill TextBlock with major and minor version.
<TextBlock>
<TextBlock.Text>
<!-- View is bound to View Model which has Version Version property. -->
<MultiBinding StringFormat="v{0}.{1}">
<Binding Path="Version.Major"/>
<Binding Path="Version.Minor"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>