如何在笔记本电脑的 Windows 7 上检测电源按钮事件并拒绝它 [英] How to detect power button event and deny it on windows 7 from laptop

查看:39
本文介绍了如何在笔记本电脑的 Windows 7 上检测电源按钮事件并拒绝它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows XP 上,我可以在按下笔记本电脑的电源按钮时检测到事件.获取APMQUERYSUSPEND事件的条件是[控制面板->电源选项->系统设置->当我按下电源按钮时->睡眠]选项必须更改为睡眠".

On windows XP, I can detect event when pushing power button of laptop. Condition to get APMQUERYSUSPEND event is option of [control panel->power option->system setting->when I press the power button->sleep] must be changed to ‘Sleep’.

MainFrm::OnPowerBroadcast(WPARAM wParam, LPARAM lParam)

{

         switch (wParam)

         {

                       case PBT_APMQUERYSUSPEND:

                       // Ask question whether to power off or not

                       // If not, return BROADCAST_QUERY_DENY

                       return BROADCAST_QUERY_DENY;

         }

但是从 Windows 7 开始,我没有任何线索可以检测到该事件.基于 Windows 7,删除了 APMQUERYSUSPEND 事件.即使我尝试了 SetThreadExecutionState API 来阻止关闭,它也不起作用.http://msdn.microsoft.com/ko-kr/library/windows/desktop/aa372716(v=vs.85).aspx

But from windows 7, I don't have any clue to detect the event. Based on windows 7, APMQUERYSUSPEND event was removed. Even though I tried SetThreadExecutionState API to block turn-off, it dose not work. http://msdn.microsoft.com/ko-kr/library/windows/desktop/aa372716(v=vs.85).aspx

你知道按下电源按钮时捕捉事件的任何想法吗?

Do you know any idea to catch event when pushing power button?

谢谢.

推荐答案

根据 Microsoft 的文档,睡眠事件一旦启动就无法再中断.有一个关于 Windows Vista/7 电源控制的内容非常丰富的演示 这里.还有一种方法可以捕获睡眠按钮事件.我将在示例中使用 C# 语法:

According to Microsoft's documentation, it is no longer possible to interrupt a sleep event once it has been initiated. There's a very informative presentation on Windows Vista/7 power control here. There is still a way to capture the sleep button event. I'll be using C# syntax here in my example:

首先,从 user32.dll API 调用 RegisterHotKey(this.Handle, 0, 0x4000, 0x5F) 来钩住睡眠键.然后覆盖您程序的WndProc 方法(取决于您使用的语言和环境,有不同的方法可以做到这一点)并监听热键消息312h.收到后立即调用 SetThreadExecutionState(EXECUTION_STATE.ES_AWAYMODE_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS) 来自 kernel32.dll API 以防止 Windows 启动睡眠事件.一段时间后(如果您希望您的系统能够再次休眠),您可以调用 SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS).

First, call RegisterHotKey(this.Handle, 0, 0x4000, 0x5F) from user32.dll API to hook the sleep key. Then override your program's WndProc method (depending on what language and environment you're using, there are different ways to do that) and listen for the hotkey message 312h. As soon as you receive it, call SetThreadExecutionState(EXECUTION_STATE.ES_AWAYMODE_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS) from kernel32.dll API to prevent Windows from initiating a sleep event. After some time (if you want your system to be able to sleep again), you can call SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS).

注意SetThreadExecutionState在sleep程序已经启动的情况下不会有任何影响,这就是为什么上面的不能对电源按钮做的原因,就我而言无法hook知道(如果有人真的知道这样做的方法,请告诉我!).这也可能是 SetThreadExecutionState 对您不起作用的原因.

Note that SetThreadExecutionState will not have any effect if a sleep procedure has already been initiated, which is why the above is not possible to do for the power button, which cannot be hooked as far as I know (if anyone actually knows of a way to do that, please let me know!). This is also probably why SetThreadExecutionState didn't work for you.

常量是:

        ES_CONTINUOUS = 0x80000000,
        ES_SYSTEM_REQUIRED = 1,
        ES_AWAYMODE_REQUIRED = 0x00000040

这篇关于如何在笔记本电脑的 Windows 7 上检测电源按钮事件并拒绝它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆