Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-08

Windows API SendMessage()

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I'm trying to use SenMessage() to turn the display on or off under program control of a console application. I got the function to work, and it turns off the display, but then the program just hangs.

Does anyone know what I am doing wrong?

Here's my sample program

Program Displaycontrol;
Uses Windows;
Begin
  Writeln('Turning Off Display');
  SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
//This seems to work, turning off my display
  Writeln('Display is Off');                      // This Writeln never
happens.    I need to force terminate the program.
  sleep(10000);
  Writeln('Turning On Display');
  SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
  Writeln('Display is On');
End.

James

Reply by email

Cyrax
to James Richters
James Richters wrote 45 lines
I'm trying to use SenMessage() to turn the display on or off under program control of a console application. I got the function to work, and it turns off the display, but then the program just hangs. Does anyone know what I am doing wrong? Here's my sample program Program Displaycontrol; Uses Windows; Begin Writeln('Turning Off Display'); SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2); //This seems to work, turning off my display Writeln('Display is Off'); // This Writeln never happens. I need to force terminate the program. sleep(10000); Writeln('Turning On Display'); SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1); Writeln('Display is On'); End. James _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

You need to use PostMessage if you want to your program to work correctly. SendMessage waits reply and you don't have a message pump in your console application, thus a hang happens.

SendMessage :
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx

Reply by email

James Richters
to Cyrax
You need to use PostMessage if you want to your program to work correctly. SendMessage waits reply and you don't have a message pump in your console application, thus a hang happens.

Thank you... PostMessage() works to turn off the display, but for some strange reason it can't turn it back on. I set a 20 second delay, and precisely at 20 seconds my monitor light turns green, but then the monitor goes back to sleep before the screen can be displayed. I'm guessing the signal is being sent to turn on the monitor, but not to get windows out of power save mode... I also see using -1 to turn the display back on is an un-documented feature. I can get the display back on with a keystroke or moving the mouse, but I'm wanting the program to turn the display back on without user input. Can anyone think of any other way I could make my console application to turn the display back on without user input?

I've tried

progname := FindWindow('displaycontrol', nil);
PostMessage(progname, WM_KEYDOWN, VK_F5, 0);

And while the key sent to the program, it does not wake the display up. I see other examples of simulating keyboard input, but I haven't been able to get them to work in a freepascal console application.

James

Reply by email

Santiago A.
to James Richters
James Richters wrote
> You need to use PostMessage if you want to your program to work correctly. SendMessage waits reply and you don't have a message pump in your console application, thus a hang happens. Thank you... PostMessage() works to turn off the display, but for some strange reason it can't turn it back on. I set a 20 second delay, and precisely at 20 seconds my monitor light turns green, but then the monitor goes back to sleep before the screen can be displayed. I'm guessing the signal is being sent to turn on the monitor, but not to get windows out of power save mode... I also see using -1 to turn the display back on is an un-documented feature. I can get the display back on with a keystroke or moving the mouse, but I'm wanting the program to turn the display back on without user input. Can anyone think of any other way I could make my console

I have found this:

https://stackoverflow.com/questions/25011141/turn-off-on-monitor-cant-turn-on

It's in c++, but it looks that since w.8, you must simulate a mouse move.

-------- Saludos Santiago A.

Reply by email

James Richters
to Santiago A.
Show quoted text 5 lines
I have found this: https://stackoverflow.com/questions/25011141/turn-off-on-monitor-cant-turn-on It's in c++, but it looks that since w.8, you must simulate a mouse move.

Thanks for the info. It got me on the right track. It seems to depend on which method is used to simulate mouse or keyboard. Here is what I found out:
Moving the mouse with SetCursorPos(x,y); does not wake up the display Moving the mouse with Mouse_Event(MouseEventF_Move,100,100,0,0); does wake up the display Simulating the Keyboard with PostMessage(progname, WM_KEYDOWN, VK_F5, 0); does not wake the display Simulating the Keyboard with Keybd_Event(VK_Shift,1,0,0); does wake up the display

James

Reply by email