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
2008-11-06 05:47:09
The PInvoke signature is incorrect. Both wParam and lParam should be typed to IntPtr
JaredPar
2008-11-06 05:50:53
Drats I was just writing this, and that code above works.
cfeduke
2008-11-06 05:51:29
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
2008-11-06 05:56:04
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
2008-11-06 05:57:47
Argument '3': cannot convert from 'int' to 'System.IntPtr' - editing James' code back to ints, it ran correctly before IntPtr edits.
cfeduke
2008-11-06 06:11:43
It runs correctly on a 32 bit machine but will fail miserably on a 64 bit one.
JaredPar
2008-11-06 06:49:40
@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
2009-06-05 21:17:44