windows上实现锁屏和息屏

时间:2024-03-11 08:04:21

可以直接调用windows api:

#include <Windows.h>

#define MONITOR_ON -1
#define MONITOR_OFF 2
#define MONITOR_STANBY 1

int main()
{
	//锁屏
	LockWorkStation();

	//息屏
	PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);

	return 0;
}

有关锁屏函数LockWorkStation的介绍:https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-lockworkstation
有关息屏使用的函数PostMessage介绍:https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagew