I’m writing an app that at times will send notifications to the user in the form of toaster messages.
If the user is not there, he can’t see the notification. So what I wanna do is be able to check if the user has locked the screen or if the screensaver happens to be activated.
Any notification that is triggered while the user cannot see it will be delayed and shown when the user logs back in and resumes his session.
I’m on Windows 7 myself, but I’d prefer a solution that works universally for Windows XP and up.
There is no documented way to find out if the workstation is currently locked. You can however get a notification when it un/locks. Subscribe the SystemEvents.SessionSwitch event, you’ll get SessionSwitchReason.SessionLock and Unlock.
The sceen saver is troublesome too. Your main window gets the WM_SYSCOMMAND message, SC_SCREENSAVE when the screen saver turns on. You can pinvoke SystemParametersInfo to check if it running. You’ll find sample code for this in my answer in this thread.
There is no good way to find out if the user fell asleep.