6

I have a method to determine battery level and kept a timer to periodically check the battery level.

   void GetBatteryLevel()
    {
        try
        {
            //Use this code in next build
            //System.Windows.Forms.PowerStatus status = System.Windows.Forms.SystemInformation.PowerStatus;

            double batteryLevel;
            ManagementObjectSearcher mos = new ManagementObjectSearcher("select EstimatedChargeRemaining from Win32_Battery");
            ManagementObjectCollection collection = mos.Get();
            foreach (ManagementObject mo in collection)
            {
                UInt16 remainingPercent = (UInt16)mo["EstimatedChargeRemaining"];
                batteryLevel            = (double)remainingPercent;
                batteryLevel            = batteryLevel / 100;
                batteryLevel            = 1.0 - batteryLevel;
                BatteryLevel            = new System.Windows.Point(0.0, batteryLevel);
            }
        }
        catch (Exception exp)
        {
            Logger.LogMessage("EXCEPTION: " + exp.StackTrace);
        }
    }

Is there a way to register for events when battery level drops or increases by 1%? I have already registered for SystemEvents.PowerModeChanged and that works fine.

4

1 回答 1

2

简短的回答来自 .Net 基类库号。

话虽如此,有可用的电源事件,但这些事件驻留在 kernel32 中。Microsoft 确实试图在 SystemEvents 类中为您提供一些此类事件的挂钩,但不幸​​的是它们并没有与所有这些事件相关联。查看电源管理 API 并没有以您想要的方式跟踪电池的事件。

电源事件: http: //msdn.microsoft.com/en-us/library/windows/desktop/aa373162 (v=vs.85).aspx

电源管理服务有一个名为 GetSystemPowerStatus 的方法,可以为您提供所需的信息。如果您了解在 .Net 中使用 C++,您可以查询电池信息并在电池发生变化时触发您自己的事件。

GetSystemPowerStatus: http: //msdn.microsoft.com/en-us/library/windows/desktop/aa372693 (v=vs.85).aspx

于 2013-09-18T15:55:29.690 回答