What line of code could I use in C ++ to turn off power saving?

I want the monitor not to fall asleep (setting up Windows, not setting up the monitor). I am using C ++. What challenge can I make?

+5
source share
4 answers
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;
};
+13
source

The simplest method, which does not change the global state of the system, as the first answer, has the following form:

WM_SYSCOMMAND. wParam SC_MONITORPOWER, 0 DefWindowProc. ( wParam - , , , DefWindowProc. .)

+5

SetThreadExecutionState(ES_DISPLAY_REQUIRED|ES_CONTINUOUS);

+3
source

Show the mouse every minute or so.

mouse_event(MOUSEEVENTF_MOVE,1,0,0,0);
mouse_event(MOUSEEVENTF_MOVE,-1,0,0,0);
Sleep(60000);
+1
source

All Articles