diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..b38bf0b --- /dev/null +++ b/Readme.md @@ -0,0 +1,33 @@ +# WinResSelector + +一款简洁优雅的 Windows 分辨率快速切换工具。 + +A sleek Windows resolution switcher utility. + +## ✨ 特性 | Features + +- 🎯 快速切换显示器分辨率 | Quick resolution switching +- 💾 保存常用分辨率配置 | Save frequently used resolution profiles +- 🔄 系统托盘快捷切换 | Quick access from system tray +- 🎨 现代化界面设计 | Modern UI design +- 🚀 轻量级且高效 | Lightweight and efficient + +## 🖼️ 预览 | Preview + +![主窗口截图](image.png) + +![托盘图标截图](tray.png) + +## 🚀 开始使用 | Getting Started + +1. 从 [Releases](https://github.com/fiko/WinResSelector/releases) 下载最新版本 +2. 运行程序,添加您常用的分辨率配置 +3. 可选择最小化到系统托盘,随时快速切换 + +Download the latest release, run the program, and add your frequently used resolution profiles. Optionally minimize to system tray for quick access. + +## 📄 开源协议 | License + +本项目基于 MIT 协议开源 - 查看 [LICENSE](LICENSE) 了解更多详情。 + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. \ No newline at end of file diff --git a/WinResSelector/App.xaml.cs b/WinResSelector/App.xaml.cs index e7bd9cc..7223079 100644 --- a/WinResSelector/App.xaml.cs +++ b/WinResSelector/App.xaml.cs @@ -9,12 +9,7 @@ namespace WinResSelector { public partial class App : Application { - public IServiceProvider Services { get; } - - public App() - { - Services = ConfigureServices(); - } + public IServiceProvider Services { get; } = ConfigureServices(); private static IServiceProvider ConfigureServices() { diff --git a/WinResSelector/Models/DisplayProfile.cs b/WinResSelector/Models/DisplayProfile.cs index caf8482..b85f564 100644 --- a/WinResSelector/Models/DisplayProfile.cs +++ b/WinResSelector/Models/DisplayProfile.cs @@ -23,7 +23,6 @@ public override string ToString() public class AppSettings { - public bool StartWithWindows { get; init; } public bool MinimizeToTray { get; init; } } } \ No newline at end of file diff --git a/WinResSelector/Services/ConfigService.cs b/WinResSelector/Services/ConfigService.cs index 97fa0d8..0601f76 100644 --- a/WinResSelector/Services/ConfigService.cs +++ b/WinResSelector/Services/ConfigService.cs @@ -14,7 +14,7 @@ public class ConfigService private class Config { - public List Profiles { get; set; } = new(); + public List Profiles { get; set; } = new(); public AppSettings Settings { get; set; } = new(); } @@ -47,9 +47,9 @@ public void LoadConfig() _isDirty = false; } - public List GetProfiles() + public List GetProfiles() { - return _config?.Profiles ?? new List(); + return _config?.Profiles ?? new List(); } public AppSettings GetSettings() @@ -57,7 +57,7 @@ public AppSettings GetSettings() return _config?.Settings ?? new AppSettings(); } - public void SaveProfiles(List profiles) + public void SaveProfiles(List profiles) { if (_config == null) _config = new Config(); _config.Profiles = profiles; diff --git a/WinResSelector/View/MainWindow.xaml b/WinResSelector/View/MainWindow.xaml index a9762b3..4ea960f 100644 --- a/WinResSelector/View/MainWindow.xaml +++ b/WinResSelector/View/MainWindow.xaml @@ -183,9 +183,6 @@ Foreground="#B9BBBE" FontSize="12" Margin="0,0,0,8"/> - diff --git a/WinResSelector/ViewModels/MainViewModel.cs b/WinResSelector/ViewModels/MainViewModel.cs index ee2c9e3..b25183d 100644 --- a/WinResSelector/ViewModels/MainViewModel.cs +++ b/WinResSelector/ViewModels/MainViewModel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; using System.Windows.Media; @@ -25,14 +26,11 @@ public partial class MainViewModel : ObservableObject [ObservableProperty] private Brush _statusMessageColor = Brushes.Black; - - [ObservableProperty] - private bool _startWithWindows; - + [ObservableProperty] private bool _minimizeToTray; - public ObservableCollection Profiles { get; } + public ObservableCollection Profiles { get; } public ObservableCollection AvailableResolutions { get; } public MainViewModel(ConfigService configService, DisplayService displayService, @@ -43,7 +41,7 @@ public MainViewModel(ConfigService configService, DisplayService displayService, _showWindow = showWindow; _closeWindow = closeWindow; - Profiles = new ObservableCollection(); + Profiles = new ObservableCollection(); AvailableResolutions = new ObservableCollection(); LoadData(); @@ -74,14 +72,13 @@ private void SaveSettings() { _configService.SaveSettings(new Models.AppSettings { - StartWithWindows = StartWithWindows, MinimizeToTray = MinimizeToTray }); } private void SaveProfiles() { - _configService.SaveProfiles(new System.Collections.Generic.List(Profiles)); + _configService.SaveProfiles(new List(Profiles)); } [RelayCommand] @@ -96,16 +93,14 @@ private void AddProfile() } [RelayCommand] - private void DeleteProfile(DisplayProfile profile) + private void DeleteProfile(DisplayProfile? profile) { - if (profile != null) + if (profile == null) return; + Profiles.Remove(profile); + // 重新排序 ID + for (var i = 0; i < Profiles.Count; i++) { - Profiles.Remove(profile); - // 重新排序 ID - for (int i = 0; i < Profiles.Count; i++) - { - Profiles[i].Id = i + 1; - } + Profiles[i]!.Id = i + 1; } } @@ -135,16 +130,14 @@ private void Exit() private void ApplyProfile(DisplayProfile profile) { - if (profile?.Display != null) + if (profile?.Display == null) return; + var success = _displayService.ChangeResolution(profile.Display); + if (!success) { - bool success = _displayService.ChangeResolution(profile.Display); - if (!success) - { - StatusMessage = "分辨率切换失败"; - StatusMessageColor = Brushes.Red; - } - UpdateCurrentResolution(); + StatusMessage = "分辨率切换失败"; + StatusMessageColor = Brushes.Red; } + UpdateCurrentResolution(); } private void LoadData() @@ -160,13 +153,13 @@ private void LoadData() // 再加载配置 _configService.LoadConfig(); var settings = _configService.GetSettings(); - StartWithWindows = settings.StartWithWindows; MinimizeToTray = settings.MinimizeToTray; var profiles = _configService.GetProfiles(); Profiles.Clear(); foreach (var profile in profiles) { + if (profile == null) continue; // 查找匹配的分辨率 var matchingResolution = AvailableResolutions.FirstOrDefault(r => r.Width == profile.Display.Width && diff --git a/image.png b/image.png new file mode 100644 index 0000000..7dcf2c4 Binary files /dev/null and b/image.png differ diff --git a/tray.png b/tray.png new file mode 100644 index 0000000..1ab7a7d Binary files /dev/null and b/tray.png differ