[CF.Skills]擷取系統電源狀態

來源:互聯網
上載者:User
 今天在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.