liuyuqi-dellpc 11 months ago
parent
commit
52662b9748

+ 4 - 1
KeepScreenLight/KeepScreenLight.csproj

@@ -60,6 +60,10 @@
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Utils\SystemSleep.cs" />
+    <EmbeddedResource Include="MainForm.resx">
+      <DependentUpon>MainForm.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="Pages\BaseForm.resx">
       <DependentUpon>BaseForm.cs</DependentUpon>
     </EmbeddedResource>
@@ -87,7 +91,6 @@
   </ItemGroup>
   <ItemGroup>
     <Folder Include="NativeMethods\" />
-    <Folder Include="Utils\" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 37 - 2
KeepScreenLight/MainForm.Designer.cs

@@ -29,13 +29,48 @@ namespace KeepScreenLight
         /// </summary>
         private void InitializeComponent()
         {
-            this.components = new System.ComponentModel.Container();
+            this.button1 = new System.Windows.Forms.Button();
+            this.label1 = new System.Windows.Forms.Label();
+            this.SuspendLayout();
+            // 
+            // button1
+            // 
+            this.button1.Font = new System.Drawing.Font("SimSun", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.button1.Location = new System.Drawing.Point(291, 273);
+            this.button1.Name = "button1";
+            this.button1.Size = new System.Drawing.Size(234, 66);
+            this.button1.TabIndex = 0;
+            this.button1.Text = "Start";
+            this.button1.UseVisualStyleBackColor = true;
+            this.button1.Click += new System.EventHandler(this.button1_Click);
+            // 
+            // label1
+            // 
+            this.label1.AutoSize = true;
+            this.label1.Location = new System.Drawing.Point(289, 155);
+            this.label1.Name = "label1";
+            this.label1.Size = new System.Drawing.Size(191, 12);
+            this.label1.TabIndex = 1;
+            this.label1.Text = "保持屏幕常亮 Keep screen light.";
+            // 
+            // MainForm
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(800, 450);
-            this.Text = "Form1";
+            this.Controls.Add(this.label1);
+            this.Controls.Add(this.button1);
+            this.Name = "MainForm";
+            this.Text = "KeepScreenLight";
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
         }
 
         #endregion
+
+        private System.Windows.Forms.Button button1;
+        private System.Windows.Forms.Label label1;
     }
 }
 

+ 16 - 1
KeepScreenLight/MainForm.cs

@@ -1,4 +1,5 @@
-using System;
+using KeepScreenLight.Utils;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
@@ -12,9 +13,23 @@ namespace KeepScreenLight
 {
     public partial class MainForm : Form
     {
+        bool flag = false;
         public MainForm()
         {
             InitializeComponent();
         }
+
+        private void button1_Click(object sender, EventArgs e)
+        {
+            if (flag)
+            {
+                // 让屏幕常亮
+                SystemSleep.PreventForCurrentThread();
+            }
+            else
+            {
+                SystemSleep.RestoreForCurrentThread();
+            }
+        }
     }
 }

+ 120 - 0
KeepScreenLight/MainForm.resx

@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+</root>

+ 88 - 0
KeepScreenLight/Utils/SystemSleep.cs

@@ -0,0 +1,88 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace KeepScreenLight.Utils
+{
+    /// <summary>
+    /// 管理屏幕休眠工具类
+    /// </summary>
+   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);
+        }
+    }
+   
+}
+