topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 11, 2018, 09:57 AM
  • Proudly celebrating 13 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: NANY 2019 - DisplayOff  (Read 217 times)

SwanKnight

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 2
    • View Profile
    • Donate to Member
NANY 2019 - DisplayOff
« on: December 04, 2018, 10:32 PM »
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....2e92l/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: C# [Select]
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4.  
  5. public class DisplayManager
  6. {
  7.     private const int SC_MONITORPOWER = 0xF170;
  8.     private const uint WM_SYSCOMMAND = 0x0112;
  9.  
  10.     [DllImport("user32.dll")]
  11.     static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  12.  
  13.     public enum MonitorState
  14.     {
  15.         On = -1,
  16.         Off = 2,
  17.         Standby = 1
  18.     }
  19.  
  20.     public void SetMonitorState(MonitorState state)
  21.     {
  22.         SendMessage(new Form().Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)state);
  23.     }
  24. }

History
1.0.0.0 - Initial release

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

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,032
    • View Profile
    • Donate to Member
Re: NANY 2019 - DisplayOff
« Reply #1 on: December 05, 2018, 10:11 PM »
Welcome to the DonationCoder site, SwanKnight, and thank you for your NANY contribution.   :up:   :D

KodeZwerg

  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 370
    • View Profile
    • Donate to Member
Re: NANY 2019 - DisplayOff
« Reply #2 on: December 07, 2018, 02: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)
sorry bad english and Delphi are my hobby ;)
politeness is not one of my strengths in writing, just because it sounds rough doesn't mean that I mean it rough.

SwanKnight

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 2
    • View Profile
    • Donate to Member
Re: NANY 2019 - DisplayOff
« Reply #3 on: December 07, 2018, 03: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: C# [Select]
  1. [DllImport("user32.dll")]
  2. static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
  3. PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);