DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: NANY 2019 - DisplayOff  (Read 13,808 times)

GrumpyCoder

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 37
    • Participated in NANY Challenge Five Times
    • Writer
NANY 2019 - DisplayOff
« on: December 05, 2018, 04:32 AM »
Application NameDisplayOff
Version1.0.0.0
Short Description Turns off the display and terminates itself.
Supported OSes Windows 7, 8, or 10
Download https://www.dropbox.com/s/30szbzs9ra2e92l/DisplayOff.zip
Author Lohengrin Percival

Description
I needed a way to manually turn off the monitor without having to use the physical power button, so I made this.
It doesn't put the PC to sleep, it just turns off the display the same way Windows does, then terminates itself and moving the mouse or pressing any key on the keyboard will turn the display back on.

Bonus
Because the application is so simple, I decided to share the code as well.

Code: ( csharp)
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class DisplayManager
{
    private const int SC_MONITORPOWER = 0xF170;
    private const uint WM_SYSCOMMAND = 0x0112;

    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    public enum MonitorState
    {
        On = -1,
        Off = 2,
        Standby = 1
    }

    public void SetMonitorState(MonitorState state)
    {
        SendMessage(new Form().Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)state);
    }
}

History
1.0.0.0 - Initial release

PS: I couldn't find the template, so I copied c.gingerich format.
« Last Edit: December 05, 2018, 04:49 AM by SwanKnight »

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: NANY 2019 - DisplayOff
« Reply #1 on: December 06, 2018, 04:11 AM »
Welcome to the DonationCoder site, SwanKnight, and thank you for your NANY contribution.  :up:  :D

KodeZwerg

  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 718
    • Participated in NANY Challenge Four Times
    • Coding Snacks
Re: NANY 2019 - DisplayOff
« Reply #2 on: December 07, 2018, 08:58 AM »
Thanks for sharing this. I have tested it and by reading your source i see that you do not trigger system, just override a powerstate.
This action kinda confuse my Windows own powersetting. (if system goes sleep mode ScreenSaver -> ScreenOff, then automatic start background jobs, those wont executed anymore with your method)

I look on my own how to deal with WinApi correct to do what you done.
For myself i just done some overrides that do exact the different way, let system always be on :-)
(i needed such feature for my MediaPlayer project, when no mousemovement = screen goes blank = monitor speaker goes off)

GrumpyCoder

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 37
    • Participated in NANY Challenge Five Times
    • Writer
Re: NANY 2019 - DisplayOff
« Reply #3 on: December 07, 2018, 09:18 AM »
Welcome to the DonationCoder site, SwanKnight, and thank you for your NANY contribution.  :up:  :D

Thank you.

Thanks for sharing this. I have tested it and by reading your source i see that you do not trigger system, just override a powerstate.
This action kinda confuse my Windows own powersetting. (if system goes sleep mode ScreenSaver -> ScreenOff, then automatic start background jobs, those wont executed anymore with your method)

The whole purpose of this code is turn off the display and nothing else.
Correct me if I'm wrong, but you want the PC to be also in a idle state? Frankly, I'm not sure how to even test for that.

Perhaps this article could be helpful. It goes in details about how the code works and some other API calls.

Someone suggested using PostMessage instead of SendMessage, but frankly I'm not sure if it'll make a difference or not.

Code: ( csharp)
[DllImport("user32.dll")]
static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);

Original address: https://www.donationcoder.com/forum/index.php?topic=46783.0