tags:

views:

1236

answers:

1

Hopefully this is a simple one, but can anyone provide some simple c# code that will launch the currently configured screensaver?

+3  A: 

Here is a good site showing how to work with all aspects of the screensaver. See the comments at the end for the code to start the screensaver.

http://www.codeproject.com/KB/cs/ScreenSaverControl.aspx

    [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
    private static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    //...

    private const int SC_SCREENSAVE = 0xF140;
    private const int WM_SYSCOMMAND = 0x0112;

    //...

    public static void SetScreenSaverRunning()
    {
    SendMessage

(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
JTA
The PInvoke signature is incorrect. Both wParam and lParam should be typed to IntPtr
JaredPar
Drats I was just writing this, and that code above works.
cfeduke
The code works with ints in the SendMessage for w and l params, but as its written the const ints will not work with IntPtrs.
cfeduke
Thanks for the change. Dont have an IDE on this computer to test. Putting good faith in the community and online content. Thanks again :-)
JTA
Argument '3': cannot convert from 'int' to 'System.IntPtr' - editing James' code back to ints, it ran correctly before IntPtr edits.
cfeduke
It runs correctly on a 32 bit machine but will fail miserably on a 64 bit one.
JaredPar
@cfeduke JaredPar is correct that wParam and lParam should be IntPtr. You need to explicitly cast the message codes to IntPtr in order to get rid of the compiler error.Example:(IntPtr)WM_SYSCOMMAND
Zach Johnson