How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Turn monitor on/off in C#

The following snippet will allow you to change your monitor’s state to either off/on or standby mode. Unlike other methods this one works on Windows 7 as well (tested under Windows 7 64bit).

The first step is to include in your class the following code:

This will allow us to send a WM_SYSCOMMAND message using SendMessage to alter the state of the monitor.

Finally, add the method which we will be calling when we want to change the monitor’s state:

Simply call the SetMonitorState method with the desirable state you want to change your monitor’s state to.

Usage:

Keep in mind that the SC_MONITORPOWER commands supports devices that have power-saving features, so depending on the monitor’s brand/drivers/firmware results might vary.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

3 Responses to Turn monitor on/off in C#

  1. Logan says:

    Somehow i am not able to use Handle

  2. Gabor says:

    Hi, I want to switch only my secondary monitor, not the main desktop.
    How can i send the command to this?
    How can i get the handle for each in multi monitor system?

    • DOSLuke says:

      Look up the screens class. It has a methods in it to select the proper screen and then that resulting screen object has a handle property.

Leave a Reply

Your email address will not be published. Required fields are marked *