What line of code could I use in C++ to disable en

2020-06-22 08:59发布

I want to prevent the monitor from going to sleep (the windows setting, not the monitor setting). I am using c++. What call do I make?

标签: c++ winapi
4条回答
萌系小妹纸
2楼-- · 2020-06-22 09:14

Wiggle the mouse every minute or so.

mouse_event(MOUSEEVENTF_MOVE,1,0,0,0);
mouse_event(MOUSEEVENTF_MOVE,-1,0,0,0);
Sleep(60000);
查看更多
小情绪 Triste *
3楼-- · 2020-06-22 09:27

SetThreadExecutionState(ES_DISPLAY_REQUIRED|ES_CONTINUOUS);

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-06-22 09:29
class KeepDisplayOn
{
public:
    KeepDisplayOn()
    {
        mPrevExecState = ::SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
        ::SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &mPrevScreenSaver, 0);
        ::SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, FALSE, NULL, 0);
    }

    ~KeepDisplayOn()
    {
        ::SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, mPrevScreenSaver, NULL, 0);
        ::SetThreadExecutionState(mPrevExecState);
    }

private:
    UINT                mPrevScreenSaver;
    EXECUTION_STATE     mPrevExecState;
};
查看更多
Rolldiameter
5楼-- · 2020-06-22 09:33

A simpler way that doesn't modify global system state like the first response does:

In your window procedure, add a handler for WM_SYSCOMMAND. When wParam is SC_MONITORPOWER, return 0 instead of deferring to DefWindowProc. (When wParam is any other value, make sure you either handle the message or pass it to DefWindowProc. Otherwise the user will have difficulty adjusting your window at runtime.)

查看更多
登录 后发表回答