啊鑫
2024-07-11 afbf8700d137710713db61955879d0f5acb73738
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#region
 
using System;
using System.Runtime.InteropServices;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    /// <summary>
    ///     监视器开关控制
    /// </summary>
    public class MonitorTool
    {
        //系统消息 
        private const uint WM_SYSCOMMAND = 0x112;
 
        //关闭显示器的系统命令   
        private const int SC_MONITORPOWER = 0xF170;
 
        //2为PowerOff, 1为省电状态,-1为开机 
        private const int MonitorPowerOff = 2;
 
        /// <summary>
        ///     关闭显示器
        /// </summary>
        public static void PowerOff(IntPtr hWnd)
        {
            SendMessage(
                hWnd,
                WM_SYSCOMMAND,
                SC_MONITORPOWER,
                2
            );
        }
 
        /// <summary>
        ///     打开显示器
        /// </summary>
        public static void PowerOn(IntPtr hWnd)
        {
            SendMessage(
                hWnd,
                WM_SYSCOMMAND,
                SC_MONITORPOWER,
                -1
            );
        }
 
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(
            IntPtr hWnd,
            uint msg,
            uint wParam,
            int lParam);
    }
}