Programmatically Turning on the Screen Saver

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.


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


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:

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 succeed or zero otherwise.

Here is the full code:

// C# Code

static extern int SendMessage
    (IntPtr hWnd,
    uint Msg,
    uint wParam,
    uint lParam);

const uint WM_SYSCOMMAND = 0x112;
const uint SC_SCREENSAVE = 0xF140;
const uint HWND_BROADCAST = 0xFFFF;

static void Main()
    new IntPtr((int)HWND_BROADCAST),
' VB.NET Code

Declare Auto Function SendMessage Lib "user32.dll" _
    (ByVal hWnd As IntPtr, _
    ByVal Msg As UInt32, _
    ByVal wParam As UInt32, _
    ByVal lParam As UInt32) As Int32

Const WM_SYSCOMMAND As UInt32 = &h212
Const SC_SCREENSAVE As UInt32 = &HF140

Sub Main()
    SendMessage( _
        New IntPtr(CInt(HWND_BROADCAST)), _
        WM_SYSCOMMAND, _
        SC_SCREENSAVE, _
End Sub

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 defined as a signed 32-bit integer, it is marshaled as System.Int32 in .NET. Also, because of 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.

Read more about PInvoking and Marshaling in other API lessons.

Changing the Windows Logon Screensaver

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

When you start Windows, you may be represented with the welcome screen, which prompts you to enter your username and password. If you leave the machine inactive for a specific time, the Windows logon screen saver starts. That screen saver is the Blank screen saver (Logon.scr) represents a blank black screen.

You can control the Windows logon screen saver from the registry options in HKEY_USERS.DefaultControl PanelDesktop.

Registry Editor + Logon Screen Saver

Changing the screen saver options

To change the logon screen saver options follow the following steps:

  1. Open the Registry Editor from Start -> Run -> regedit.exe.
  2. Locate the logon options in the registry key HKEY_USERS.DefaultControl PanelDesktop.

There, we are interested in three values:

    For changing the current screen saver.
    Set this value to the path of the new screen saver (relative paths allowed.)
    Note that, most screen savers are located in %windir%System32 (the default path) folder and they have the extension “scr”.
    The default screen saver is the Blank screen saver (Logon.scr).
  • ScreenSaveTimeOut:
    For changing the time that you must wait for the screen saver to start.
    Set this value to the number of seconds to wait. For example, 60 for a minute.
    The default timeout is 600 seconds for 10 minutes.
  • ScreenSaveActive:
    For turning the screen saver on or off.
    To turn off the screen saver set this value to 0. Conversely, set it to 1 for turning it on.

Actually, these options and others on “HKEY_USERS.DefaultControl Panel” are the default options for new users. For example, setting the Desktop/FontSmoothing to 2 means sets the screen font smoothing to ClearType for every new user. And changing the screen saver options also, affects all the new users not the logon screen only.

To be honest, the HKEY_USERS hive contains the customizations for every user on the machine based on his SID (Security Identifiers). So, you can find users’ customizations in this registry hive. Also, you can find current user’s customizations in HKEY_CURRENT_USER hive.