黄季冬=>fox23

Freesc Huang & Smart Devices
数据加载中……
[CF.Skills]获取系统电源状态
 今天在Peter Foot博客上看到如何获取系统的电源状态(不是电池状态)的一篇文章,
他使用的VB描述:

<DllImport("coredll.dll")> _
PublicSharedFunction GetSystemPowerState(
ByVal pBuffer As System.Text.StringBuilder, ByVal Length AsInteger, ByRef pFlags As PowerState) AsInteger
EndFunction

<Flags()> _
PublicEnum PowerState
[
On= &H10000 '// on state
Off= &H20000 ' // no power, full off
Critical = &H40000 '// critical off
Boot = &H80000 ' // boot state
Idle = &H100000 ' // idle state
Suspend = &H200000 ' // suspend state
Unattended = &H400000 ' // Unattended state.
Reset= &H800000 ' // reset state
UserIdle = &H1000000 ' // user idle state
BackLightOn = &H2000000 ' // device screen backlight on
Password = &H10000000 ' // This state is password protected.
EndEnum


PrivateSub Button1_Click(
ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim sb AsNew System.Text.StringBuilder(260)
Dim flags As PowerState = 0
Dim ret AsInteger= GetSystemPowerState(sb, sb.Capacity, flags)

TextBox1.Text 
= sb.ToString()
TextBox2.Text 
= flags.ToString()
EndSub 

我写了一个C#的版本:

        private void menuPowerState_Click(object sender, EventArgs e)
        
{
            StringBuilder sb 
= new StringBuilder(32);
            MyRef.GetSysPowerState(sb, 
32);
        }

    [Flags]
    
public enum PowerState:uint
    
{
        
//更多的参数在Pm.h中
        POWER_STATE_ON = 0x00010000,
        POWER_STATE_OFF 
= 0x00020000,
        POWER_STATE_CRITICAL 
= 0x00040000,
        POWER_STATE_BOOT 
= 0x00080000,
        POWER_STATE_IDLE 
= 0x00100000,
        POWER_STATE_SUSPEND 
= 0x00200000,
        POWER_STATE_RESUME 
= 0x00400000,
        POWER_STATE_RESET 
= 0x00800000,
    }

    
class MyRef
    
{
        
private static PowerState state = 0;
        [DllImport(
"Coredll.dll", SetLastError = true)]
        
public static extern void GetSystemPowerState(StringBuilder sb, uint length, ref PowerState ps);

        
public static void GetSysPowerState(StringBuilder sb, uint length)
        
{
            GetSystemPowerState(sb, length, 
ref state);
            MessageBox.Show(String.Format(
"Is devive power on? {0}",(state&PowerState.POWER_STATE_ON)==PowerState.POWER_STATE_ON));
        }

    }

效果如下:

相当easy~

Enjoy it~

©Freesc Huang
  黄季冬<fox23>@HUST

posted on 2008-03-09 11:31 Freesc Huang 阅读(773) 评论(6)  编辑 收藏 网摘 所属分类: Compact Framework

评论

#1楼 2008-03-10 09:44 下风      

学习,请问下Pm.h这个文件具体在什么位置呢?
    回复  引用  查看    

#2楼[楼主] 2008-03-10 10:15 fox23      

@下风
Coredll.lib
    回复  引用  查看    

#3楼 2008-04-05 17:27 如影如风      

能不能解释一下各个状态的具体含义呢?
    回复  引用  查看    

#4楼 2008-11-14 14:29 Mobile 6[未注册用户]

能够实现真正的断电关机吗?好像POWER_STATE_OFF和POWER_STATE_SUSPEND都是挂起,并没有真正关机啊?
    回复  引用    

#5楼[楼主] 2008-11-14 22:48 fox23      

@Mobile 6
电源方面,特别是关机相关都没有通用标准的实现,通常只是让系统Suspend
    回复  引用  查看    



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1097190




相关文章:

相关链接: