diff --git a/src/Snap.Hutao/Snap.Hutao/App.xaml.cs b/src/Snap.Hutao/Snap.Hutao/App.xaml.cs index 2910fed9..bb79f0d1 100644 --- a/src/Snap.Hutao/Snap.Hutao/App.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/App.xaml.cs @@ -74,10 +74,10 @@ public sealed partial class App : Application private void LogDiagnosticInformation() { - RuntimeOptions hutaoOptions = serviceProvider.GetRequiredService(); + RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService(); - logger.LogInformation("FamilyName: {name}", hutaoOptions.FamilyName); - logger.LogInformation("Version: {version}", hutaoOptions.Version); - logger.LogInformation("LocalCache: {folder}", hutaoOptions.LocalCache); + logger.LogInformation("FamilyName: {name}", runtimeOptions.FamilyName); + logger.LogInformation("Version: {version}", runtimeOptions.Version); + logger.LogInformation("LocalCache: {folder}", runtimeOptions.LocalCache); } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/IShellLinkInterop.cs b/src/Snap.Hutao/Snap.Hutao/Core/Shell/IShellLinkInterop.cs new file mode 100644 index 00000000..ed21b6c6 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/Shell/IShellLinkInterop.cs @@ -0,0 +1,9 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +namespace Snap.Hutao.Core.Shell; + +internal interface IShellLinkInterop +{ + void CreateDesktopShoutcutForElevatedLaunch(); +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs new file mode 100644 index 00000000..f5e6d5e1 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs @@ -0,0 +1,35 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using System.IO; +using Windows.Win32; +using Windows.Win32.System.Com; +using Windows.Win32.UI.Shell; +using Windows.Win32.UI.WindowsAndMessaging; + +namespace Snap.Hutao.Core.Shell; + +[ConstructorGenerated] +[Injection(InjectAs.Transient, typeof(IShellLinkInterop))] +internal sealed partial class ShellLinkInterop : IShellLinkInterop +{ + private readonly RuntimeOptions runtimeOptions; + + public void CreateDesktopShoutcutForElevatedLaunch() + { + IShellLinkW shellLink = (IShellLinkW)new ShellLink(); + shellLink.SetPath("powershell"); + shellLink.SetArguments($""" + -Command "Start-Process shell:AppsFolder\{runtimeOptions.FamilyName}!App -verb runas" + """); + shellLink.SetShowCmd(SHOW_WINDOW_CMD.SW_SHOWMINNOACTIVE); + + string iconPath = Path.Combine(runtimeOptions.InstalledLocation, "Snap.Hutao.exe"); + shellLink.SetIconLocation(iconPath, 0); + string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + string target = Path.Combine(desktop, $"{SH.AppNameAndVersion.Format(runtimeOptions.Version)}.lnk"); + + IPersistFile persistFile = (IPersistFile)shellLink; + persistFile.Save(target, false); + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/NativeMethods.txt b/src/Snap.Hutao/Snap.Hutao/NativeMethods.txt index 75d7a42c..ff73b855 100644 --- a/src/Snap.Hutao/Snap.Hutao/NativeMethods.txt +++ b/src/Snap.Hutao/Snap.Hutao/NativeMethods.txt @@ -41,6 +41,12 @@ GetWindowThreadProcessId ReleaseDC SetForegroundWindow +// COM +IPersistFile +IShellLinkW +ShellLink +SHELL_LINK_DATA_FLAGS + // WinRT IMemoryBufferByteAccess @@ -60,6 +66,4 @@ LPTHREAD_START_ROUTINE MINMAXINFO // System.Com -CWMO_FLAGS - -//IShellLinkW \ No newline at end of file +CWMO_FLAGS \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs index f337db17..96013c45 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs @@ -3354,6 +3354,15 @@ namespace Snap.Hutao.Resource.Localization { } } + /// + /// 查找类似 操作完成 的本地化字符串。 + /// + internal static string ViewModelSettingActionComplete { + get { + return ResourceManager.GetString("ViewModelSettingActionComplete", resourceCulture); + } + } + /// /// 查找类似 清除失败,文件目录权限不足,请使用管理员模式重试 的本地化字符串。 /// @@ -5109,6 +5118,33 @@ namespace Snap.Hutao.Resource.Localization { } } + /// + /// 查找类似 创建 的本地化字符串。 + /// + internal static string ViewPageSettingCreateDesktopShortcutAction { + get { + return ResourceManager.GetString("ViewPageSettingCreateDesktopShortcutAction", resourceCulture); + } + } + + /// + /// 查找类似 在桌面上创建默认以管理员方式启动的快捷方式,更新后需要重新创建 的本地化字符串。 + /// + internal static string ViewPageSettingCreateDesktopShortcutDescription { + get { + return ResourceManager.GetString("ViewPageSettingCreateDesktopShortcutDescription", resourceCulture); + } + } + + /// + /// 查找类似 创建快捷方式 的本地化字符串。 + /// + internal static string ViewPageSettingCreateDesktopShortcutHeader { + get { + return ResourceManager.GetString("ViewPageSettingCreateDesktopShortcutHeader", resourceCulture); + } + } + /// /// 查找类似 执行 的本地化字符串。 /// diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 37269eaa..30339b4e 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -1271,6 +1271,9 @@ 切换账号失败 + + 操作完成 + 清除失败,文件目录权限不足,请使用管理员模式重试 @@ -1856,6 +1859,15 @@ 复制 + + 创建 + + + 在桌面上创建默认以管理员方式启动的快捷方式,更新后需要重新创建 + + + 创建快捷方式 + 执行 diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs index 068d1f53..b51db023 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs @@ -2,12 +2,10 @@ // Licensed under the MIT license. using Microsoft.Extensions.Primitives; -using Snap.Hutao.Core.ExceptionService; using Snap.Hutao.Core.Windowing; using Snap.Hutao.Model; using Snap.Hutao.Model.Entity; using Snap.Hutao.Service.Abstraction; -using Snap.Hutao.Service.Game; using System.Globalization; using System.IO; diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml index fd19b8b6..c525e592 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml @@ -91,6 +91,13 @@ Description="{Binding HutaoOptions.WebView2Version}" Header="{shcm:ResourceString Name=ViewPageSettingWebview2Header}" HeaderIcon="{shcm:FontIcon Glyph=}"/> + ? selectedBackdropType; private NameValue? selectedCulture; @@ -254,4 +256,11 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel infoBarService.Success("无感验证复合 Url 配置成功"); } } + + [Command("CreateDesktopShortcutCommand")] + private void CreateDesktopShortcutForElevatedLaunch() + { + shellLinkInterop.CreateDesktopShoutcutForElevatedLaunch(); + infoBarService.Information(SH.ViewModelSettingActionComplete); + } } \ No newline at end of file