简体   繁体   English

如何在C#中正确关闭显示器电源?

[英]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. LCD指示灯仍然亮起,但屏幕完全变黑。

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. 我不知道为什么会发生这种情况,也许有些人知道为什么......如果我设置Windows电源设置以在X分钟后关闭显示器,这将永远不会发生,但我需要一个应用程序来执行此操作以便我可以调用它我想要的捷径。

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 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. 通常1次重启将解决问题。 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? 显然有一些过程或过程需要责备,但你如何追踪它们呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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