重构Win32Natives

This commit is contained in:
ww-rm
2025-11-08 19:17:09 +08:00
parent fb319e09d8
commit 8e771fbaa4
19 changed files with 91 additions and 60 deletions

View File

@@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpineRuntime35", "SpineRunt
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpineRuntime34", "SpineRuntimes\SpineRuntime34\SpineRuntime34.csproj", "{348605F7-3FF4-1DE0-4B91-7AEFE7BC5C55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Win32Natives", "Win32Natives\Win32Natives.csproj", "{48864874-7307-950E-A667-62BB66357C62}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@@ -105,6 +107,10 @@ Global
{348605F7-3FF4-1DE0-4B91-7AEFE7BC5C55}.Debug|x64.Build.0 = Debug|x64
{348605F7-3FF4-1DE0-4B91-7AEFE7BC5C55}.Release|x64.ActiveCfg = Release|x64
{348605F7-3FF4-1DE0-4B91-7AEFE7BC5C55}.Release|x64.Build.0 = Release|x64
{48864874-7307-950E-A667-62BB66357C62}.Debug|x64.ActiveCfg = Debug|x64
{48864874-7307-950E-A667-62BB66357C62}.Debug|x64.Build.0 = Debug|x64
{48864874-7307-950E-A667-62BB66357C62}.Release|x64.ActiveCfg = Release|x64
{48864874-7307-950E-A667-62BB66357C62}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -1,6 +1,6 @@
using Microsoft.Win32;
using NLog;
using SpineViewer.Natives;
using Win32Natives;
using SpineViewer.Resources;
using SpineViewer.Services;
using SpineViewer.ViewModels.MainWindow;
@@ -15,6 +15,7 @@ using System.IO.Pipes;
using System.Reflection;
using System.Windows;
using System.Windows.Interop;
using SpineViewer.Extensions;
namespace SpineViewer
{
@@ -365,9 +366,8 @@ namespace SpineViewer
{
Resources.MergedDictionaries.Add(new() { Source = new(uri, UriKind.Relative) });
Resources.MergedDictionaries.Add(new() { Source = new("Resources/Theme.xaml", UriKind.Relative) });
var hwnd = new WindowInteropHelper(Current.MainWindow).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
Current.MainWindow.SetWindowTextColor(AppResource.Color_PrimaryText);
Current.MainWindow.SetWindowCaptionColor(AppResource.Color_Region);
_skin = value;
}
catch (Exception ex)

View File

@@ -1,6 +1,4 @@
using SFML.Graphics;
using SFML.System;
using SkiaSharp;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;
@@ -9,29 +7,31 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Win32Natives;
namespace SpineViewer.Extensions
{
public static class WpfExtension
{
public static FloatRect ToFloatRect(this Rect self)
public static SFML.Graphics.FloatRect ToFloatRect(this Rect self)
{
return new((float)self.X, (float)self.Y, (float)self.Width, (float)self.Height);
}
public static Vector2f ToVector2f(this Size self)
public static SFML.System.Vector2f ToVector2f(this Size self)
{
return new((float)self.Width, (float)self.Height);
}
public static Vector2u ToVector2u(this Size self)
public static SFML.System.Vector2u ToVector2u(this Size self)
{
return new((uint)self.Width, (uint)self.Height);
}
public static Vector2i ToVector2i(this Size self)
public static SFML.System.Vector2i ToVector2i(this Size self)
{
return new((int)self.Width, (int)self.Height);
}
@@ -60,6 +60,18 @@ namespace SpineViewer.Extensions
return wb;
}
public static void SetWindowTextColor(this Window self, Color color)
{
var hwnd = new WindowInteropHelper(self).Handle;
Dwmapi.SetWindowTextColor(hwnd, color.R, color.G, color.B);
}
public static void SetWindowCaptionColor(this Window self, Color color)
{
var hwnd = new WindowInteropHelper(self).Handle;
Dwmapi.SetWindowCaptionColor(hwnd, color.R, color.G, color.B);
}
//public static void SaveToFile(this BitmapSource bitmap, string path)
//{
// var ext = Path.GetExtension(path)?.ToLowerInvariant();

View File

@@ -41,5 +41,6 @@
<ProjectReference Include="..\NLog.Windows.Wpf\NLog.Windows.Wpf.csproj" />
<ProjectReference Include="..\SFMLRenderer\SFMLRenderer.csproj" />
<ProjectReference Include="..\Spine\Spine.csproj" />
<ProjectReference Include="..\Win32Natives\Win32Natives.csproj" />
</ItemGroup>
</Project>

View File

@@ -5,7 +5,6 @@ using NLog;
using Spine;
using Spine.Implementations;
using SpineViewer.Models;
using SpineViewer.Natives;
using SpineViewer.Services;
using SpineViewer.Utils;
using System;

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using SpineViewer.Extensions;
using SpineViewer.Resources;
using System;
using System.Collections.Generic;
@@ -14,6 +14,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Win32Natives;
namespace SpineViewer.Views
{
@@ -30,9 +31,8 @@ namespace SpineViewer.Views
private void AboutDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
}
}

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using SpineViewer.Extensions;
using SpineViewer.Resources;
using System;
using System.Collections.Generic;
@@ -14,6 +14,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Win32Natives;
namespace SpineViewer.Views
{
@@ -30,9 +31,8 @@ namespace SpineViewer.Views
private void DiagnosticsDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
}
}

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using Win32Natives;
using SpineViewer.Resources;
using SpineViewer.Services;
using SpineViewer.ViewModels.Exporters;
@@ -16,6 +16,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SpineViewer.Extensions;
namespace SpineViewer.Views.ExporterDialogs
{
@@ -32,9 +33,8 @@ namespace SpineViewer.Views.ExporterDialogs
private void CustomFFmpegExporterDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using SpineViewer.Extensions;
using SpineViewer.Resources;
using SpineViewer.Services;
using SpineViewer.ViewModels.Exporters;
@@ -16,6 +16,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Win32Natives;
namespace SpineViewer.Views.ExporterDialogs
{
@@ -32,9 +33,8 @@ namespace SpineViewer.Views.ExporterDialogs
private void FFmpegVideoExporterDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using SpineViewer.Extensions;
using SpineViewer.Resources;
using SpineViewer.Services;
using SpineViewer.ViewModels.Exporters;
@@ -16,6 +16,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Win32Natives;
namespace SpineViewer.Views.ExporterDialogs
{
@@ -32,9 +33,8 @@ namespace SpineViewer.Views.ExporterDialogs
private void FrameExporterDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using SpineViewer.Extensions;
using SpineViewer.Resources;
using SpineViewer.Services;
using SpineViewer.ViewModels.Exporters;
@@ -16,6 +16,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Win32Natives;
namespace SpineViewer.Views.ExporterDialogs
{
@@ -32,9 +33,8 @@ namespace SpineViewer.Views.ExporterDialogs
private void FrameSequenceExporterDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)

View File

@@ -1,8 +1,8 @@
using NLog;
using SFMLRenderer;
using Spine;
using SpineViewer.Extensions;
using SpineViewer.Models;
using SpineViewer.Natives;
using SpineViewer.Resources;
using SpineViewer.Services;
using SpineViewer.Utils;
@@ -24,6 +24,7 @@ using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using Win32Natives;
namespace SpineViewer.Views;
@@ -98,13 +99,13 @@ public partial class MainWindow : Window
// Initialize Wallpaper RenderWindow
_wallpaperRenderWindow = new(new(1, 1), "SpineViewerWallpaper", SFML.Window.Styles.None);
_wallpaperRenderWindow.SetVisible(false);
_wallpaperRenderWindow.MaxFps = 30;
var handle = _wallpaperRenderWindow.SystemHandle;
var style = User32.GetWindowLong(handle, User32.GWL_STYLE) | User32.WS_POPUP;
var exStyle = User32.GetWindowLong(handle, User32.GWL_EXSTYLE) | User32.WS_EX_LAYERED | User32.WS_EX_TOOLWINDOW;
var exStyle = User32.GetWindowLong(handle, User32.GWL_EXSTYLE) | User32.WS_EX_TOOLWINDOW;
User32.SetWindowLong(handle, User32.GWL_STYLE, style);
User32.SetWindowLong(handle, User32.GWL_EXSTYLE, exStyle);
User32.SetLayeredWindowAttributes(handle, 0, byte.MaxValue, User32.LWA_ALPHA);
DataContext = _vm = new(_renderPanel, _wallpaperRenderWindow);
@@ -153,9 +154,8 @@ public partial class MainWindow : Window
private void MainWindow_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using SpineViewer.Extensions;
using SpineViewer.Resources;
using SpineViewer.Services;
using SpineViewer.ViewModels.Exporters;
@@ -16,6 +16,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Win32Natives;
namespace SpineViewer.Views
{
@@ -32,9 +33,8 @@ namespace SpineViewer.Views
private void PreferenceDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)

View File

@@ -1,4 +1,4 @@
using SpineViewer.Natives;
using SpineViewer.Extensions;
using SpineViewer.Resources;
using SpineViewer.ViewModels;
using System;
@@ -16,6 +16,7 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Win32Natives;
namespace SpineViewer.Views
{
@@ -33,9 +34,8 @@ namespace SpineViewer.Views
private void ProgressDialog_SourceInitialized(object? sender, EventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
Dwmapi.SetWindowTextColor(hwnd, AppResource.Color_PrimaryText);
Dwmapi.SetWindowCaptionColor(hwnd, AppResource.Color_Region);
this.SetWindowTextColor(AppResource.Color_PrimaryText);
this.SetWindowCaptionColor(AppResource.Color_Region);
}
private void ProgressWindow_Loaded(object sender, RoutedEventArgs e)

View File

@@ -4,9 +4,8 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace SpineViewer.Natives
namespace Win32Natives
{
/// <summary>
/// dwmapi.dll 包装类
@@ -24,15 +23,15 @@ namespace SpineViewer.Natives
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, uint dwAttribute, ref uint pvAttribute, int cbAttribute);
public static bool SetWindowCaptionColor(IntPtr hwnd, Color color)
public static bool SetWindowCaptionColor(IntPtr hwnd, byte r, byte g, byte b)
{
int c = color.R | (color.G << 8) | (color.B << 16);
int c = r | (g << 8) | (b << 16);
return 0 == DwmSetWindowAttribute(hwnd, DWMWA_CAPTION_COLOR, ref c, sizeof(uint));
}
public static bool SetWindowTextColor(IntPtr hwnd, Color color)
public static bool SetWindowTextColor(IntPtr hwnd, byte r, byte g, byte b)
{
int c = color.R | (color.G << 8) | (color.B << 16);
int c = r | (g << 8) | (b << 16);
return 0 == DwmSetWindowAttribute(hwnd, DWMWA_TEXT_COLOR, ref c, sizeof(uint));
}

View File

@@ -7,7 +7,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace SpineViewer.Natives
namespace Win32Natives
{
/// <summary>
/// gdi32.dll 包装类

View File

@@ -7,7 +7,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace SpineViewer.Natives
namespace Win32Natives
{
/// <summary>
/// shell32.dll 包装类

View File

@@ -7,7 +7,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace SpineViewer.Natives
namespace Win32Natives
{
/// <summary>
/// user32.dll 包装类

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
<PlatformTarget>x64</PlatformTarget>
<TargetFramework>net8.0-windows</TargetFramework>
<BaseOutputPath>$(SolutionDir)out</BaseOutputPath>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<Version>0.0.1</Version>
</PropertyGroup>
</Project>