引言
我們在使用桌面應用程序時,經常會看到一些程序在系統托盤中顯示圖標,且會有一些常用功能。如QQ、釘釘、微信等,同時,這些程序在系統啟動后也跟隨自動啟動。本文將介紹C#實現程序在系統托盤中顯示圖標及開機自啟動的內容。實現
1、程序在系統托盤中顯示圖標
NotifyIcon 控件,通常用于在系統托盤中顯示圖標,通過使用它就可以我們想要的效果。
常用屬性
屬性 | 描述 |
---|
Icon | 在系統托盤中顯示的圖標 |
Text | 鼠標懸停在圖標時顯示的文本 |
Visible | 指定是否可見 |
常用方法
方法 | 描述 |
---|
ShowContextMenu | 在系統托盤上下文菜單中顯示指定的菜單 |
實現步驟
3、在主窗體的Load事件中將 NotifyIcon 添加到系統托盤;4、程序退出時,移除系統托盤的 NotifyIcon;
示例代碼
using System;
using System.Windows.Forms;
namespace Fountain.WinForm.NotifyDemo
{
public partial class FormMain : Form
{
private NotifyIcon notifyIcon = new NotifyIcon();
private ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
public FormMain()
{
InitializeComponent();
}
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;
}
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("關于");
sboutMenuItem.Tag = "關于";
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);
}
}
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);
}
}
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);
}
}
private void AboutMenuItem_Click(object sender, EventArgs e)
{
try
{
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
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);
}
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
e.Cancel = true;
this.Hide();
this.notifyIcon.Dispose();
}
catch (Exception objException)
{
MessageBox.Show(objException.Message);
}
}
private void FormMain_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
this.notifyIcon.Visible = true;
}
}
}
}
2、系統開機自啟動應用程序
通過將應用程序往注冊表開機啟動項,實現開機自啟動。下面通過示例介紹其實現。using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace Fountain.WinForm.NotifyDemo
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
string applictionName = Process.GetCurrentProcess().MainModule.ModuleName;
string applictionPath = Process.GetCurrentProcess().MainModule.FileName;
#region 當前登陸用戶的注冊表啟動項
RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
registryKey.SetValue(applictionName, applictionPath);
#endregion
#region 所有用戶的注冊表啟動項
#endregion
}
}
}
小結
以上是C#實現系統托盤顯示應用程序圖標與應用程序開機自啟動的實現案例。
該文章在 2024/7/12 17:04:08 編輯過