I have an ACEPC T6, with a 7 inch LCD screen connected via HDMI, and a USB touchscreen.
This system is connected to a processor board via a USB serial link which reads various sensors and feeds the data back to the PC.
The program on the PC is written with VB.NET 2019. Everything works fine with one exception.
I use the ScreenSaver to blank the screen after 10 minutes of activity, and touching the screen brings things back fine.
My problem is that I have a PIR sensor connected to the processor, which sends a command back to the PC. My objective is for this to simulate the screen being touched, and the display be restored, i.e. when someone walks into the room, the display returns.
I have tried all sorts of options, simulating mouse effects, sending key strokes, but all to no avail.
Any help would be appreciated.
I should have mentioned I am running Windows 10 Pro.
You're probably not able to have a positive result using SendInput() or other functions because a ScreenSaver runs in special Desktop, named (guess what)
Screen-saver
.You need to get the handle to this Desktop to actively interact with it or its Windows.
You can use the SystemParametersInfo function, passing
SPI_GETSCREENSAVERRUNNING
to determine whether a ScreenSaver is currently active, then call OpenDesktop() get the handle of the Desktop (using its name), find the ScreenSaver Window (the ScreenSaver is a standard executable) and post aWM_CLOSE
message to close it.Here, I'm using EnumDesktopWindows to find the ScreenSaver.
(The ScreenSaver Window should the be Foreground Window anyway).
In case it fails, we can try to close the Desktop, calling CloseDesktop().
Some more possibly interesting info related to ScreenSaver here:
Handling Screen Savers
If you have a procedure that receives a Command through a serial port or something similar, verify whether this procedure is run in a Thread other than the UI Thread, in case you need to access the UI in this occasion.
Add this code to the procedure: it checks whether a ScreenSaver is active and tries to close it.
It's been tested in both Windows 10 (19H2) and Windows 7 SP1.
.Net Framework 4.8 - VB.Net v.15
► Assuming this procedure is run in the UI Thread.
Me.Activate()
is not strictly required and can be used from the UI Tread only. Remove it if your code runs on a different Thread.NativeMethods
class and helper methods:C# version, just in case: