国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用c#捕獲windows的關機事件

2019-11-18 16:34:49
字體:
來源:轉載
供稿:網友

在公司上班,下班時需要簽退,而我呢隔三差五就會忘那么一次。怎么辦呢,于是就想能不能捕獲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惹的貨;實在是不可思議。

大家有沒有什么好主意可以克服這兩個缺點呢?
下載源文件


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 确山县| 迭部县| 桐柏县| 小金县| 邹平县| 扬州市| 西平县| 南靖县| 大埔县| 佛教| 铜山县| 搜索| 高淳县| 徐汇区| 定结县| 金坛市| 青冈县| 哈巴河县| 长宁区| 东宁县| 北票市| 德钦县| 冀州市| 丹巴县| 大港区| 屯门区| 洪江市| 灵山县| 望都县| 格尔木市| 合水县| 武汉市| 榆林市| 汤阴县| 汉沽区| 广南县| 星子县| 万宁市| 都江堰市| 苏州市| 泰来县|