用API關閉顯示器並鎖屏(C,C#,VB6示例)
阿新 • • 發佈:2019-01-10
使用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