C#實(shí)現(xiàn)Winform序在系統(tǒng)托盤顯示圖標(biāo)和開機(jī)自啟動(dòng)
實(shí)現(xiàn)步驟
- 創(chuàng)建 NotifyIcon 控件并設(shè)置屬性;
- 編寫 NotifyIcon 響應(yīng)控制事件;
- 在主窗體的Load事件中將 NotifyIcon 添加到系統(tǒng)托盤;
- 程序退出時(shí),移除系統(tǒng)托盤的 NotifyIcon;
NotifyIcon 控件,通常用于在系統(tǒng)托盤中顯示圖標(biāo),通過(guò)使用它就可以我們想要的效果。
屬性 | 描述 |
---|---|
Icon | 在系統(tǒng)托盤中顯示的圖標(biāo) |
Text | 鼠標(biāo)懸停在圖標(biāo)時(shí)顯示的文本 |
Visible | 指定是否可見 |
常用方法
方法 | 描述 |
---|---|
ShowContextMenu | 在系統(tǒng)托盤上下文菜單中顯示指定的菜單 |
添加控件(拖拽方式)
將NotifyIcon和一個(gè)ContextMenuStrip控件。拖到主窗體中可以修改控件名稱
NotifyIcon 托盤圖標(biāo)
ContextMenuStrip 托盤圖標(biāo)右擊彈出的菜單
設(shè)置控件
點(diǎn)擊 ContextMenuStrip 右上方的三角圖標(biāo) -> 編輯項(xiàng),彈出項(xiàng)信合編輯器
添加右健菜單信息
添加主窗體事件
在最小化或關(guān)閉主窗體時(shí),顯示在任務(wù)欄托盤區(qū)域,實(shí)現(xiàn)了單擊關(guān)閉時(shí),不真正關(guān)閉程序,而是將主界面隱藏HIDE掉,同時(shí)開始顯示托盤菜單。
// 只有Form_Closing事件中 e.Cancel可以用。 // 你的是Form_Closed事件。 Form_Closed事件時(shí)窗口已關(guān)了 ,Cancel沒(méi)用了; // Form_Closing是窗口即將關(guān)閉時(shí)詢問(wèn)你是不是真的關(guān)閉才有Cancel事件 private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) { // 注意判斷關(guān)閉事件reason來(lái)源于窗體按鈕,否則用菜單退出時(shí)無(wú)法退出! if (e.CloseReason == CloseReason.UserClosing) { //取消"關(guān)閉窗口"事件 e.Cancel = true; // 取消關(guān)閉窗體 //使關(guān)閉時(shí)窗口向右下角縮小的效果 this.WindowState = FormWindowState.Minimized; this.mainNotifyIcon.Visible = true; //this.m_cartoonForm.CartoonClose(); this.Hide(); return; } }
實(shí)現(xiàn)雙擊托盤打開主程序
// 添加托盤程序 // 版本更新自1.0.1 private void mainNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) { if (this.Visible) { this.WindowState = FormWindowState.Minimized; this.mainNotifyIcon.Visible = true; this.Hide(); } else { this.Visible = true; this.WindowState = FormWindowState.Normal; this.Activate(); } }
// 添加托盤程序右鍵菜單項(xiàng) // 版本更新自1.0.1 // 退出 // 添加日期 -- 2015-07-29 21:44 private async void toolStripMenuItemQuit_Click(object sender, EventArgs e) { if (MessageBox.Show("你確定要退出?", "系統(tǒng)提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { this.mainNotifyIcon.Visible = false; this.Close(); this.Dispose(); System.Environment.Exit(System.Environment.ExitCode); } }
代碼方式添加
using System; using System.Windows.Forms; namespace Fountain.WinForm.NotifyDemo { public partial class FormMain : Form { /// <summary> /// 通知控件 /// </summary> private NotifyIcon notifyIcon = new NotifyIcon(); /// <summary> /// 通知控件顯示菜單 /// </summary> private ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); /// <summary> /// 構(gòu)造方法 /// </summary> public FormMain() { InitializeComponent(); } /// <summary> /// 窗體加載 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormMain_Load(object sender, EventArgs e) { this.InitializeNotifyMenu(); this.notifyIcon.Text = this.Text; this.notifyIcon.Visible = true; this.notifyIcon.Icon = this.Icon; this.notifyIcon.ContextMenuStrip = this.contextMenuStrip; this.notifyIcon.DoubleClick += notifyIcon_DoubleClick; } /// <summary> /// 托盤菜單 /// </summary> private void InitializeNotifyMenu() { try { contextMenuStrip.Items.Clear(); ToolStripMenuItem showMenuItem = new ToolStripMenuItem("顯示界面"); showMenuItem.Tag = "顯示"; showMenuItem.Click += new EventHandler(ShowMenuItem_Click); contextMenuStrip.Items.Add(showMenuItem); ToolStripMenuItem sboutMenuItem = new ToolStripMenuItem("關(guān)于"); sboutMenuItem.Tag = "關(guān)于"; sboutMenuItem.Click += new EventHandler(AboutMenuItem_Click); contextMenuStrip.Items.Add(sboutMenuItem); ToolStripMenuItem exitMenuItem = new ToolStripMenuItem("退出"); exitMenuItem.Tag = "退出"; exitMenuItem.Click += new EventHandler(ExistMenuItem_Click); contextMenuStrip.Items.Add(exitMenuItem); } catch(Exception exception) { throw new Exception(exception.Message); } } /// <summary> /// 右擊任務(wù)欄圖標(biāo) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void notifyIcon_DoubleClick(object sender, EventArgs e) { try { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; this.Hide(); } else if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } } catch (Exception objException) { throw new Exception(objException.Message); } } /// <summary> /// 顯示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ShowMenuItem_Click(object sender, EventArgs e) { try { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } catch (Exception objException) { throw new Exception(objException.Message); } } /// <summary> /// 關(guān)于 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AboutMenuItem_Click(object sender, EventArgs e) { try { } catch (Exception objException) { MessageBox.Show(objException.Message); } } /// <summary> /// 退出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ExistMenuItem_Click(object sender, EventArgs e) { try { if (MessageBox.Show("你確定要退出程序嗎?","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { this.notifyIcon.Visible = false; this.notifyIcon.Dispose(); this.Dispose(); Application.Exit(); } } catch (Exception objException) { MessageBox.Show(objException.Message); } } /// <summary> /// 主窗體關(guān)閉 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { try { e.Cancel = true; this.Hide(); this.notifyIcon.Dispose(); } catch (Exception objException) { MessageBox.Show(objException.Message); } } /// <summary> /// 窗體大小變化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormMain_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.ShowInTaskbar = false; this.Hide(); this.notifyIcon.Visible = true; } } } }
系統(tǒng)開機(jī)自啟動(dòng)應(yīng)用程序
using Microsoft.Win32; using System; using System.Diagnostics; using System.Windows.Forms; namespace Fountain.WinForm.NotifyDemo { public partial class FormMain : Form { /// <summary> /// 構(gòu)造方法 /// </summary> public FormMain() { InitializeComponent(); } /// <summary> /// 窗體加載 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormMain_Load(object sender, EventArgs e) { //一般不會(huì)放這個(gè)事件里去設(shè)置,而且設(shè)置時(shí)需要看下注冊(cè)表要有沒(méi)有寫入成功有的話可以不用調(diào)了,偷懶多調(diào)一次也沒(méi)事 string applictionName = Process.GetCurrentProcess().MainModule.ModuleName; string applictionPath = Process.GetCurrentProcess().MainModule.FileName; #region 當(dāng)前登陸用戶的注冊(cè)表啟動(dòng)項(xiàng) RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); registryKey.SetValue(applictionName, applictionPath); #endregion #region 所有用戶的注冊(cè)表啟動(dòng)項(xiàng) //RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); //registryKey.SetValue(applictionName, applictionPath); #endregion } } }
到此這篇關(guān)于C#實(shí)現(xiàn)Winform序在系統(tǒng)托盤顯示圖標(biāo)和開機(jī)自啟動(dòng)的文章就介紹到這了,更多相關(guān)C# Winform開機(jī)自啟動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)ProperTyGrid自定義屬性的方法
這篇文章主要介紹了C#實(shí)現(xiàn)ProperTyGrid自定義屬性的方法,主要通過(guò)接口ICustomTypeDescriptor實(shí)現(xiàn),需要的朋友可以參考下2014-09-09C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03WPF實(shí)現(xiàn)Table布局控件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用WPF實(shí)現(xiàn)Table布局控件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10C#實(shí)現(xiàn)基于鏈表的內(nèi)存記事本實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)基于鏈表的內(nèi)存記事本,實(shí)例分析了C#基于鏈表實(shí)現(xiàn)的記事本功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07