From 279d01551f0414d6cedd2c29325b73f2f286b9d6 Mon Sep 17 00:00:00 2001 From: "T.Miyake" Date: Mon, 2 Oct 2017 12:43:45 +0900 Subject: [PATCH] The speed of the selected fan is automatically restored when the application is restarted. --- PocketFanController/AboutWindow.xaml | 2 +- PocketFanController/App.xaml.cs | 3 + PocketFanController/Model.cs | 64 ++++++++++++++++--- PocketFanController/NotifyIconWrapper.cs | 3 + .../Properties/AssemblyInfo.cs | 4 +- 5 files changed, 63 insertions(+), 13 deletions(-) diff --git a/PocketFanController/AboutWindow.xaml b/PocketFanController/AboutWindow.xaml index dee955a..6b573e8 100644 --- a/PocketFanController/AboutWindow.xaml +++ b/PocketFanController/AboutWindow.xaml @@ -7,7 +7,7 @@ Title="About | Pocket Fan Controller" Height="300" Width="300" ScrollViewer.VerticalScrollBarVisibility="Disabled" ShowInTaskbar="False" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"> Pocket Fan Controller - Version 0.5.2 + Version 0.5.3 © 2017 Takafumi Miyake Third-Party Software Usage and Licenses diff --git a/PocketFanController/App.xaml.cs b/PocketFanController/App.xaml.cs index b59a1c5..36dd45d 100644 --- a/PocketFanController/App.xaml.cs +++ b/PocketFanController/App.xaml.cs @@ -17,6 +17,9 @@ protected override void OnStartup(StartupEventArgs e) } protected override void OnExit(ExitEventArgs e) { + //現在の状態を保存する。(再起動時に復元できるように) + Model.Instance.SaveLastState(); + //アプリケーション終了時にデフォルト設定に戻す。(安全のため) Model.Instance.SetDefault(); diff --git a/PocketFanController/Model.cs b/PocketFanController/Model.cs index c5b2899..3257941 100644 --- a/PocketFanController/Model.cs +++ b/PocketFanController/Model.cs @@ -12,6 +12,7 @@ public sealed class Model { // Singleton instance. public static Model Instance { get; } = new Model(); + private Model(){} public int CurrentState { get; set; } = 0; @@ -23,10 +24,18 @@ private Model(){} public void GetManualConfigs() { - ManualMargin = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin") == 0 ? 5 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin"); - ManualT0 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0") == 0 ? 40 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0"); - ManualT1 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1") == 0 ? 60 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1"); - ManualT2 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2") == 0 ? 75 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2"); + ManualMargin = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin") == 0 + ? 5 + : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin"); + ManualT0 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0") == 0 + ? 40 + : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0"); + ManualT1 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1") == 0 + ? 60 + : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1"); + ManualT2 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2") == 0 + ? 75 + : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2"); } public void GetCurrentStatus() @@ -36,7 +45,7 @@ public void GetCurrentStatus() var state2 = new[] {99, 10, 99}; var state3 = new[] {10, 99, 99}; var state4 = new[] {99, 99, 85}; - var states = new[] {state0, state1, state2, state3,state4}; + var states = new[] {state0, state1, state2, state3, state4}; var t0 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0"); var t1 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1"); @@ -106,10 +115,14 @@ public void SetSlowest() public void SetManual() { - WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin")); - WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0")); - WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1")); - WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2")); + WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", + ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin")); + WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", + ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0")); + WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", + ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1")); + WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", + ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2")); RestartService(); } @@ -121,7 +134,38 @@ public void SaveManualConfig(int margin, int t0, int t1, int t2) WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2", t2); } - public void WriteReg(string subKey, string keyName, int value) + public void SaveLastState() => WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "laststate",CurrentState); + + public void LoadAndResetLastState() + { + var lastState = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "laststate"); + switch (lastState) + { + case 0: + SetDefault(); + break; + case 1: + SetFastest(); + break; + case 2: + SetFast(); + break; + case 3: + SetSlow(); + break; + case 4: + SetSlowest(); + break; + case 5: + SetManual(); + break; + default: + SetDefault(); + break; + } + } + + public void WriteReg(string subKey, string keyName, int value) { var key = Registry.LocalMachine.CreateSubKey(subKey); key?.SetValue(keyName, value, RegistryValueKind.DWord); diff --git a/PocketFanController/NotifyIconWrapper.cs b/PocketFanController/NotifyIconWrapper.cs index 0af5e01..93239a9 100644 --- a/PocketFanController/NotifyIconWrapper.cs +++ b/PocketFanController/NotifyIconWrapper.cs @@ -27,6 +27,9 @@ public NotifyIconWrapper() //ちょっと無理やりだけど、アイコンをクリックするたびにステータスを更新する notifyIcon1.Click += UpdateCurrentStatus; + //前回終了時の状態を復元する。 + Model.LoadAndResetLastState(); + UpdateCurrentStatus(null,null); } diff --git a/PocketFanController/Properties/AssemblyInfo.cs b/PocketFanController/Properties/AssemblyInfo.cs index 2289fcc..ec3433d 100644 --- a/PocketFanController/Properties/AssemblyInfo.cs +++ b/PocketFanController/Properties/AssemblyInfo.cs @@ -51,7 +51,7 @@ // すべての値を指定するか、次を使用してビルド番号とリビジョン番号を既定に設定できます // 既定値にすることができます: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.5.2.0")] -[assembly: AssemblyFileVersion("0.5.2.0")] +[assembly: AssemblyVersion("0.5.3.0")] +[assembly: AssemblyFileVersion("0.5.3.0")] [assembly: NeutralResourcesLanguage("en")]