Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendMessage/SC_MONITORPOWER won't turn monitor ON when running Windows 8

I turn my monitors on and off by using the following code:

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MONITORPOWER = 0xF170;
private const int MonitorTurnOn = -1;
private const int MonitorShutoff = 2;

//Turn them off
SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MonitorShutoff);

//Turn them on
SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MonitorTurnOn);

This used to work as intended, but after installing Windows 8 (I assume this is the reason, since I see others have the same issue) turning the screen on won't work. I can still turn it off, but no matter how many times I run SendMessage() with MonitorTurnOn, I still have to move the mouse or press a key to get the monitors back on.

Any suggestions on how to make this work on Windows 8?

like image 205
Erlend D. Avatar asked Sep 24 '12 20:09

Erlend D.


3 Answers

I had the same problem, the solution I found is to move the mouse :

mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, NULL);
Sleep(40);
mouse_event(MOUSEEVENTF_MOVE, 0, -1, 0, NULL);

It will wake the monitor on. Earlypearl

like image 156
earlypearl Avatar answered Oct 28 '22 05:10

earlypearl


Here's Earlypearl's answer with the needed includes:

[DllImport("user32.dll")]
static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);

private const int MOUSEEVENTF_MOVE = 0x0001;

private void Wake(){
    mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);
    Sleep(40);
    mouse_event(MOUSEEVENTF_MOVE, 0, -1, 0, UIntPtr.Zero);
}
like image 28
Erlend D. Avatar answered Oct 28 '22 06:10

Erlend D.


I had the same idea for this issue Just Changed the dear earlypearl's solution a wee bit and tested it on windows XP, 7, 8, Server 2008 and all worked perfectly.

mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);

it does not need to be called twice.

like image 2
Omid S. Avatar answered Oct 28 '22 04:10

Omid S.