Search code examples
c#screensaver

Launch System Screensaver from C# Windows Form


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


Solution

  • 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);
    }