前兩天在博客園看到《如何防止程序屢次運行》,文章寫的很好,最後還留下一個問題給咱們思考。關於Winform的防止屢次運行,曾經也想研究過,可是後來工做上沒有須要,因而就放棄了研究,這兩天找資料,將其封裝了一下,最後實現的效果爲:Winform程序運行後,再次點擊exe,會將Winform顯示出去,若該窗體被其餘窗體遮擋,則將其前置,若該窗體被最小化至托盤,將其顯示並前置。html
使用命名事件,進程在此啓動時,前一個進程會收到通知,並作出迴應。ide
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; namespace Ulitiy { /// <summary> /// 任務欄簡單封裝 /// </summary> /// <remarks> /// 檢查程序是否再次運行:在main方法裏調用:TaskBarUtil.CheckCreated(); /// 主窗體在load事件或者構造方法初始化組件後調用:new TaskBarUtil(this, notifyIcon1); /// </remarks> public class TaskBarUtil { private Form mainForm; private NotifyIcon notifyIcon1; public static EventWaitHandle ProgramStarted; public TaskBarUtil(Form main, NotifyIcon notifyIcon1) { this.mainForm = main; this.notifyIcon1 = notifyIcon1; Load(); } [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); #region 右下角圖標控制 private void Load() { //註冊進程OnProgramStarted ThreadPool.RegisterWaitForSingleObject(ProgramStarted, (obj, timeout) => { ShowForm(); }, null, -1, false); #region 窗體事件 mainForm.SizeChanged += new EventHandler((sender, e) => { if (mainForm.WindowState == FormWindowState.Minimized) { HideForm(); } }); mainForm.FormClosing += new FormClosingEventHandler((sender, e) => { //注意判斷關閉事件Reason來源於窗體按鈕,不然用菜單退出時沒法退出! if (e.CloseReason == CloseReason.UserClosing) { mainForm.WindowState = FormWindowState.Minimized; //使關閉時窗口向右下角縮小的效果 notifyIcon1.Visible = true; e.Cancel = true; } }); #endregion #region 任務欄圖標上下文事件 ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip(); //設置任務欄圖標上下文事件 var tsmShow = new ToolStripMenuItem(); tsmShow.Name = "tsmShow"; tsmShow.Text = "顯示"; tsmShow.Click += new System.EventHandler((sender, e) => { if (mainForm.Visible) return; ShowForm(); }); var tsmExit = new ToolStripMenuItem(); tsmExit.Text = "退出"; tsmExit.Name = "tsmShow"; tsmExit.Click += new System.EventHandler((sender, e) => { Application.Exit(); }); contextMenuStrip1.Items.Add(tsmShow); contextMenuStrip1.Items.Add(tsmExit); #endregion #region 任務欄圖標事件 notifyIcon1.ContextMenuStrip = contextMenuStrip1; notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; //notifyIcon1.Click += new EventHandler((sender, e) => //{ // //ShowForm(); //}); notifyIcon1.MouseClick += new MouseEventHandler((sender, e) => { if (e.Button != MouseButtons.Right) { ShowForm(); } }); #endregion } private void ShowForm() { mainForm.Visible = true; //顯示窗體 if (mainForm.WindowState == FormWindowState.Minimized) mainForm.WindowState = FormWindowState.Normal; //恢復窗體默認大小 //該屬性在設置後,再次雙擊exe,會致使窗體在彈出時假死,使用form的Actived事件替代 //mainForm.ShowInTaskbar = true; mainForm.Show(); //前置該窗體 SetForegroundWindow(mainForm.Handle); } private void HideForm() { mainForm.Visible = false; //隱藏窗體 //notifyIcon1.ShowBalloonTip(3000, "提示", "雙擊恢復窗口", ToolTipIcon.Info); //出顯汽泡提示,能夠不用 //mainForm.ShowInTaskbar = false; //從狀態欄中隱藏 mainForm.Hide(); } #endregion #region 檢查是否啓動過,若是啓動則通知前一個進程,並退出當前進程 /// <summary> /// 檢查是否啓動過,若是啓動則通知前一個進程,並退出當前進程 /// </summary> public static void CheckCreated() { // 嘗試建立一個命名事件 bool createNew; //ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew); ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, Application.ProductName, out createNew); // 若是該命名事件已經存在(存在有前一個運行實例),則發事件通知並退出 if (!createNew) { TaskBarUtil.ProgramStarted.Set(); Environment.Exit(1); } } #endregion } }
其中遇到的問題有在顯示和隱藏對窗體的操做中,若是改變form的ShowInTaskbar會出問題。通過不嚴格的測試,這種發生在,在Winform運行後,屢次點擊exe,在此過程當中單機窗體關閉,偶爾會出現沒法找到句柄的錯誤。因此在顯示和隱藏窗體的操做中,就沒有對該屬性進行操做。測試
封裝類包含了以下功能:this
一、Winform 進程只能運行一個實例。spa
二、Winform 任務欄圖標含上下文菜單,顯示和退出,幷包含相應的事件。code
3. Winform 任務欄圖標含鼠標點擊事件,點擊即顯示窗體。orm
使用過程當中注意: 拖入notifyicon控件,並指定圖標。htm
若是不須要這其中的功能,能夠將類任意修改,知足你的須要。blog
示例下載進程