Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need C# code to turn on/off windows screensaver programmatically.
I googled and found some code.

C#
[DllImport("User32.dll")]
public static extern int SendMessage
    (IntPtr hWnd,
    uint Msg,
    uint wParam,
    uint lParam);
public const uint WM_SYSCOMMAND = 0x112;
public const uint SC_SCREENSAVE = 0xF140;
public enum SpecialHandles
{
    HWND_DESKTOP = 0x0,
    HWND_BROADCAST = 0xFFFF
}
public static void TurnOnScreenSaver()
{
    SendMessage(
        new IntPtr((int)SpecialHandles.HWND_BROADCAST),
        WM_SYSCOMMAND,
        SC_SCREENSAVE,
        0);
}


Is it complete code? Qhere is the main method? How can i execute it?
Qhat should I add to this code?

I use:
.NET framework 3.5
Windows XP professional service pack 2
Posted
Updated 10-Oct-10 0:11am
v2

Thanks for your reply. that works fine.
i need another method like TurnOnScreenSaver() that stops the screen saver. That is i wanna turn on the screen saver first (already done) then on a specific condition i wanna turn off the screen saver and continue the process(on and off) . i don't wanna use Timer.
 
Share this answer
 
Yes that should work - all you have to do is call TurnOnScreenSaver when you want to use it.
For example:
C#
private void button1_Click_1(object sender, EventArgs e)
    {
    Timer t = new Timer();
    t.Interval = 2000;
    t.Tick += new EventHandler(t_Tick);
    t.Start();
    }
void t_Tick(object sender, EventArgs e)
    {
    Timer t = sender as Timer;
    if (t != null)
        {
        t.Stop();
        }
    TurnOnScreenSaver();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900