Browse Source

屏幕常亮

liuyuqi-dellpc 1 year ago
parent
commit
38f47d1f75

+ 8 - 0
DayOf1440/DayOf1440.csproj

@@ -32,6 +32,9 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
+  <PropertyGroup>
+    <ApplicationIcon>logo.ico</ApplicationIcon>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Core" />
@@ -54,6 +57,7 @@
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Utils\SystemSleep.cs" />
     <EmbeddedResource Include="MainForm.resx">
       <DependentUpon>MainForm.cs</DependentUpon>
     </EmbeddedResource>
@@ -113,5 +117,9 @@
   <ItemGroup>
     <None Include="Resources\background %282%29.png" />
   </ItemGroup>
+  <ItemGroup>
+    <Content Include="logo.ico" />
+    <None Include="Resources\logo.ico" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 1 - 0
DayOf1440/MainForm.Designer.cs

@@ -116,6 +116,7 @@ namespace DayOf1440
             this.Name = "MainForm";
             this.Text = "Form1";
             this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
+            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed);
             this.Load += new System.EventHandler(this.MainForm_Load);
             this.DoubleClick += new System.EventHandler(this.MainForm_DoubleClick);
             this.panelNum.ResumeLayout(false);

+ 10 - 1
DayOf1440/MainForm.cs

@@ -1,4 +1,5 @@
-using System;
+using DayOf1440.Utils;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
@@ -22,6 +23,8 @@ namespace DayOf1440
         {
             loadChar();
             setPanelToCenter();
+            // 让屏幕常亮
+            SystemSleep.PreventForCurrentThread();
             timer1.Start();
         }
 
@@ -95,5 +98,11 @@ namespace DayOf1440
         private void  setPanelToCenter() {
             panelNum.Location = new Point((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width-596)/2,(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height-202)/2);
         }
+
+        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
+        {
+            // 取消常亮
+            SystemSleep.RestoreForCurrentThread();
+        }
     }
 }

+ 10 - 0
DayOf1440/Properties/Resources.Designer.cs

@@ -169,5 +169,15 @@ namespace DayOf1440.Properties {
                 return ((System.Drawing.Bitmap)(obj));
             }
         }
+        
+        /// <summary>
+        ///   Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
+        /// </summary>
+        internal static System.Drawing.Icon logo {
+            get {
+                object obj = ResourceManager.GetObject("logo", resourceCulture);
+                return ((System.Drawing.Icon)(obj));
+            }
+        }
     }
 }

+ 3 - 0
DayOf1440/Properties/Resources.resx

@@ -121,6 +121,9 @@
   <data name="background__2_" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\background (2).png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
   </data>
+  <data name="logo" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>..\Resources\logo.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
+  </data>
   <data name="_0" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\0 .png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
   </data>

BIN
DayOf1440/Resources/logo.ico


+ 84 - 0
DayOf1440/Utils/SystemSleep.cs

@@ -0,0 +1,84 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DayOf1440.Utils
+{
+   public static class SystemSleep
+    {
+        [DllImport("kernel32")]
+        private static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);
+        [Flags]
+        private enum ExecutionState : uint
+        {
+            /// <summary>
+            /// Forces the system to be in the working state by resetting the system idle timer.
+            /// </summary>
+            SystemRequired = 0x01,
+
+            /// <summary>
+            /// Forces the display to be on by resetting the display idle timer.
+            /// </summary>
+            DisplayRequired = 0x02,
+
+            /// <summary>
+            /// This value is not supported. If <see cref="UserPresent"/> is combined with other esFlags values, the call will fail and none of the specified states will be set.
+            /// </summary>
+            [Obsolete("This value is not supported.")]
+            UserPresent = 0x04,
+
+            /// <summary>
+            /// Enables away mode. This value must be specified with <see cref="Continuous"/>.
+            /// <para />
+            /// Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping.
+            /// </summary>
+            AwaymodeRequired = 0x40,
+
+            /// <summary>
+            /// Informs the system that the state being set should remain in effect until the next call that uses <see cref="Continuous"/> and one of the other state flags is cleared.
+            /// </summary>
+            Continuous = 0x80000000,
+        }
+        /// <summary>
+        /// 设置此线程此时开始一直将处于运行状态,此时计算机不应该进入睡眠状态。
+        /// 此线程退出后,设置将失效。
+        /// 如果需要恢复,请调用 <see cref="RestoreForCurrentThread"/> 方法。
+        /// </summary>
+        /// <param name="keepDisplayOn">
+        /// 表示是否应该同时保持屏幕不关闭。
+        /// 对于游戏、视频和演示相关的任务需要保持屏幕不关闭;而对于后台服务、下载和监控等任务则不需要。
+        /// </param>
+        public static void PreventForCurrentThread(bool keepDisplayOn = true)
+        {
+            SetThreadExecutionState(keepDisplayOn
+                ? ExecutionState.Continuous | ExecutionState.SystemRequired | ExecutionState.DisplayRequired
+                : ExecutionState.Continuous | ExecutionState.SystemRequired);
+        }
+
+        /// <summary>
+        /// 恢复此线程的运行状态,操作系统现在可以正常进入睡眠状态和关闭屏幕。
+        /// </summary>
+        public static void RestoreForCurrentThread()
+        {
+            SetThreadExecutionState(ExecutionState.Continuous);
+        }
+
+        /// <summary>
+        /// 重置系统睡眠或者关闭屏幕的计时器,这样系统睡眠或者屏幕能够继续持续工作设定的超时时间。
+        /// </summary>
+        /// <param name="keepDisplayOn">
+        /// 表示是否应该同时保持屏幕不关闭。
+        /// 对于游戏、视频和演示相关的任务需要保持屏幕不关闭;而对于后台服务、下载和监控等任务则不需要。
+        /// </param>
+        public static void ResetIdle(bool keepDisplayOn = true)
+        {
+            SetThreadExecutionState(keepDisplayOn
+                ? ExecutionState.SystemRequired | ExecutionState.DisplayRequired
+                : ExecutionState.SystemRequired);
+        }
+    }
+   
+}

BIN
DayOf1440/logo.ico