Cliquez pour évaluer et commenter
MSDN
MSDN Library
Développement .NET
Versions précédentes
.NET Framework 3.0
Windows Forms
 Gestion de l'alimentation dans Wind...
Cette page est spécifique à
.NET Framework 3.0

D'autres versions sont également disponibles pour :
Programmation Windows Forms
Gestion de l'alimentation dans Windows Forms

Your Windows Forms applications can take advantage of the power management features in the Windows operating system. Your applications can monitor the power status of a computer and take action when a status change occurs. For example, if your application is running on a portable computer, you might want to disable certain features in your application when the computer's battery charge falls under a certain level.

The .NET Framework provides a PowerModeChanged event that occurs whenever there is a change in power status, such as when a user suspends or resumes the operating system, or when the AC power status or battery status changes. The PowerStatus property of the SystemInformation class can be used to query for the current status, as shown in the following code example.

Visual Basic
Private Sub PowerModeChanged(ByVal Sender As System.Object, ByVal e As _
Microsoft.Win32.PowerModeChangedEventArgs)
    Dim si As System.Windows.Forms.SystemInformation
    Select Case si.PowerStatus.BatteryChargeStatus
        Case BatteryChargeStatus.Low
            MessageBox.Show("Battery is running low", _ System.Windows.Forms.MessageBoxIcon.Exclamation)
        Case BatteryChargeStatus.Critical
            MessageBox.Show("Battery is critically low", _ System.Windows.Forms.MessageBoxIcon.Stop)
        Case Else
            ' Battery is okay.
            Exit Select
    End Select
End Sub
C#
private void powerModeChanged(System.Object sender, _
Microsoft.Win32.PowerModeChangedEventArgs e)
{
    int si = SystemInformation.PowerStatus;
    switch (si)
        {
        case BatteryChargeStatus.Low:
            MessageBox.Show("Battery is running low", _ MessageBoxIcon.Exclamation);
        case BatteryChargeStatus.Low:
            MessageBox.Show("Battery is critcally low", _ MessageBoxIcon.Stop);
        Default:
            // Battery is okay.
        }
}

Besides the BatteryChargeStatus enumerations, the PowerStatus property also contains enumerations for determining battery capacity (BatteryFullLifeTime) and battery charge percentage (BatteryLifePercent, BatteryLifeRemaining).

You can use the SetSuspendState method of the Application to put a computer into hibernation or suspend mode. If the force argument is set to false, the operating system will broadcast an event to all applications requesting permission to suspend. If the disableWakeEvent argument is set to true, the operating system disables all wake events.

The following code example demonstrates how to put a computer into hibernation.

Visual Basic
Private si as System.Windows.Forms.SystemInformation
If si.PowerStatus.BatteryChargeStatus.Critical = True Then
    Application.SetSuspendState(PowerState.Hibernate, False, False)
End If
C#
if (SystemInformation.PowerStatus.BatteryChargeStatus.ToString() == _
"Critical")
    {
        Application.SetSuspenState (PowerState.Hibernate, false, false);
    }

Voir aussi

Contenu de la communauté   Qu'est-ce que le Contenu de la communauté ?
Ajouter du contenu RSS  Annotations
Processing
© 2009 Microsoft Corporation. Tous droits réservés. Conditions d'utilisation | Marques | Confidentialité
Page view tracker