用API关闭显示器并锁屏(C,C#,VB6示例)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvyiwuhen/article/details/86230402

使用SendMessage函数可实现,详细用法如下:

1、关闭屏幕

SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 2)

2、屏幕处于低能耗状态

SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1)

3、打开屏幕

SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, -1)

4、屏幕处于低能耗状态

SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1)

5、屏幕处于低能耗状态

SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1)


C语言的,win32程序

#include 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Eliminate user''s interaction for 500 ms
Sleep(500);
// Turn off monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
// Turn on monitor
// SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
// Low power monitor
// SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 1);
return 0;
}

C#的

[System.Runtime.InteropServices.DllImport("user32.dll",   EntryPoint="SendMessage")]  
  public   static   extern   int   SendMessage   (  
            int   hwnd,  
            int   wMsg,  
            int   wParam,  
            int   lParam);  
  SendMessage(this.Handle.ToInt32(),0x112,0xF170,2); //关闭  
  SendMessage(this.Handle.ToInt32(),0x112,0xF170,-1); //打开

VB6的

Option   Explicit  
   
  Private   Declare   Function   SendScreenMessage   Lib   "user32"   _  
        Alias   "SendMessageA"   _  
      (ByVal   hwnd   As   Long,   _  
        ByVal   wMsg   As   Long,   _  
        ByVal   wParam   As   Long,   _  
        lParam   As   Any)   As   Long  
   
  Private   Const   MONITOR_ON   =   -1&  
  Private   Const   MONITOR_LOWPOWER   =   1&  
  Private   Const   MONITOR_OFF   =   2&  
  Private   Const   SC_MONITORPOWER   =   &HF170&  
  Private   Const   WM_SYSCOMMAND   =   &H112  
   
  ''关闭   显示器  
  Public   Function   MonitorOff(Form   As   Form)  
           
          Call   SendScreenMessage(Form.hwnd,   WM_SYSCOMMAND,   SC_MONITORPOWER,   ByVal   MONITOR_OFF)  
   
  End   Function  
   
  ''开启显示器  
  Public   Function   MonitorOn(Form   As   Form)  
           
          Call   SendScreenMessage(Form.hwnd,   WM_SYSCOMMAND,   SC_MONITORPOWER,   ByVal   MONITOR_ON)  
   
  End   Function  
   
  ''关闭显示器电源   :)---深度睡眠  
  Public   Function   MonitorPowerDown(Form   As   Form)  
           
          Call   SendScreenMessage(Form.hwnd,   WM_SYSCOMMAND,   SC_MONITORPOWER,   ByVal   MONITOR_LOWPOWER)  
           
  End   Function

 参考:http://blog.sina.com.cn/s/blog_ae331d770101myu2.html

             https://blog.csdn.net/xs1102/article/details/73466270

猜你喜欢

转载自blog.csdn.net/lvyiwuhen/article/details/86230402