This commit is contained in:
Sunny 2024-12-27 21:28:27 +08:00
parent b01e4e14e1
commit 6b5d644bd9
16 changed files with 73 additions and 72 deletions

View File

@ -1,67 +1,77 @@
using Microsoft.Win32; using System;
using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
using Microsoft.Win32;
namespace Sunny.UI namespace Sunny.UI
{ {
public static class ApplicationEx public static class ApplicationEx
{ {
private static string StartUpPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; private static readonly string StartUpPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
//获取GUID /// <summary>
/// 获取当前应用程序的 GUID
/// </summary>
/// <returns>应用程序的 GUID</returns>
public static Guid AppGuid() public static Guid AppGuid()
{ {
Assembly asm = Assembly.GetEntryAssembly(); var asm = Assembly.GetEntryAssembly();
if (asm == null) return Guid.Empty;
object[] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true)); object[] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true));
return new Guid((attr[0] as GuidAttribute).Value); return attr.Length > 0 && attr[0] is GuidAttribute ga ? new Guid(ga.Value) : Guid.Empty;
} }
public static string Folder(this Environment.SpecialFolder specialFolder) /// <summary>
{ /// 获取指定特殊文件夹的路径,并处理路径格式
return Environment.GetFolderPath(specialFolder).DealPath(); /// </summary>
} /// <param name="specialFolder">特殊文件夹枚举</param>
/// <returns>处理后的文件夹路径</returns>
public static string Folder(this Environment.SpecialFolder specialFolder) => Environment.GetFolderPath(specialFolder).DealPath();
/// <summary>
/// 获取指定特殊文件夹下的应用程序文件夹路径,并在必要时创建该文件夹
/// </summary>
/// <param name="specialFolder">特殊文件夹枚举</param>
/// <param name="createIfNotExists">如果文件夹不存在,是否创建</param>
/// <returns>应用程序文件夹路径</returns>
public static string FolderWithApplication(this Environment.SpecialFolder specialFolder, bool createIfNotExists = true) public static string FolderWithApplication(this Environment.SpecialFolder specialFolder, bool createIfNotExists = true)
{ {
string dir = (specialFolder.Folder() + Application.ProductName).DealPath(); var dir = (specialFolder.Folder() + Application.ProductName).DealPath();
if (createIfNotExists) Dir.CreateDir(dir); if (createIfNotExists) Dir.CreateDir(dir);
return dir; return dir;
} }
/// <summary> /// <summary>
/// 用作当前漫游用户的应用程序特定数据的公共储存库的目录。 漫游用户在网络上的多台计算机上工作。 漫游用户的配置文件保留在网络服务器上,并在用户登录时加载到系统中。 /// 获取当前漫游用户的应用程序特定数据的公共储存库的目录
/// C:\Users\{YourUserName}\AppData\Roaming\{Application.ProductName}\ /// C:\Users\{YourUserName}\AppData\Roaming\{Application.ProductName}\
/// </summary> /// </summary>
/// <returns></returns> /// <returns>应用程序数据文件夹路径</returns>
public static string ApplicationDataFolder() => Environment.SpecialFolder.ApplicationData.FolderWithApplication(); public static string ApplicationDataFolder() => Environment.SpecialFolder.ApplicationData.FolderWithApplication();
/// <summary> /// <summary>
/// 用作当前非漫游用户使用的应用程序特定数据的公共储存库的目录 /// 获取当前非漫游用户使用的应用程序特定数据的公共储存库的目录
/// C:\Users\{YourUserName}\AppData\Local\{Application.ProductName}\ /// C:\Users\{YourUserName}\AppData\Local\{Application.ProductName}\
/// </summary> /// </summary>
/// <returns></returns> /// <returns>本地应用程序数据文件夹路径</returns>
public static string LocalApplicationDataFolder() => Environment.SpecialFolder.LocalApplicationData.FolderWithApplication(); public static string LocalApplicationDataFolder() => Environment.SpecialFolder.LocalApplicationData.FolderWithApplication();
/// <summary> /// <summary>
/// 用作所有用户使用的应用程序特定数据的公共储存库的目录 /// 获取所有用户使用的应用程序特定数据的公共储存库的目录
/// C:\ProgramData \{Application.ProductName}\ /// C:\ProgramData \{Application.ProductName}\
/// </summary> /// </summary>
/// <returns></returns> /// <returns>公共应用程序数据文件夹路径</returns>
public static string CommonApplicationDataFolder() => Environment.SpecialFolder.CommonApplicationData.FolderWithApplication(); public static string CommonApplicationDataFolder() => Environment.SpecialFolder.CommonApplicationData.FolderWithApplication();
/// <summary> /// <summary>
/// 增加当前程序到开机自动运行 /// 增加当前程序到开机自动运行
/// </summary> /// </summary>
/// <param name="arguments"></param> /// <param name="arguments">启动参数</param>
public static void AddToStartup(string arguments) public static void AddToStartup(string arguments)
{ {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true)) using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true);
{ key?.SetValue(Application.ProductName, $"\"{Application.ExecutablePath}\" {arguments}");
key.SetValue(Application.ProductName, "\"" + Application.ExecutablePath + "\" " + arguments);
}
} }
/// <summary> /// <summary>
@ -69,10 +79,8 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static void AddToStartup() public static void AddToStartup()
{ {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true)) using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true);
{ key?.SetValue(Application.ProductName, $"\"{Application.ExecutablePath}\"");
key.SetValue(Application.ProductName, "\"" + Application.ExecutablePath + "\"");
}
} }
/// <summary> /// <summary>
@ -80,22 +88,18 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static void RemoveFromStartup() public static void RemoveFromStartup()
{ {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true)) using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true);
{ if (Application.ProductName != null) key?.DeleteValue(Application.ProductName, false);
key.DeleteValue(Application.ProductName, false);
}
} }
/// <summary> /// <summary>
/// 判断当前程序是否开机自动运行 /// 判断当前程序是否开机自动运行
/// </summary> /// </summary>
/// <returns></returns> /// <returns>是否开机自动运行</returns>
public static bool StartupEnabled() public static bool StartupEnabled()
{ {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, false)) using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, false);
{ return key != null && key.GetValue(Application.ProductName) != null;
return key.GetValue(Application.ProductName) != null;
}
} }
/// <summary> /// <summary>
@ -103,30 +107,27 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static void CheckAndUpdateStartupPath() public static void CheckAndUpdateStartupPath()
{ {
if (StartupEnabled()) if (!StartupEnabled()) return;
string arg = string.Empty;
// 从注册表键值中读取启动参数
using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true);
if (key == null) return;
string oldValue = key.GetValue(Application.ProductName)?.ToString();
if (oldValue == null) return;
if (oldValue.StartsWith("\""))
{ {
string oldValue; arg = string.Join("\"", oldValue.Split('\"').Skip(2)).Trim();
string arg = string.Empty;
//Read Argument From Registry Key Value
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true))
{
oldValue = key.GetValue(Application.ProductName).ToString();
if (oldValue.StartsWith("\""))
{
arg = string.Join("\"", oldValue.Split('\"').Skip(2)).Trim();
}
else if (oldValue.Contains(" "))
{
arg = string.Join(" ", oldValue.Split(' ').Skip(1));
}
}
if (string.IsNullOrEmpty(arg))
AddToStartup();
else
AddToStartup(arg);
} }
else if (oldValue.Contains(" "))
{
arg = string.Join(" ", oldValue.Split(' ').Skip(1));
}
if (string.IsNullOrEmpty(arg))
AddToStartup();
else
AddToStartup(arg);
} }
} }
} }

View File

@ -77,7 +77,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
/// <summary> /// <summary>
/// 颜色改变事件 /// 颜色改变事件

View File

@ -100,7 +100,7 @@ namespace Sunny.UI
public event EventHandler SelectionChangeCommitted; public event EventHandler SelectionChangeCommitted;
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
[DefaultValue(0), Category("SunnyUI"), Description("垂直滚动条宽度,最小为原生滚动条宽度")] [DefaultValue(0), Category("SunnyUI"), Description("垂直滚动条宽度,最小为原生滚动条宽度")]
public int ScrollBarWidth public int ScrollBarWidth

View File

@ -82,7 +82,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
[DefaultValue(true), Description("过滤框输入逐一过滤"), Category("SunnyUI")] [DefaultValue(true), Description("过滤框输入逐一过滤"), Category("SunnyUI")]
public bool Filter1by1 { get; set; } = true; public bool Filter1by1 { get; set; } = true;

View File

@ -86,7 +86,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
[DefaultValue(false)] [DefaultValue(false)]
[Description("显示清除按钮"), Category("SunnyUI")] [Description("显示清除按钮"), Category("SunnyUI")]

View File

@ -56,7 +56,7 @@ namespace Sunny.UI
[Browsable(false)] [Browsable(false)]
[Description("控件在界面显示时需要多语翻译的属性名称数组"), Category("SunnyUI")] [Description("控件在界面显示时需要多语翻译的属性名称数组"), Category("SunnyUI")]
public virtual string[] FormTranslatorProperties { get; } public virtual string[] FormTranslatorProperties => null;
[DefaultValue(true)] [DefaultValue(true)]
[Description("控件在界面显示时需要多语翻译"), Category("SunnyUI")] [Description("控件在界面显示时需要多语翻译"), Category("SunnyUI")]

View File

@ -66,7 +66,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
/// <summary> /// <summary>
/// 需要额外设置ToolTip的控件 /// 需要额外设置ToolTip的控件

View File

@ -74,7 +74,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
public void Render() public void Render()
{ {

View File

@ -63,7 +63,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
public event EventHandler ValueChanged; public event EventHandler ValueChanged;

View File

@ -80,7 +80,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
public override void SetDPIScale() public override void SetDPIScale()
{ {

View File

@ -63,7 +63,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
/// <summary> /// <summary>
/// 需要额外设置ToolTip的控件 /// 需要额外设置ToolTip的控件

View File

@ -98,7 +98,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
public override void SetDPIScale() public override void SetDPIScale()
{ {

View File

@ -45,7 +45,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
public delegate void OnValueChanged(object sender, string value); public delegate void OnValueChanged(object sender, string value);
public event OnValueChanged ValueChanged; public event OnValueChanged ValueChanged;

View File

@ -87,7 +87,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {

View File

@ -152,7 +152,7 @@ namespace Sunny.UI
} }
[Browsable(false)] [Browsable(false)]
public override string[] FormTranslatorProperties { get; } public override string[] FormTranslatorProperties => null;
private void Edit_FontChanged(object sender, EventArgs e) private void Edit_FontChanged(object sender, EventArgs e)
{ {

View File

@ -56,7 +56,7 @@ namespace Sunny.UI
[Browsable(false)] [Browsable(false)]
[Description("控件在界面显示时需要多语翻译的属性名称数组"), Category("SunnyUI")] [Description("控件在界面显示时需要多语翻译的属性名称数组"), Category("SunnyUI")]
public virtual string[] FormTranslatorProperties { get; } public virtual string[] FormTranslatorProperties => null;
[DefaultValue(true)] [DefaultValue(true)]
[Description("控件在界面显示时需要多语翻译"), Category("SunnyUI")] [Description("控件在界面显示时需要多语翻译"), Category("SunnyUI")]