/// <summary> /// This method is called when a Windows message /// is sent to this window. /// The method calls the registered event handlers. /// </summary> protected override void WndProc(ref Message m) { // Make sure it is a Power Management message. if (m.Msg == PowerManagementNativeMethods.PowerBroadcastMessage && (int)m.WParam == PowerManagementNativeMethods.PowerSettingChangeMessage) { PowerManagementNativeMethods.PowerBroadcastSetting ps = (PowerManagementNativeMethods.PowerBroadcastSetting)Marshal.PtrToStructure( m.LParam, typeof(PowerManagementNativeMethods.PowerBroadcastSetting)); IntPtr pData = new IntPtr(m.LParam.ToInt64() + Marshal.SizeOf(ps)); Guid currentEvent = ps.PowerSetting; // IsMonitorOn if (ps.PowerSetting == EventManager.MonitorPowerStatus && ps.DataLength == Marshal.SizeOf(typeof(Int32))) { Int32 monitorStatus = (Int32)Marshal.PtrToStructure(pData, typeof(Int32)); PowerManager.IsMonitorOn = monitorStatus != 0; EventManager.monitorOnReset.Set(); } if (!EventManager.IsMessageCaught(currentEvent)) { ExecuteEvents((ArrayList)eventList[currentEvent]); } } else { base.WndProc(ref m); } }
/// <summary> /// This method is called when a Windows message /// is sent to this window. /// The method calls the registered event handlers. /// </summary> protected override void WndProc(ref Message m) { // Make sure it is a Power Management message. if (m.Msg == PowerManagementNativeMethods.WM_POWERBROADCAST && (int)m.WParam == PowerManagementNativeMethods.PBT_POWERSETTINGCHANGE) { PowerManagementNativeMethods.PowerBroadcastSetting ps = (PowerManagementNativeMethods.PowerBroadcastSetting)Marshal.PtrToStructure( m.LParam, typeof(PowerManagementNativeMethods.PowerBroadcastSetting)); IntPtr pData = new IntPtr(m.LParam.ToInt64() + Marshal.SizeOf(ps)); Guid currentEvent = ps.PowerSetting; // Update the appropriate Property. // Power Personality if (ps.PowerSetting == EventManager.PowerPersonalityChange && ps.DataLength == Marshal.SizeOf(typeof(Guid))) { Guid newPersonality = (Guid)Marshal.PtrToStructure(pData, typeof(Guid)); PowerManager.powerPersonality = PersonalityGuids.GuidToEnum(newPersonality); // Tell PowerManager that is now safe to // read the powerPersonality member. EventManager.personalityReset.Set(); } // Power Source else if (ps.PowerSetting == EventManager.PowerSourceChange && ps.DataLength == Marshal.SizeOf(typeof(Int32))) { Int32 powerSrc = (Int32)Marshal.PtrToStructure(pData, typeof(Int32)); PowerManager.powerSource = (PowerSource)powerSrc; EventManager.powerSrcReset.Set(); } // Battery capacity else if (ps.PowerSetting == EventManager.BatteryCapacityChange && ps.DataLength == Marshal.SizeOf(typeof(Int32))) { Int32 battCapacity = (Int32)Marshal.PtrToStructure(pData, typeof(Int32)); PowerManager.batteryLifePercent = battCapacity; EventManager.batteryLifeReset.Set(); } // IsMonitorOn else if (ps.PowerSetting == EventManager.MonitorPowerStatus && ps.DataLength == Marshal.SizeOf(typeof(Int32))) { Int32 monitorStatus = (Int32)Marshal.PtrToStructure(pData, typeof(Int32)); PowerManager.isMonitorOn = monitorStatus == 0 ? false : true; EventManager.monitorOnReset.Set(); } if (!EventManager.IsMessageCaught(currentEvent)) { ExecuteEvents((ArrayList)eventList[currentEvent]); } } else { base.WndProc(ref m); } }