tags:

views:

35

answers:

3

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.

+1  A: 

There are many reason why the user can't see your notifications also for example full screen video playback or that the user is just not there.

I suggest that instead of checking if the notification can be displayed check if the user is there, you can do that by monitoring the keyboard and mouse.

Shay Erlichmen
That might work, but only as long as the user doesn't play full-screen games.
Pieter
@Pieter you need to check that the screen is not in full screen exclusive mode
Shay Erlichmen
+1  A: 

Use SystemParametersInfo to detect whether screen saver is running - the calltype is SPI_GETSCREENSAVERRUNNING. This is supported on Win2000 and above.

There is code from @dan_g on StackOverflow here to check if wksta is locked.

Steve Townsend
+1  A: 

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.

Hans Passant