C定时关闭监视器(显示器).docVIP

  • 3
  • 0
  • 约1.97千字
  • 约 3页
  • 2017-06-08 发布于重庆
  • 举报
C定时关闭监视器(显示器)

C#关闭笔记本监视器(或电脑显示器)源码 这个功能对笔记本非常有用 windows为我们提供了一个API。可以实现即时关闭监视器。 ?Code?[] LRESULT WINAPI SendMessage __in HWND hWnd, __in UINT Msg, __in WPARAM wParam, __in LPARAM lParam ; 详见:/en-us/library/ms644950 VS.85 .aspx 此API在平台调用中,如下签名: ?Code?[] //C# Signature: [DllImport user32.dll, EntryPoint SendMessage CharSet CharSet.Auto ] static extern int IntPtr SendMessage IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam ; 此方法的各个参数类型有一些重载,一般还是建议用IntPtr,否则,在64位平台OR其它情况下可能会崩溃~ 我们需要传给Msg 一个系统消息,即:WM_SYSCOMMAND 然后是附加信息,比如屏幕保护:SC_SCREENSAVE,监视器电源:SC_MONITORPOWER WM_SYSCOMMAND有如下取值: ?Code?[] enum SysCommands : int SC_SIZE 0xF000, SC_MOVE 0xF010, SC_MINIMIZE 0xF020, SC_MAXIMIZE 0xF030, SC_NEXTWINDOW 0xF040, SC_PREVWINDOW 0xF050, SC_CLOSE 0xF060, SC_VSCROLL 0xF070, SC_HSCROLL 0xF080, SC_MOUSEMENU 0xF090, SC_KEYMENU 0xF100, SC_ARRANGE 0xF110, SC_RESTORE 0xF120, SC_TASKLIST 0xF130, SC_SCREENSAVE 0xF140, SC_HOTKEY 0xF150, //#if WINVER 0x0400 //Win95 SC_DEFAULT 0xF160, SC_MONITORPOWER 0xF170, SC_CONTEXTHELP 0xF180, SC_SEPARATOR 0xF00F, //#endif /* WINVER 0x0400 */ //#if WINVER 0x0600 //Vista SCF_ISSECURE 0 //#endif /* WINVER 0x0600 */ /* * Obsolete names */ SC_ICON SC_MINIMIZE, SC_ZOOM SC_MAXIMIZE, 以上解释调用 先在窗体新建一个按钮button1,然后在窗体双击打开CS页的代码的如下: ?Code?[] using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace CloseMonitor public partial class Form1 : Form public Form1 InitializeComponent ; [DllImport user32.dll, EntryPoint SendMessage, CharSet CharSet.Auto ] private static extern int SendMessage IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr iParam ; private void button1_Click object sender, EventArgs e const uint WM_SYSCOMMAND 0x112; const uint SC_MONITORPOWER 0xF170; SendMessage this.Handle,WM_SYSCOMMAND, IntPtr SC_MONITORPOWER, IntPtr 2 ;

文档评论(0)

1亿VIP精品文档

相关文档