Skip to content

Instantly share code, notes, and snippets.

@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>
@emoacht
emoacht / ArrayConstructorExtension.cs
Created June 5, 2022 14:01
MarkupExtension to construct an array
[MarkupExtensionReturnType(typeof(object[]))]
public class ArrayConstructorExtension : MarkupExtension
{
private readonly object[] _elements;
public ArrayConstructorExtension(object a) => _elements = new[] { a };
public ArrayConstructorExtension(object a, object b) => _elements = new[] { a, b };
public ArrayConstructorExtension(object a, object b, object c) => _elements = new[] { a, b, c };
public ArrayConstructorExtension(object a, object b, object c, object d) => _elements = new[] { a, b, c, d };
public ArrayConstructorExtension(object a, object b, object c, object d, object e) => _elements = new[] { a, b, c, d, e };
@emoacht
emoacht / StoreHelperNew2.cs
Created June 3, 2022 14:09
Set owner window after .NET 5.0.
// using WinRT.Interop;
public void SetOwnerWindow(StoreContext context, Window window)
{
var handle = new WindowInteropHelper(window).Handle;
InitializeWithWindow.Initialize(context, handle);
}
@emoacht
emoacht / StoreHelperNew1.cs
Last active June 3, 2022 14:17
Set owner window after .NET 5.0.
// using WinRT;
public void SetOwnerWindow(StoreContext context, Window window)
{
var handle = new WindowInteropHelper(window).Handle;
var initWindow = context.As<IInitializeWithWindow>();
initWindow.Initialize(handle);
}
@emoacht
emoacht / StoreHelperOld.cs
Created June 3, 2022 13:52
Set owner window before .NET 5.0.
[ComImport]
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInitializeWithWindow
{
void Initialize(IntPtr hwnd);
}
public void SetOwnerWindow(StoreContext context, Window window)
{
@emoacht
emoacht / StoreHelper.cs
Created June 3, 2022 13:25
Update package by Windows.Services.Store API on .NET 5.0.
using System;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Windows.Services.Store;
using WinRT.Interop;
internal static class StoreHelper
{