| | 网站首页 | 资料中心 | 安盟论坛 | | |
![]() |
![]() |
| 您现在的位置: 安全联盟 >> 资料中心 >> 编程开发 >> 文章正文 |
|
|||||
| 使用c#捕获windows的关机事件 | |||||
作者:天 文章来源:本站原创 点击数: 更新时间:2007-6-11 ![]() |
|||||
|
关键代码如下: using System; using System.Collections.Generic; using System.Windows.Forms; using Microsoft.Win32; namespace Shutdown { static class Program { /**//// /// 应用程序的主入口点。 /// [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); } } } 此程序在使用c#2.0在Windows2003下测试通过。大家在使用SystemEvents.SessionEnding事件时切记要在程序退出时移除事件。 不过有两点遗憾之处: 1. 使用这种方式不能捕获休眠时的事件 2. 这个程序占用的内存太多了,只有这么一个小功能居然占了12M的内存 |
|||||
| 文章录入:admin 责任编辑:admin | |||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | |||||
| 友情链接 | ||||||||||||||
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | | |
![]() |
Copyright © 2006-2007 安全联盟, All Rights Reserved 站长:天 |