模拟睡眠模式(关闭屏幕并禁用输入) [英] Simulated Sleep Mode (turn screen off and disable inputs)
问题描述
大家好,
我正在用c#开发一个程序,该程序要求用户模拟睡眠模式而计算机不会真正进入睡眠状态,并保持程序运行.主要是,我需要关闭屏幕并禁用除电源按钮以外的所有用户输入. 然后,电源按钮将重新启用输入并重新打开屏幕(这将主要用于win8.1平板电脑).这可能吗?
I am developing a program in c# that requires the user to simulate a sleep mode without the computer actually going to sleep and keeps the program running. Mainly, I need to turn the screen off and disable all user inputs except the power button. The power button will then re-enable the inputs and turn the screen back on (this will mainly be used on a win8.1 tablet). Is this possible?
我可以关闭屏幕,但是任何输入都可以重新打开它.
I can turn the screen off, but any input brings it right back on.
所以我的主要2个问题:如何禁用所有用户输入(键盘/鼠标/触摸屏)?以及如何拦截电源按钮以返回?
So my main 2 questions: How do I disable all user inputs (keyboard/mouse/touchscreen)? And how do I intercept to the power button to return?
谢谢大家.
PS.如果我要求c/c ++这样做,也可以.
PS. If I require c/c++ to do this, that's ok too.
推荐答案
您可以使用的发送消息功能 Win32 AP I.在C#中,您可以通过InteropServices调用它.
You can use the send message function of Win32 API. In C#, you may call it via InteropServices.
using System.Runtime.InteropServices;
private const int HWND_BROADCAST = 0xFFFF;
private const int SC_MONITORPOWER = 0xF170;
private const int WM_SYSCOMMAND = 0x112;
private const int MONITOR_ON = -1;
private const int MONITOR_OFF = 2;
private const int MONITOR_STANBY = 1;
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
//turn off monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
//turn on monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
有关WM_SYSCOMMAND的更多详细信息,此处.
More details about WM_SYSCOMMAND here.
要禁用用户输入,可以使用 BlockInput函数.
To disable the user input, you may use the BlockInput function.
这篇关于模拟睡眠模式(关闭屏幕并禁用输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!