ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Special User Sections > N.A.N.Y. 2019

NANY 2019 - DisplayOff

(1/1)

GrumpyCoder:
Application NameDisplayOffVersion1.0.0.0Short Description Turns off the display and terminates itself.Supported OSes   Windows 7, 8, or 10Download   https://www.dropbox.com/s/30szbzs9ra2e92l/DisplayOff.zipAuthor   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# ---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.

skwire:
Welcome to the DonationCoder site, SwanKnight, and thank you for your NANY contribution.   :up:   :D

KodeZwerg:
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:
Welcome to the DonationCoder site, SwanKnight, and thank you for your NANY contribution.   :up:   :D
-skwire (December 05, 2018, 10:11 PM)
--- End quote ---

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)
-KodeZwerg (December 07, 2018, 02:58 AM)
--- End quote ---

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# ---[DllImport("user32.dll")]static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);

Navigation

[0] Message Index

Go to full version