简体   繁体   中英

How to turn off the display of a Win8 tablet

Is there a way to turn off the display of a Win8 Tablet without putting the tablet in sleep mode?

I use following C++ code, but this code puts the tablet in sleep mode:

const LPARAM OFF = 2;
// const LPARAM LOW = 1;
const LPARAM ON = -1;
LPARAM state = 0;

if (monitorOn) state = ON;    // set monitor on
else state = OFF;             // set monitor off

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, state);

We need to create a new VNC connection while the display is off. But we can't do that while the Tablet is in sleeping mode. Also the monitor on functionality (see code above) doesn't work in sleeping mode...

Anybody knows how I can only turn off the display of a Win8 Tablet?

You can try using the Power Management API to keep the computer on when it is put to sleep. I'm not sure if you can still connect using VNC when the computer is in this state, but it's worth a try.

#include <atlbase.h>
#include <atlutil.h>
#include <powrprof.h>

#pragma comment(lib, "PowrProf.lib")

#include <iostream>

using namespace std;

int main()
{
  try
  {
    POWER_REQUEST_CONTEXT context;
    context.Version = POWER_REQUEST_CONTEXT_VERSION;
    context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
    context.Reason.SimpleReasonString = L"Turn screen off";

    CHandle powerRequest(PowerCreateRequest(&context));
    if(powerRequest == INVALID_HANDLE_VALUE)
      AtlThrowLastWin32();

    if(!PowerSetRequest(powerRequest, PowerRequestAwayModeRequired))
      AtlThrowLastWin32();

    if(!SetSuspendState(FALSE, FALSE, FALSE))
      AtlThrowLastWin32();

    if(!PowerSetRequest(powerRequest, PowerRequestAwayModeRequired))
      AtlThrowLastWin32();

    return 0;
  }
  catch (const CAtlException &e)
  {
    wcout << "Error: " << AtlGetErrorDescription(e).GetString() << endl;

    return e.m_hr;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM