新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
如何使用C++制作Windows的關機事件
下面研究在C++里,使用C++捕獲windows的關機事件,看看C++是否可以做一個程序,能讓它在關機的時候提醒我一下呢,這樣就不會在有的文件沒保存下的情況下,關機導致的損失了。

站在用戶的角度思考問題,與客戶深入溝通,找到孟州網站設計與孟州網站推廣的解決方案,憑借多年的經驗,讓設計與互聯(lián)網技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網站制作、成都做網站、企業(yè)官網、英文網站、手機端網站、網站推廣、國際域名空間、網站空間、企業(yè)郵箱。業(yè)務覆蓋孟州地區(qū)。
非常幸運很容易就找到了Microsoft.Win32命名空間下面的SystemEvents類,他有一個靜態(tài)的事件SessionEnding在系統(tǒng)注銷或者關機時發(fā)生,此事件只有在winform的程序下有效,而在控制臺程序下面無效,不能激發(fā)事件;還有一點我們必須在程序推出時將加上的事件移除掉,否則就容易造成內存溢出。
關鍵代碼如下:
- 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();
- }
此程序在使用C++在Windows2003下測試通過。大家在使用SystemEvents.SessionEnding事件時切記要在程序退出時移除事件。
不過有兩點遺憾之處:
1. 使用這種方式不能捕獲休眠時的事件
2. 這個程序占用的內存太多了,只有這么一個小功能居然占了12M的內存,這都是.Net framework惹的貨;實在是不可思議。
大家有沒有什么好主意可以克服這兩個缺點呢?
網站標題:如何使用C++制作Windows的關機事件
轉載來源:http://m.5511xx.com/article/codegpj.html


咨詢
建站咨詢
