[C#]偵測系統Power狀態的改變以及是否進入Sleep mode
namespace WindowsFormsApplication15 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
}
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
textBox1.Text += e.Mode.ToString() + Environment.NewLine;
}
}
}
namespace WindowsFormsApplication15 { public partial class Form1 : Form { private const int WM_POWERBROADCAST = 0x218; private const int PBT_APMSUSPEND = 0x4; private const int PBT_APMRESUMESUSPEND = 0x7; private const int PBT_APMRESUMEAUTOMATIC = 0x12; public Form1() { InitializeComponent(); }
protected override void WndProc(ref Message m)
{
if(m.Msg == WM_POWERBROADCAST)
{
switch ((int)m.WParam)
{
case PBT_APMSUSPEND:
textBox1.Text += "PBT_APMSUSPEND" + Environment.NewLine;
break;
case PBT_APMRESUMEAUTOMATIC:
textBox1.Text += "PBT_APMRESUMEAUTOMATIC" + Environment.NewLine;
break;
case PBT_APMRESUMESUSPEND:
textBox1.Text += "PBT_APMRESUMESUSPEND" + Environment.NewLine;
break;
}
}
base.WndProc(ref m);
}
}
}