网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.C#中使用TextBox控件的两个问题.
.C#中为DataGrid添加下拉列表框.
.如何在控件的设计时得到窗体设计.
.用Visual C#来清空回收站(1).
.实用技巧:.Net框架类库中定时器.
.论C#变得越来越臃肿是不可避免的.
.C#结合串口通信类实现串口通信源.
.c#远程获取网页内容.
.给大家一个新的加密方法,C#的.
.信息反馈-邮件(数据库是XML) .
.利用正则表达式来反转一句话,以单.
.C#操作技巧的数据类型之间的转换.
.用Visual C#中轻松浏览数据库记.
.C#算法设计与分析-寻找素数.
.如何把html中的相对路径变成绝对.
.C#可逆加密算法收集.
.c# 正确读取存储中文,以及如何获.
.C#模拟MSN窗体抖动.
.用浏览器来接收C# 的程序返回的时.
.C#实现SMTP服务器,使用TCP命令实.

使用c#捕获windows的关机事件

发表日期:2007-4-24


在公司上班,下班时需要签退,而我呢隔三差五就会忘那么一次。怎么办呢,于是就想能不能捕获windows的关机事件,做一个程序让它在关机的时候提醒我一下呢。

非常幸运很容易就找到了Microsoft.Win32命名空间下面的SystemEvents类,他有一个静态的事件SessionEnding在系统注销或者关机时发生,此事件只有在winform的程序下有效,而在控制台程序下面无效,不能激发事件;还有一点我们必须在程序推出时将加上的事件移除掉,否则就容易造成内存溢出。

关键代码如下:


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 - 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的内存,这都是.Net framework惹的货;实在是不可思议。

大家有没有什么好主意可以克服这两个缺点呢?
下载源文件

上一篇:C#图像放大问题解决方法 人气:5444
下一篇:关于正则表达式匹配无异常资源耗尽的解决方案 人气:3872
浏览全部c#的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐