获取当前笔记本电脑盖状态 [英] Get current laptop lid state

查看:589
本文介绍了获取当前笔记本电脑盖状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个C#应用程序(专门用于笔记本电脑),我想知道盖子的状态,即何时打开,何时关闭。

I'm writing a C# app (specifically for laptops) and I want to be aware of the state of the lid, i.e. when is it open and when is it closed.

我已经使用pInvoke以及 Microsoft的RegisterPowerSettingNotification函数,借助此SO回答< a>。

I've already used pInvoke along with Microsoft's RegisterPowerSettingNotification function with the help of this SO answer.

所以,上面我知道当盖子被关闭或打开并知道它的状态,
但是我想得到状态在任何其他事情之前的盖子继续。
我注意到,即使盖子没有改变,回调函数被调用,我可以检查它的参数知道盖子的状态,但这对我来说不好,因为它是一个事件,我可以不是简单地等待它发生(也许有另一种方式来解决这个问题?)。

So, with the above I know when the lid is being closed or opened and know its state, BUT I want to get the state of the lid before anything else continues. I've noticed that even if there is no change to the lid, the callback function is called and I can check its parameter to know the lid's state, but this not good for me since it is an event and I can't simply wait for it to occur (maybe there's another way to work around this?).

我也试过PowerEnumerate函数,但无法从中得到一些东西(也许有一些我不知道有什么可以帮助)

I've also tried PowerEnumerate function but couldn't get something from it (maybe there is something I don't know there that could help)

编辑1:
我不能等待或延迟应用程序,有两个问题需要我获得没有CB的状态,第一个是盖子在膝上型计算机连接到监视器时闭合,从而允许用户使用膝上型计算机。如果用户在这种情况下启动应用程序,它不应该启动。
第二个原因是我想让应用程序是用户友好的,并通知当启动或停止时会发生什么,如果我在错误的时间获取cb,它可能会被忽略,由于应用程序的内部逻辑,然后将不得不等到下一次我得到一个cb,我会在那里。

EDIT 1: I can't wait or delay the app, and there are 2 issues that require me to get the state without a CB, the first is that a lid can be closed while the laptop is connected to a monitor thus allowing the user to use the laptop. If the user starts the app in this situation it should not start. The second reason is that I want the app to be user friendly and notify what happens when it starts or stops, and if I get the cb in the wrong time it might be ignored, due to the app's inner logic, and then will have to wait until the next time I get a cb, I'd at all.

所以,(不是双关语)
有办法马上获取这些信息吗?

So, (no pun intended) Is there a way to get this information right away?

编辑2:
从评论似乎不清楚我需要什么。我需要一种查询盖子状态的方法。即 - 调用一些API或某事,返回值是状态。
感谢!

EDIT 2: It seems from comments that it's not clear what I need. I need a way to query to state of the lid. i.e. - call some API or something and that the return value is the state. thanks!

关键字:GUID_LIDSWITCH_STATE_CHANGE,WM_POWERBROADCAST,电源设置

Keywords: GUID_LIDSWITCH_STATE_CHANGE , WM_POWERBROADCAST, Power Setting

推荐答案

所以经过长时间的搜索,由@ Llwyd的答案,我找到了一个解决方案。
我注意到,当盖子关闭时,设备从设备管理器中删除,所以解决方案是检查它是否存在。
它的代码是一个有点大量上传这里,所以我不会,但以下是需要:
1)使用 WmiMonitorConnectionParams标识内部屏幕的name\ID VideoOutputTechnology 和状态( DISPLAY_DEVICE_ATTACHED 和/或 DISPLAY_DEVICE_ACTIVE
2)一旦我知道设备的名称/ ID,我可以通过枚举System.Windows.Forms.Screen.AllScreens(或保存DISPLAY_DEVICE_ATTACHED的值)来方便地查找。

So after a long search led by @Llwyd's answer, I found a solution. I noticed that when the lid is closed the device is removed from the device manager, so the solution is to check whether it is there or not. The code behind it is a bit massive to upload here so I won't, but the following was need: 1) Identify Internal screen's name\ID with WmiMonitorConnectionParams and VideoOutputTechnology and state (DISPLAY_DEVICE_ATTACHED and/or DISPLAY_DEVICE_ACTIVE) 2) Once I know the device's name/ID I can easily look it up by enumerating System.Windows.Forms.Screen.AllScreens (or save the value from the DISPLAY_DEVICE_ATTACHED)

这篇关于获取当前笔记本电脑盖状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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