草原上的野狼

啸苍天,没日月,孤单影只; 忍地寒,耐绝境,经熔炼,将成大业!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::


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);
        }


    }

}

 


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 - 2000);
        }


        
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 

 

 

posted on 2008-07-30 17:27  血狼  阅读(1418)  评论(0编辑  收藏  举报