在 Windows 中捕获笔记本电脑盖关闭事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4486674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:49:42  来源:igfitidea点击:

Capturing laptop lid closing event in windows?

windows

提问by Bear of the Year

I am looking for a way to intercept the laptop lid closing event. In windows 7, the power management allows me to select a desired behavior when the lid is closed. So there must be a way windows knows when the lid is closed.

我正在寻找一种方法来拦截笔记本电脑盖关闭事件。在 Windows 7 中,电源管理允许我在关闭盖子时选择所需的行为。所以一定有办法让窗户知道盖子什么时候合上。

I did my research, but only found suggestions to monitor the sleep event. I would like to be more specific to only respond to lid closing.

我做了我的研究,但只找到了监测睡眠事件的建议。我想更具体地只响应盖子关闭。

Does anyone have a suggestion?

有人有建议吗?

Thanks!

谢谢!

采纳答案by Samuel Neff

You can register for notification when the lid is shut with RegisterPowerSettingNotification.

当盖子关闭时,您可以注册通知RegisterPowerSettingNotification

AnswerLid Close Action change notification

AnswerLid 关闭操作更改通知

http://social.msdn.microsoft.com/Forums/en-US/tabletandtouch/thread/0bbf90be-9322-47fb-bfa4-016b57211b3a

http://social.msdn.microsoft.com/Forums/en-US/tabletandtouch/thread/0bbf90be-9322-47fb-bfa4-016b57211b3a

In Vista you can register for a callback for when the Lid Close Action changes. This is done by calling RegisterPowerSettingNotification (see http://msdn2.microsoft.com/en-us/library/aa373196.aspxfor details). The GUID for this power setting you're interested in is GUID_LIDCLOSE_ACTION. This is defined in wdm.h in the Platform SDK.

Once registered, a WM_POWERBROADCAST will be sent to your application with wParam set to PBT_POWERSETTINGCHANGE. This event is sent anytime the value for the lid close action changes. The lParam contains a pointer to a POWERBROADCAST_SETTING structure (see http://msdn2.microsoft.com/en-us/library/aa372723.aspx) containing information on the setting change.

在 Vista 中,您可以注册盖子关闭操作更改时的回调。这是通过调用 RegisterPowerSettingNotification 完成的(有关详细信息,请参阅http://msdn2.microsoft.com/en-us/library/aa373196.aspx)。您感兴趣的此电源设置的 GUID 是 GUID_LIDCLOSE_ACTION。这在平台 SDK 的 wdm.h 中定义。

注册后,WM_POWERBROADCAST 将发送到您的应用程序,并将 wParam 设置为 PBT_POWERSETTINGCHANGE。每当盖子关闭操作的值发生变化时,就会发送此事件。lParam 包含一个指向 POWERBROADCAST_SETTING 结构的指针(参见http://msdn2.microsoft.com/en-us/library/aa372723.aspx),其中包含有关设置更改的信息。

回答by Stefan

The question refers to GUID_LIDSWITCH_STATE_CHANGE not to GUID_LIDCLOSE_ACTION.

问题是指 GUID_LIDSWITCH_STATE_CHANGE 而不是 GUID_LIDCLOSE_ACTION。

GUID_LIDCLOSE_ACTION monitors if the user changes the power behavior when the lid is closing (Control Panel -> Power Settings -> Choose what the close lid does)

GUID_LIDCLOSE_ACTION 监控用户是否在合上盖子时更改电源行为(控制面板 -> 电源设置 -> 选择合上盖子的作用)

If you want to monitor the event of lid close/open, you need to register for GUID_LIDSWITCH_STATE_CHANGE. I used it a Windows service:

如果要监控盖子关闭/打开的事件,则需要注册 GUID_LIDSWITCH_STATE_CHANGE。我使用它作为 Windows 服务:

int ServiceMain(int argc, char** argv)
{
    serviceStatusHandle = RegisterServiceCtrlHandlerExA(serviceName, (LPHANDLER_FUNCTION_EX) ServiceControlHandler, 0);
    ...
    lidcloseRegHandle = RegisterPowerSettingNotification(serviceStatusHandle, &GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_SERVICE_HANDLE);
    ...
}

And in service control handler:

在服务控制处理程序中:

/**
* Event handler for windows service.
*/
void WINAPI ServiceControlHandler(DWORD controlCode, DWORD evtype, PVOID evdata, PVOID Context)
{
    switch (controlCode)
    {...
         case SERVICE_CONTROL_POWEREVENT:
         WriteToLog("Service Control: SERVICE_CONTROL_POWEREVENT builds and fwd the msg");
         msg.control = SERVICE_CONTROL_POWEREVENT;
         msg.event_type = (int) evtype;
         msg.event_data = evdata;
     ...
    }
}

evtype is PBT_POWERSETTINGCHANGE and in evdata you have the event logged: 0 for closed and 1 for opened.

evtype 是 PBT_POWERSETTINGCHANGE 并且在 evdata 中记录了事件:0 表示关闭,1 表示打开。

More details here: https://msdn.microsoft.com/en-us/library/aa372723.aspxhttps://msdn.microsoft.com/en-us/library/hh769082(v=vs.85).aspx

更多详细信息:https: //msdn.microsoft.com/en-us/library/aa372723.aspx https://msdn.microsoft.com/en-us/library/hh769082(v=vs.85).aspx