C# 关闭/打开显示器工具+源码(一个API的简单操作)
如有转载,请注明出处:http://www.cnblogs.com/flydoos/archive/2011/08/23/2150132.html
C# 关闭/打开显示器工具+源码(一个API的简单操作)
最近这些日子,连夜总是要下载一些资料,要是看到电脑亮灯吧,又会给家里人骂,真够纠结的,更郁闷的是笔记本又不能“立刻”关闭显示器(待机一段时间才会自动关闭)~~超级无语。所以就写个小程序,来帮我关闭显示器喽,程序只有一段话,没别的。一看就懂,直接上源码:
如果这篇文章对你有用,请留个手印~~
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Runtime.InteropServices;
9
10 namespace CloseMonitor
11 {
12 public partial class Form1 : Form
13 {
14 [DllImport("user32.dll")]
15 public static extern IntPtr SendMessage(
16 IntPtr hWnd,
17 uint msg,
18 uint wParam,
19 int lParam);
20
21 private const uint WM_SYSCOMMAND = 0x0112;
22 private const uint SC_MONITORPOWER = 0xF170;
23
24 public Form1()
25 {
26 InitializeComponent();
27 }
28
29 private void Form1_Load(object sender, EventArgs e)
30 {
31 SendMessage(
32 this.Handle,
33 WM_SYSCOMMAND,
34 SC_MONITORPOWER,
35 2
36 ); //关闭显示器;
37
38 // SendMessage(
39 // this.Handle,
40 // WM_SYSCOMMAND,
41 // SC_MONITORPOWER,
42 // -1
43 // ); //打开显示器;
44 }
45 }
46 }
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Runtime.InteropServices;
9
10 namespace CloseMonitor
11 {
12 public partial class Form1 : Form
13 {
14 [DllImport("user32.dll")]
15 public static extern IntPtr SendMessage(
16 IntPtr hWnd,
17 uint msg,
18 uint wParam,
19 int lParam);
20
21 private const uint WM_SYSCOMMAND = 0x0112;
22 private const uint SC_MONITORPOWER = 0xF170;
23
24 public Form1()
25 {
26 InitializeComponent();
27 }
28
29 private void Form1_Load(object sender, EventArgs e)
30 {
31 SendMessage(
32 this.Handle,
33 WM_SYSCOMMAND,
34 SC_MONITORPOWER,
35 2
36 ); //关闭显示器;
37
38 // SendMessage(
39 // this.Handle,
40 // WM_SYSCOMMAND,
41 // SC_MONITORPOWER,
42 // -1
43 // ); //打开显示器;
44 }
45 }
46 }