我将form1.cs逐行解读,并根据情况加了中文的注释,你可以将其替换你以前下载的源文件中的form1.cs文件,其他文件不要改动。
在这儿可以下载,加了注释的form1.cs
http://metababy.chinay.com/sp/metababy/16272452560.cs
以下是代码:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Timers; using System.Diagnostics; namespace PopupKiller { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListView listView1; private System.Windows.Forms.ColumnHeader columnHeader1;//在 ListView 控件中显示一个列标头。 private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenu contextMenu_WebPages;//ContextMenu 类表示当用户在控件或窗体的特定区域上单击鼠标右键时会显示的快捷菜单。 private System.Windows.Forms.ContextMenu contextMenu_App; private System.Windows.Forms.MenuItem App_Hide;//表示在 MainMenu 或 ContextMenu 内显示的单个项。 private System.Windows.Forms.MenuItem App_Show; private System.Windows.Forms.MenuItem App_Exit; private System.Windows.Forms.MenuItem App_About; private System.Windows.Forms.MenuItem App_Save; private System.Windows.Forms.MenuItem WebPages_Add; private System.Windows.Forms.MenuItem WebPages_Edit; private System.Windows.Forms.MenuItem WebPages_Remove; private System.Windows.Forms.MenuItem separator; private System.ComponentModel.IContainer components;//提供容器的功能。容器是在逻辑上包含零个或更多个组件的对象。 // private System.Timers.Timer m_Timer;//在应用程序中生成定期事件。 /* Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发 Elapsed 事件的周期性间隔。然后可以操控此事件以提供定期处理。例如,假设您有一台关键性服务器,必须每周 7 天、每天 24 小时都保持运行。可以创建一个使用 Timer 的服务,以定期检查服务器并确保系统开启并在运行。如果系统不响应,则该服务可以尝试重新启动服务器或通知管理员。 基于服务器的 Timer 是为在多线程环境中用于辅助线程而设计的。服务器计时器可以在线程间移动来处理引发的 Elapsed 事件,这样就可以比 Windows 计时器更精确地按时引发事件。 */ private int m_nInterval;//申明时间间隔变量 private System.Windows.Forms.MenuItem App_HotKey; //右击弹出菜单的热键项(hotkey) // const protected const String m_sFilename = "PopupKiller.log.xml";//定义字符串常量"m_sFilename"的值为"PopupKiller.log.xml" protected Keys m_hotkey;//记录热键的键值的变量 protected bool m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey;//bool(布尔值,只能取真或假),这儿定义后面三个变量为布尔型的。 public Form1() { // // 需要设计器支持(这句不管,是系统自动生成的,我只是将英文翻译成中文而已) // InitializeComponent(); // 注册默认热键 (重载 .xml 文件中的 <hotkey...>...</hotkey>) // m_hotkey = Keys.J; // 默认的热键是 Ctrl+Shift+J m_ctrlhotkey = true;//允许ctrl键 m_shifthotkey = true;//允许shiftl键 m_althotkey = false;//不允许shiftl键 m_winhotkey = false;//不允许shiftl键 SetHotKey(m_hotkey, m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey);//设置新的热键 // 载入XML文件,并将相关项显示在listview列表中 // LoadPages(); // 开启timer(计时器)规则检测 // m_Timer = new System.Timers.Timer(); // 直接给出名称空间是因为源文件头几行,并没有引用System.Timers,在这里须指明 m_Timer.Elapsed += new ElapsedEventHandler(OnTimerKillPopup);/* ElapsedEventHandler 委托 表示将处理 Timer 的 Elapsed 事件的方法。 [C#] [Serializable] public delegate void ElapsedEventHandler( object sender, ElapsedEventArgs e ); 参数 事件处理程序的声明必须与 ElapsedEventHandler 委托声明具有相同的参数。 sender 事件的源。 e 包含事件数据的 ElapsedEventArgs 对象。 备注 创建 ElapsedEventHandler 委托时,标识将处理该事件的方法。若要使该事件与事件处理程序关联,请将该委托的一个实例添加到该事件。每当事件出现时就调用事件处理程序,除非移除了该委托。 */ Interval = 3000; // 间隔设为3秒(即3000毫秒) EnableTimer(); } /// <summary> /// 清除所有用过的资源(系统自动生成,不要管这句) /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// 这是系统自动生成的,提示说,不要修改任何代码 /// 不要管这句。下面这些看着头晕的代码段是FORM介面,用不着深究,有兴趣,看看就行了。意思就是说在什么位置放置什么样的按钮,文本之类的。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader(); this.listView1 = new System.Windows.Forms.ListView(); this.columnHeader1 = new System.Windows.Forms.ColumnHeader(); this.contextMenu_WebPages = new System.Windows.Forms.ContextMenu(); this.WebPages_Add = new System.Windows.Forms.MenuItem(); this.WebPages_Edit = new System.Windows.Forms.MenuItem(); this.WebPages_Remove = new System.Windows.Forms.MenuItem(); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.contextMenu_App = new System.Windows.Forms.ContextMenu(); this.App_About = new System.Windows.Forms.MenuItem(); this.App_HotKey = new System.Windows.Forms.MenuItem(); this.App_Save = new System.Windows.Forms.MenuItem(); this.separator = new System.Windows.Forms.MenuItem(); this.App_Hide = new System.Windows.Forms.MenuItem(); this.App_Show = new System.Windows.Forms.MenuItem(); this.App_Exit = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // listView1 // this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader1}); this.listView1.ContextMenu = this.contextMenu_WebPages; this.listView1.Dock = System.Windows.Forms.DockStyle.Fill; this.listView1.LabelEdit = true; this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(240, 389); this.listView1.TabIndex = 0; this.listView1.View = System.Windows.Forms.View.Details; // // columnHeader1 // this.columnHeader1.Text = "Web pages"; this.columnHeader1.Width = 230; // // contextMenu_WebPages // this.contextMenu_WebPages.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.WebPages_Add, this.WebPages_Edit, this.WebPages_Remove}); this.contextMenu_WebPages.Popup += new System.EventHandler(this.menu_WebPages_BeforePopup); // // WebPages_Add // this.WebPages_Add.Index = 0; this.WebPages_Add.Text = "Add"; this.WebPages_Add.Click += new System.EventHandler(this.menu_WebPages_Add); // // WebPages_Edit // this.WebPages_Edit.Index = 1; this.WebPages_Edit.Text = "Edit"; this.WebPages_Edit.Click += new System.EventHandler(this.menu_WebPages_Edit); // // WebPages_Remove // this.WebPages_Remove.Index = 2; this.WebPages_Remove.Text = "Remove"; this.WebPages_Remove.Click += new System.EventHandler(this.menu_WebPages_Remove); // // notifyIcon1 // this.notifyIcon1.ContextMenu = this.contextMenu_App; this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); this.notifyIcon1.Text = ((string)(configurationAppSettings.GetValue("notifyIcon1.Text", typeof(string)))); this.notifyIcon1.Visible = true; this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick); // // contextMenu_App // this.contextMenu_App.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.App_About, this.App_HotKey, this.App_Save, this.separator, this.App_Hide, this.App_Show, this.App_Exit}); this.contextMenu_App.Popup += new System.EventHandler(this.menu_App_BeforePopup); // // App_About // this.App_About.Index = 0; this.App_About.Text = "About"; this.App_About.Click += new System.EventHandler(this.menu_App_About); // // App_HotKey // this.App_HotKey.Index = 1; this.App_HotKey.Text = "Hotkey"; this.App_HotKey.Click += new System.EventHandler(this.menu_App_HotKey); // // App_Save // this.App_Save.Index = 2; this.App_Save.Text = "Save"; this.App_Save.Click += new System.EventHandler(this.menu_App_Save); // // separator // this.separator.Index = 3; this.separator.Text = "-"; // // App_Hide // this.App_Hide.Index = 4; this.App_Hide.Text = "Hide"; this.App_Hide.Click += new System.EventHandler(this.menu_App_Hide); // // App_Show // this.App_Show.Index = 5; this.App_Show.Text = "Show"; this.App_Show.Click += new System.EventHandler(this.menu_App_Show); // // App_Exit // this.App_Exit.Index = 6; this.App_Exit.Text = "Exit"; this.App_Exit.Click += new System.EventHandler(this.menu_App_Exit); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(240, 389); this.ContextMenu = this.contextMenu_App; this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listView1}); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form1"; this.ShowInTaskbar = false; this.Text = "Popup Killer"; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序从这儿开始运行 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } public int Interval { get { return m_nInterval; } set { m_nInterval = value; m_Timer.Interval = m_nInterval; } } // 以下是右击弹出菜单的代码 // // private void menu_App_About(object sender, System.EventArgs e) { MessageBox.Show("Popup Killer. (c) SRodriguez 2002");//这一句是显示版权信息,可以随便改成一些有趣的话(当然,只能改"" 这间的话哈,比如改为"花纯春改编,改得乱惨了" } private void menu_App_Save(object sender, System.EventArgs e)//以下代码是为了写一个xml文件 { System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(m_sFilename, new System.Text.UTF8Encoding()); w.Formatting = System.Xml.Formatting.Indented; w.WriteStartDocument(); w.WriteStartElement("popupkiller"); // <popupkiller> foreach (ListViewItem item in listView1.Items) { w.WriteStartElement("page"); // <page> w.WriteString( item.Text ); w.WriteEndElement(); // </page> } w.WriteStartElement("hotkey"); // <hotkey> w.WriteAttributeString("ctrl",m_ctrlhotkey?"true":"false"); w.WriteAttributeString("shift",m_shifthotkey?"true":"false"); w.WriteAttributeString("alt",m_althotkey?"true":"false"); w.WriteAttributeString("windows",m_winhotkey?"true":"false"); int n = (int) m_hotkey; w.WriteString( n.ToString() ); w.WriteEndElement(); // </hotkey> w.WriteEndElement(); // </popupkiller> w.Close(); } public void LoadPages()//以下代码是为了读xml文件 { System.Xml.XmlTextReader reader = null; // 如果xml文件不存在,则按以下代码处理,以消除异常 if (!System.IO.File.Exists(m_sFilename)) return; try { //读取数据,忽略空白 reader = new System.Xml.XmlTextReader(m_sFilename); reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None; bool bEnablePageContent = false; // 用于过滤 <page>...</page> 内容 bool bEnableHotkeyContent = false; // 用于过滤 <hotkey ...>...</hotkey> 内容 //分析文件,并显示第一个节点 while (reader.Read()) { switch (reader.NodeType) { case System.Xml.XmlNodeType.Element: // 我们正在分析一个 <page> 元素吗 ? bEnablePageContent = (reader.Name=="page"); bEnableHotkeyContent = (reader.Name=="hotkey"); break; case System.Xml.XmlNodeType.Text: // 我们正在分析<page>元素,那就让我们将这个<page>加入listview列表吧 if (bEnablePageContent) listView1.Items.Add( reader.Value ); else if (bEnableHotkeyContent) { m_hotkey = (Keys) System.Int32.Parse(reader.Value); SetHotKey(m_hotkey, m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey); // 注册热键 } bEnablePageContent = false; bEnableHotkeyContent = false; break; case System.Xml.XmlNodeType.Attribute: if (bEnableHotkeyContent) { if (reader.Name=="ctrl") m_ctrlhotkey = (reader.Value=="true")?true:false; if (reader.Name=="shift") m_shifthotkey = (reader.Value=="true")?true:false; if (reader.Name=="alt") m_althotkey = (reader.Value=="true")?true:false; if (reader.Name=="windows") m_winhotkey = (reader.Value=="true")?true:false; } break; } } // while 语句到此结束 } catch( System.Xml.XmlException e) { e.ToString(); } finally { if (reader!=null) reader.Close(); } } private void menu_App_Hide(object sender, System.EventArgs e)//隐藏窗口 { this.WindowState = FormWindowState.Minimized; Hide(); } private void menu_App_Show(object sender, System.EventArgs e)//显示窗口 { Show(); this.WindowState = FormWindowState.Normal; } private void menu_App_Exit(object sender, System.EventArgs e)//退出程序 { // 清除注册过的热键 // NativeWIN32.UnregisterHotKey(Handle, 100); notifyIcon1.Visible = false; // 清除系统托盘上的图标 Application.Exit(); } // 事件未附上委托,(简单重载). protected override void OnClosing(CancelEventArgs e) { // 方法被重载了,所以,当窗口为可见状态时,点关闭按钮,窗口就最小化并隐藏了 if (this.Visible==true) { e.Cancel = true; // 最小化并隐藏窗口 this.WindowState = FormWindowState.Minimized; Hide(); } // 否则,关闭程序 } private void menu_App_BeforePopup(object sender, System.EventArgs e) { if ( this.WindowState == FormWindowState.Minimized ) { App_Show.Enabled = true; App_Hide.Enabled = false; } else { App_Show.Enabled = false; App_Hide.Enabled = true; } } // 双击系统托盘上的图标,显示窗口 private void notifyIcon1_DoubleClick(object sender, System.EventArgs e) { this.WindowState = FormWindowState.Normal; Show(); } // 列表的右击弹出菜单的具体实现代码 // // private void menu_WebPages_Add(object sender, System.EventArgs e) { listView1.Items.Add( "***EDIT PAGE***" ); } private void menu_WebPages_Edit(object sender, System.EventArgs e) { if ( listView1.SelectedItems.Count==1 ) listView1.SelectedItems[0].BeginEdit(); } private void menu_WebPages_Remove(object sender, System.EventArgs e) { ListView.SelectedListViewItemCollection selcoll = listView1.SelectedItems; while ( selcoll.Count>0 )// selcoll.Count的值是动态变化的,当列表项大于0时,即,新加有一项及以上的值时,删除默认的第一项 listView1.Items.Remove( selcoll[0] ); } private void menu_WebPages_BeforePopup(object sender, System.EventArgs e) { WebPages_Edit.Enabled = ( listView1.SelectedItems.Count==1 ); } // 记时器操作 // // protected void EnableTimer() { m_Timer.Enabled = true; } protected void DisableTimer() { m_Timer.Enabled = false; } protected bool IsTimerEnabled() { return m_Timer.Enabled; } protected void OnTimerKillPopup(Object source, ElapsedEventArgs e) { DisableTimer(); if (true) // 用 WIN32 FindWindowEx : 直接对所有IE窗口进行操作 FindPopupToKill(); else // 涉及到进程,线程,窗口 { Process[] myProcesses = Process.GetProcessesByName("IEXPLORE"); // 遍历所有IE进程 foreach(Process myProcess in myProcesses) { FindPopupToKill(myProcess); } } EnableTimer(); } protected void FindPopupToKill() { IntPtr hParent = IntPtr.Zero; IntPtr hNext = IntPtr.Zero; String sClassNameFilter = "IEFrame"; // 所有Internet Explorer窗口的 CLASSNAME do { hNext = NativeWIN32.FindWindowEx(hParent,hNext,sClassNameFilter,IntPtr.Zero); // 得到一个窗口句柄来操作 if ( !hNext.Equals(IntPtr.Zero) ) { // 得到窗口标题 NativeWIN32.STRINGBUFFER sLimitedLengthWindowTitle; NativeWIN32.GetWindowText(hNext, out sLimitedLengthWindowTitle, 256); String sWindowTitle = sLimitedLengthWindowTitle.szText; if (sWindowTitle.Length>0) { // 在列表中找这个标题 foreach (ListViewItem item in listView1.Items) { if ( sWindowTitle.StartsWith(item.Text) ) NativeWIN32.SendMessage(hNext, NativeWIN32.WM_SYSCOMMAND, NativeWIN32.SC_CLOSE, IntPtr.Zero); // try soft kill } } } } while (!hNext.Equals(IntPtr.Zero)); } protected void FindPopupToKill(Process p) { // 遍历所有线程,以找到要KILL的窗口 foreach (ProcessThread t in p.Threads) { int threadId = t.Id; NativeWIN32.EnumThreadProc callbackProc = new NativeWIN32.EnumThreadProc(MyEnumThreadWindowsProc); NativeWIN32.EnumThreadWindows(threadId, callbackProc, IntPtr.Zero /*lParam*/); } } // 回调枚举窗口,通常附于当前进程的线程之一(这句没翻译好,原文是:callback used to enumerate Windows attached to one of the threads of the current process being traversed) bool MyEnumThreadWindowsProc(IntPtr hwnd, IntPtr lParam) { // 得到窗口标题 NativeWIN32.STRINGBUFFER sLimitedLengthWindowTitle; NativeWIN32.GetWindowText(hwnd, out sLimitedLengthWindowTitle, 256); String sWindowTitle = sLimitedLengthWindowTitle.szText; if (sWindowTitle.Length==0) return true; // 在列表中找这个标题 foreach (ListViewItem item in listView1.Items) { if ( sWindowTitle.StartsWith(item.Text) ) NativeWIN32.SendMessage(hwnd, NativeWIN32.WM_SYSCOMMAND, NativeWIN32.SC_CLOSE, IntPtr.Zero); // try soft kill } return true; } // 热键管理 // // private void menu_App_HotKey(object sender, System.EventArgs e) { FormHotkey f = new FormHotkey(); String sHotKey = ""; if (m_ctrlhotkey) sHotKey += "Ctrl"; if (m_shifthotkey) { if (sHotKey.Length!=0) sHotKey += " + "; sHotKey += "Shift"; } if (m_althotkey) { if (sHotKey.Length!=0) sHotKey += " + "; sHotKey += "Alt"; } if (m_winhotkey) { if (sHotKey.Length!=0) sHotKey += " + "; sHotKey += "Win"; } if (sHotKey.Length!=0) sHotKey += " + "; sHotKey += (char)(int)m_hotkey; f.SetHotKey(sHotKey); f.SetParentForm(this); f.Show(); } protected override void WndProc( ref Message m )//重载windows消息传递函数 { const int WM_HOTKEY = 0x0312; switch(m.Msg) { case WM_HOTKEY: IntPtr hwnd = NativeWIN32.GetForegroundWindow(); if (!hwnd.Equals(IntPtr.Zero)) { NativeWIN32.STRINGBUFFER sWindowTitle; NativeWIN32.GetWindowText(hwnd, out sWindowTitle, 256); if (sWindowTitle.szText.Length>0) AddWindowTitle( sWindowTitle.szText ); } break; } base.WndProc(ref m ); } public void AddWindowTitle(String sTitle) { if (sTitle.Length==0) return; if (listView1.Items.Count==0) { listView1.Items.Add(sTitle); return; } // 普通搜索 bool bFound = false; foreach (ListViewItem item in listView1.Items) { bFound = sTitle.StartsWith( item.Text ); } if (!bFound) listView1.Items.Add(sTitle); } public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows) { m_hotkey = c; m_ctrlhotkey = bCtrl; m_shifthotkey = bShift; m_althotkey = bAlt; m_winhotkey = bWindows; // 更新热键 NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None; if (m_ctrlhotkey) modifiers |= NativeWIN32.KeyModifiers.Control; if (m_shifthotkey) modifiers |= NativeWIN32.KeyModifiers.Shift; if (m_althotkey) modifiers |= NativeWIN32.KeyModifiers.Alt; if (m_winhotkey) modifiers |= NativeWIN32.KeyModifiers.Windows; NativeWIN32.RegisterHotKey(Handle, 100, modifiers, m_hotkey); //Keys.J); } } }