gpt4 book ai didi

c++ - 电源按钮和盖子 sleep 定制

转载 作者:行者123 更新时间:2023-11-28 07:11:11 25 4
gpt4 key购买 nike

据我所知,Windows 控制面板中只有 sleep /休眠和关机选项可用于响应电源按钮和笔记本电脑盖子关闭:

enter image description here

我编写了一个 Windows 服务,我很好奇是否可以自定义这些选项?主要是我需要能够在让系统进入休眠状态之前注销 Windows 用户。

最佳答案

好的,必须回答我自己的问题。这是我能够找到的:

很容易知道盖子关闭时将执行什么电源操作。这是代码(它必须从用户模式进程运行):

void getLidClosedAction()
{
GUID* pGuidActivePwrSchm = NULL;
DWORD dwR = PowerGetActiveScheme(NULL, &pGuidActivePwrSchm);
if(dwR == ERROR_SUCCESS)
{
DWORD val;

val = -1;
dwR = PowerReadACValueIndex(NULL, pGuidActivePwrSchm, &GUID_SYSTEM_BUTTON_SUBGROUP, &GUID_LIDCLOSE_ACTION, &val);
if(dwR == ERROR_SUCCESS)
{
_tprintf(L"Lid closed action: ");

switch(val)
{
case 0:
_tprintf(L"Do nothing\n");
break;
case 1:
_tprintf(L"Sleep\n");
break;
case 2:
_tprintf(L"Hibernate\n");
break;
case 3:
_tprintf(L"Shut-down\n");
break;
default:
_tprintf(L"Unknown value=%d\n", val);
break;
}
}
else
{
_tprintf(L"PowerReadACValueIndex error=%d\n", dwR);
}


if(pGuidActivePwrSchm)
{
LocalFree(pGuidActivePwrSchm);
pGuidActivePwrSchm = NULL;
}
}
else
{
_tprintf(L"PowerGetActiveScheme error=%d\n", dwR);
}

}

并且在盖子关闭时设置电源操作也很容易(同样,代码必须在用户模式进程中运行——否则您将需要使用 以外的方式获取电源方案 GUID PowerGetActiveScheme 调用):

BOOL setLidClosedAction(DWORD dwVal)
{
//'dwVal' = can be one of:
// 0 = do nothing
// 1 = sleep
// 2 = hibernate
// 3 = shut-down
BOOL bRes = FALSE;

GUID* pGuidActivePwrSchm = NULL;
DWORD dwR = PowerGetActiveScheme(NULL, &pGuidActivePwrSchm);
if(dwR == ERROR_SUCCESS)
{
dwR = PowerWriteACValueIndex(NULL, pGuidActivePwrSchm, &GUID_SYSTEM_BUTTON_SUBGROUP, &GUID_LIDCLOSE_ACTION, dwVal);
if(dwR == ERROR_SUCCESS)
{
bRes = TRUE;
}
else
{
_tprintf(L"PowerWriteACValueIndex error=%d\n", dwR);
}

if(pGuidActivePwrSchm)
{
LocalFree(pGuidActivePwrSchm);
pGuidActivePwrSchm = NULL;
}
}
else
{
_tprintf(L"PowerGetActiveScheme error=%d\n", dwR);
}

return bRes;
}

这也适用于电源/ sleep 按钮操作。 GUID_SYSTEM_BUTTON_SUBGROUP 中的 GUID 如下:

GUID_POWERBUTTON_ACTION = power button
GUID_SLEEPBUTTON_ACTION = sleep button
GUID_USERINTERFACEBUTTON_ACTION = sometimes another OEM sleep button

不幸的是,我无法看到如何为这些事件设置自定义电源操作。

关于c++ - 电源按钮和盖子 sleep 定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20948220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com