解決辦法:在MainForm中重寫Windows消息處理方法
示例:
using System;
using System.Windows.Forms;
using Microsoft.Win32;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
private const UInt32 WM_ENDSESSION = 0x0016;
public MainForm()
{
InitializeComponent();
}
protected override void WndProc(ref Message message)
{
if (WM_ENDSESSION == message.Msg)
{
Application.Exit();
}
base.WndProc(ref message);
}
}
}