views:

866

answers:

2

I have an application than disables the screen saver temporarily in order to run a slide show. I use this to turn it on or off:

i = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, active, Nothing, SPIF_SENDWININICHANGE)

This works fine in XP. In Windows 7, it disables the screen saver properly. However, when it is enabled again, at the normal time for the screen saver to come on, the system asks for a password instead of showing the screen saver.

From this time on until the screen saver settings are adjusted in the control panel, the password screen is displayed in place of the screen saver.

Is there something else I should be doing for Windows 7? I understand that SPI_GETSCREENSAVEACTIVE is not supported in Windows 7, but SPI_SETSCREENSAVEACTIVE is supposed to be.

+1  A: 

What I've done before in this situation is not to disable the screensaver, but to prevent the screensaver from starting. To do this, I periodically (on a timer) send a "Left-Shift Up" keystroke to the OS.

c#:

[DllImport("user32")]
private static extern void keybd_event(byte bVirtualKey, byte bScanCode, int dwFlags, int dwExtraInfo);

private const byte VK_LSHIFT = 0xA0;
private const int KEYEVENTF_KEYUP = 0x0002;

// When the timer elapses, send Left Shift Up
private void timer1_Tick(object sender, EventArgs e)
{
    keybd_event(VK_LSHIFT, 0x45, KEYEVENTF_KEYUP, 0);
}

vb.net:

Private Const VK_LSHIFT As Byte = 160

Private Const KEYEVENTF_KEYUP As Integer = 2

Private Declare Sub keybd_event Lib "user32" (ByVal bVirtualKey As Byte, ByVal bScanCode As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

' When the timer elapses, send Left Shift Up
Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
    keybd_event(VK_LSHIFT, 69, KEYEVENTF_KEYUP, 0)
End Sub

(I'm not a vb developer, I just ran the c# code through an automated c# -> vb.net converter)

I figure that a Left Shift Up keystroke is the least likely to interfere with an application. The worst that can happen is that, if, at the exact same that the timer fires, you have the Left Shift down, it will cause the shift to end.

You can, of course, use any other key to keep the screen saver from activating.

fre0n
Why the downvote?
fre0n
This would work, but it would cause some complications that I'd like to avoid. I prefer to learn how to make it work in Win7 without periodic events. (Don't know about the downvote.)
xpda
+2  A: 

The solution is to change the registry value HKCU\Control Panel\Desktop ScreenSaveActive key instead of using SystemParametersInfo. This works in both XP and Windows 7.

Call Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop", "ScreenSaveActive", "1")
xpda