Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Win32

Programmatically Turning on the Screen Saver

Rate me:
Please Sign up or sign in to vote.
4.24/5 (8 votes)
20 May 2009CPL2 min read 35.1K   18   7
Learn how to programmatically turn on the screen saver with API via .NET and C#

I have also written this article in my blog, Just Like a Magic.

Overview

This lesson focuses on how to programmatically turn on the screen saver.

Background

In Windows, you can turn it on automatically by leaving the machine inactive for a specific period. You can control this period from the Screen Saver options from the desktop properties dialog. The following figure shows the Screen Saver Settings dialog.

Screen Saver Settings

Programmatically Turning on the Screen Saver

In this section, we will learn how to turn on the screen saver in .NET and C#. Of course you can write the code in any language you prefer, but here we will write it in C#. You can turn on the screen saver by sending the WM_SYSCOMMAND message with the parameter SC_SCREENSAVE. Sending a message can be done using the SendMessage() function that resides in the User32.dll library. The definition of this function is as follows:

C#
LRESULT SendMessage(
    HWND hWnd,
    UINT Msg,
    WPARAM wParam,
    LPARAM lParam
);

This function takes four arguments:

  • hWnd: Handle to the window to send the message to. You can set this argument to a window handle, the desktop handle (HWND_DESKTOP), or the handle for all top-level windows (HWND_BROADCAST).
  • Msg: The message to send.
  • wParam: Additional message-specific options.
  • lParam: Additional message-specific options.

This function returns a value specific to the message sent. Usually, it returns non-zero if it succeeds or zero otherwise. Here is the full 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);
}

Code Explanation

First, we created our PInvoke method. This method is decorated by the DllImportAttribute attribute specifying the library which the method resides in. Also PInvoke methods must be declared as "static" and "extern". Because LRESULT is defined as a signed 32-bit integer, it is marshaled as System.Int32 in .NET. Also, because System.IntPtr is the best type for marshaling any Win32 raw handle, we have used it for the first argument. UINT, WPARAM, AND LPARAM are all defined as an unsigned 32-bit integer, so we have marshaled them as System.UInt32. HWND_BROADCAST represents the handle for all top-level windows, so we have sent them the order to turn on the screen saver.

PInvoke stands for Platform Invocation, it is the process of creating a wrapper for the .NET to interact with unmanaged functions.

Marshaling is the process of creating a bridge between .NET types and unmanaged types.

You can use PostMessage() in place of SendMessage() if you want to send the message asynchronously and don't want to wait for a response.

History

  • 20th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
QuestionProgramatically inhibiting the screensaver Pin
Sharjith17-Nov-11 9:14
professionalSharjith17-Nov-11 9:14 
GeneralTurning on the screen saver Pin
andywawa1-Feb-11 23:25
andywawa1-Feb-11 23:25 
Questionstart the screensaver ? Pin
gillardg21-May-09 3:10
gillardg21-May-09 3:10 
AnswerRe: start the screensaver ? Pin
Mohammad Elsheimy21-May-09 7:06
Mohammad Elsheimy21-May-09 7:06 
GeneralGreat Example Pin
taloweb20-May-09 20:33
taloweb20-May-09 20:33 
GeneralRe: Great Example Pin
Mohammad Elsheimy21-May-09 7:07
Mohammad Elsheimy21-May-09 7:07 
QuestionWhere's the code? Pin
Dewey20-May-09 14:50
Dewey20-May-09 14:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.