简体   繁体   中英

How to correctly turn off monitor power in C#?

I'm already using some code to turn off the monitor power but I'm still with a little problem.

The code works fine most of times but once a in a while, the monitor turns blank. What I mean by this is that the screen is totally black, you can't see anything, but you can see it's still lit. The LCD light is still turned on but the screen is completely black.

I have no idea why this happens, maybe some of you know why... This will never happen if I set Windows Power settings to turn the monitor off after X minutes, but I need an application to do this so I can call it with a shortcut whenever I want.

The code I'm using is something like this:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

const int WM_SYSCOMMAND   = 0x0112;
const int SC_MONITORPOWER = 0xF170;
const int HWND_BROADCAST  = 0xFFFF;

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);

Any ideas?

You don't want to be broadcasting that message to all windows. You are relying on every one of them to process it as you are expecting.

You need to create your own and only send the message to it so you can control the behavior.

There's a few solutions in the comments of this post by Raymond Chen: http://blogs.msdn.com/oldnewthing/archive/2006/06/13/629451.aspx

Have you tested this on multiple machines? Could be a driver problem...

没有任何效果,我创建了自己的Window并使用了Window窗口,大多数时候,屏幕关闭,它完全变黑(LCD灯熄灭),但每隔一段时间,LCD灯就不会关闭...... :(

i've got this problem too. machines often work ok, and then for no known reason, wont allow the screens to sleep. I can't find a concrete commonality.

It even happens when machines are logged out. Usually 1 reboot will clear the problem. Not always.

I've even seen the screens wake up and refuse to sleep again on a machine that was logged out, and no one interacted with it.

Obviously there is a some process or processes to blame, but how do you trace them?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM