Windows 7 Power Management

Source: Internet
Author: User

The previous article introduced how to manage network resources in Windows 7. In addition, the Windows API Code Pack also provides power management functions. This article will continue to demonstrate how to perform Power Management in Windows 7 through simple instances.

First, add TabControl and TabItem for The XAML code section to display the power status information.

<Grid>    <TabControl x:Name="tabControl">        <TabItem x:Name="tabItem" Header="Power Management Info">        </TabItem>    </TabControl></Grid>

The AddProperty () method in the previous example can be used to add relevant property information. Create LoadPowerMgmt () method and obtain power status data through the PowerManager class.

private void LoadPowerMgmt(){    StackPanel stackPanel = new StackPanel();    stackPanel.Orientation = Orientation.Vertical;    AddProperty("Power Personality: ", PowerManager.PowerPersonality.ToString(), stackPanel);    AddProperty("Power Source: ", PowerManager.PowerSource.ToString(), stackPanel);    AddProperty("Is Battery Persent: ", PowerManager.IsBatteryPresent.ToString(), stackPanel);    AddProperty("Is UPS Present: ", PowerManager.IsUpsPresent.ToString(), stackPanel);    AddProperty("Is Battery Short Term: ", PowerManager.IsBatteryShortTerm.ToString(), stackPanel);    AddProperty("Battery Life (%) : ", PowerManager.BatteryLifePercent.ToString(), stackPanel);    AddProperty("Is Monitor Required: ", PowerManager.MonitorRequired.ToString(), stackPanel);    AddProperty("Is Monitor On: ", PowerManager.IsMonitorOn.ToString(), stackPanel);       tabItem.Content = stackPanel;}

Call the LoadPowerMgmt () method in MainWindow () to automatically load the program. In this way, we can see the power details obtained by the LoadPowerMgmt () method in the Tab.

public MainWindow(){    InitializeComponent();    LoadPowerMgmt();}

In addition to obtaining the above static data, you can also add dynamic power events for the program. The following code adds the BatteryLifePercentChanged event to Window_Loaded. When the battery power changes, the related event is triggered.

private void Window_Loaded(object sender, RoutedEventArgs e){    PowerManager.BatteryLifePercentChanged += new EventHandler(BatteryLifePercentChanged);        }  

Add the event content to BatteryLifePercentChanged. When the BatteryLifePercentChanged, modify the progress bar and Label value. For other power events, see PowerSourceChanged, SystemBusyChanged, PowerPersonalityChanged, and IsMonitorOnChanged.

private void BatteryLifePercentChanged(object sender, EventArgs e){    powerBar.Value = PowerManager.BatteryLifePercent;    powerVal.Content = string.Format("{0}%", PowerManager.BatteryLifePercent.ToString());}

Run the program:

Source code download

PowerMgmt.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.