using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
static class Program
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormShutdown formShutdown = new FormShutdown();
SystemEvents.SessionEnding += new SessionEndingEventHandler(formShutdown.SystemEvents_SessionEnding);
Application.Run(formShutdown);
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
static class Program
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormShutdown formShutdown = new FormShutdown();
SystemEvents.SessionEnding += new SessionEndingEventHandler(formShutdown.SystemEvents_SessionEnding);
Application.Run(formShutdown);
}
}
}
Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
public partial class FormShutdown : Form
{
const string MESSAGE_TXT = "您签退了吗?";
const string MESSAGE_TITLE = "提示";
public FormShutdown()
{
InitializeComponent();
}
internal void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
DialogResult result = MessageBox.Show(MESSAGE_TXT, MESSAGE_TITLE, MessageBoxButtons.YesNo);
e.Cancel = (result == DialogResult.No);
}
private void FormShutdown_Load(object sender, EventArgs e)
{
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - 200, 0);
}
protected override void OnClosed(EventArgs e)
{
SystemEvents.SessionEnding -= new SessionEndingEventHandler(this.SystemEvents_SessionEnding);
base.OnClosed(e);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Shutdown
{
public partial class FormShutdown : Form
{
const string MESSAGE_TXT = "您签退了吗?";
const string MESSAGE_TITLE = "提示";
public FormShutdown()
{
InitializeComponent();
}
internal void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
DialogResult result = MessageBox.Show(MESSAGE_TXT, MESSAGE_TITLE, MessageBoxButtons.YesNo);
e.Cancel = (result == DialogResult.No);
}
private void FormShutdown_Load(object sender, EventArgs e)
{
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - 200, 0);
}
protected override void OnClosed(EventArgs e)
{
SystemEvents.SessionEnding -= new SessionEndingEventHandler(this.SystemEvents_SessionEnding);
base.OnClosed(e);
}
}
}
注意:
1、控制台应用程序不会引发 SessionEnding 事件。
2、因为这是一个静态事件,所以释放应用程序时必须分离事件处理程序,否则会导致内存泄漏。
参照
http://www.cnblogs.com/yukaizhao/archive/2007/04/23/csharp_catch_shutdown_events.html
http://msdn.microsoft.com/zh-cn/library/microsoft.win32.systemevents.sessionending(VS.85).aspx