Skip to content

Instantly share code, notes, and snippets.

@cnbluefire
cnbluefire / Directory.Build.props
Last active April 20, 2026 15:33
Enable the "MSIX packaging" context menu in Visual Studio.
<Project>
<PropertyGroup>
<EnableMsixTooling>true</EnableMsixTooling>
<LogSDKReferenceResolutionErrorsAsWarnings>true</LogSDKReferenceResolutionErrorsAsWarnings>
<RuntimeIdentifier Condition="'$(Platform)' == 'x86'">win-x86</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(Platform)' == 'win32'">win-x86</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(Platform)' == 'x64'">win-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="'$(Platform)' == 'arm64'">win-arm64</RuntimeIdentifier>
<_PublishProfileFileName>win-$(Platform).pubxml</_PublishProfileFileName>
<PublishProfile Condition="'$(_PublishProfileFileName)' != '' and Exists('$(MSBuildProjectDirectory)\Properties\PublishProfiles\$(_PublishProfileFileName)')">$(_PublishProfileFileName)</PublishProfile>
@cnbluefire
cnbluefire / DeviceFamilyInfoHelper.cs
Created April 16, 2026 10:39
Retrieving device information using RtlGetDeviceFamilyInfoEnum.
using System.Runtime.InteropServices;
public static class DeviceFamilyInfoHelper
{
private static nint pGetDeviceFamilyInfo;
static DeviceFamilyInfoHelper()
{
if (!NativeLibrary.TryLoad("ntdll.dll", out var handle)
|| !NativeLibrary.TryGetExport(handle, "RtlGetDeviceFamilyInfoEnum", out pGetDeviceFamilyInfo))
@cnbluefire
cnbluefire / CompositionEffect.cs
Last active May 7, 2026 06:08
Composition effect brush without Win2D
using System.Collections;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Windows.UI.Composition;
using WinRT;
@cnbluefire
cnbluefire / TextBlockExtensions.cs
Created March 16, 2026 13:57
WinUI3 get TextBlock character bounding box.
public static class TextBlockExtensions
{
// https://github.com/microsoft/microsoft-ui-xaml/blob/main/src/dxaml/xcp/core/text/TextBlock/TextBlockView.cpp#L606
public static TextPointer GetAdjustedPosition(this TextBlock textBlock, int charIndex)
{
const int PlaceHolderPositionsForInlines = 2;
var contentLength = textBlock.Text.Length + 2 * PlaceHolderPositionsForInlines;
int charCount = charIndex;
@cnbluefire
cnbluefire / ContentExternalBackdropLink.cs
Created January 14, 2026 09:12
ContentExternalOutputLink and ContentExternalBackdropLink
@cnbluefire
cnbluefire / Frog.cs
Last active December 4, 2025 09:46
猜成语小玩具
using System.Text.Json;
var data = LoadIdioms();
Console.WriteLine("""
使用空格分割四字的个匹配,支持以下模式:
汉字:精确匹配
字母:模糊匹配拼音
字母+感叹号:精确匹配拼音
数字:音调,可以追加到字母后面,如sheng!1
下划线:忽略
@cnbluefire
cnbluefire / Program.cs
Created August 18, 2025 11:12
CsWin32 ComWrappers Test
using System.Runtime.InteropServices;
using System;
using Windows.Win32.Foundation;
using Windows.Win32.System.Com;
using System.Runtime.CompilerServices;
using System.IO;
using Windows.Win32;
using System.Collections;
using CsWin32ComWrappersTest;
using System.Security.Cryptography;
@cnbluefire
cnbluefire / Program.cs
Created August 7, 2025 14:44
Create a Windows notification to open a url using Edge.
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
// Edge Package AUMID
const string AUMID = "Microsoft.MicrosoftEdge.Stable_8wekyb3d8bbwe!App";
var notifier = ToastNotificationManager.CreateToastNotifier(AUMID);
var xml = $$$"""
<toast launch='{{{BuildLaunchId(new Uri("https://www.4399.com/"))}}}'>
@cnbluefire
cnbluefire / CompositionPathFactory.cs
Last active July 31, 2025 11:04
Create CompositionPath from ID2D1Geometry
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.UI.Composition;
using WinRT;
public static class CompositionPathFactory
{
private static ref readonly Guid IID_ICompositionPathFactory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@cnbluefire
cnbluefire / FlipViewHelper.cs
Last active April 25, 2025 10:06
禁止使用鼠标滚轮操作 FlipView
public static class FlipViewHelper
{
public static bool GetAllowPointerWheelFlip(FlipView obj)
{
return (bool)obj.GetValue(AllowPointerWheelFlipProperty);
}
public static void SetAllowPointerWheelFlip(FlipView obj, bool value)
{
obj.SetValue(AllowPointerWheelFlipProperty, value);